简体   繁体   English

在扩展刀片中声明变量,并在另一个刀片中获取值

[英]Declare a variable in the extended blade and get the value in another blade

How can I get the value that is declared inside the layout.blade.php file to another blade.php . 如何将在layout.blade.php文件中声明的值获取到另一个blade.php中

Here is my Code which is simplified to make you understand : 这是我的代码,简化后可以使您理解:

client.blade.php client.blade.php

<?php
$a = '33';
?>
@extends('layouts.layout')
@section('body')
<?php
echo $b;
?>

In the layout.blade.php : layout.blade.php中

<?php
echo $a;
$b = "hi";
?>

What happens is, I am able to print the variable $a in the layout.php file, but the variable $b that is declared inside the layout.blade.php is not printing in the client.blade.php . 什么情况是,我能够打印变量$alayout.php中的文件,但变量$b是的layout.blade.php内声明未在client.blade.php打印。

How can I make available of it ? 我该如何利用它?

I'm pretty sure you cant do that. 我敢肯定你不能那样做。 The way the template works, you cant send data "back" the other way. 模板的工作方式不能以其他方式“返回”发送数据。 You should declare the variable in your controller, and pass it to both views. 您应该在控制器中声明变量,并将其传递给两个视图。

Or you should use a view composer , and have the variable automatically assigned when the view is created. 或者,您应该使用视图编辑器 ,并在创建视图时自动分配变量。

You can't do that. 你不能那样做。
You should use View::composer as suggested or use the View::share method that allow you to create a sort of global variable avaiable in all the views you need. 您应该按照建议使用View::composer或使用View::share方法,该方法允许您创建一种在所需的所有视图中都可用的全局变量。

For example you can add a View::share('user', Auth::user()); 例如,您可以添加View::share('user', Auth::user()); in your BaseController and use $user in all the views rendered by controllers that extends the BaseController 在您的BaseController并在extends BaseController控制器呈现的所有视图中使用$user

Doc: http://laravel.com/docs/4.2/responses#views Doc: http : //laravel.com/docs/4.2/responses#views

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM