php Laravel 4 Blade @include 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21182399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:52:42  来源:igfitidea点击:

Laravel 4 Blade @include variable

phplaravel-4blade

提问by user1995781

I was trying to do includewith Laravel blade, but the problem is it can't pass the variable. Here's my example code:

我试图include使用 Laravel Blade,但问题是它无法传递变量。这是我的示例代码:

file_include.blade.php

file_include.blade.php

<?php
  $myvar = "some text";

main.blade.php

主刀片.php

@include('file_include')
{{$myvar}}

When I run the file, it return the error "Undefined variable: myvar". So, how can I pass the variable from the include file to the main file?

当我运行该文件时,它返回错误“未定义变量:myvar”。那么,如何将变量从包含文件传递到主文件?

Thank you.

谢谢你。

回答by Kenny

Why would you pass it from the include to the calling template? If you need it in the calling template, create it there, then pass it into the included template like this:

为什么要将它从包含传递给调用模板?如果您在调用模板中需要它,请在那里创建它,然后将其传递到包含的模板中,如下所示:

@include('view.name', array('some'=>'data'))

Above code snippet from http://laravel.com/docs/templates

以上来自http://laravel.com/docs/templates 的代码片段

回答by ErcanE

Unfortunately Laravel Blade engine doesn't support what you expected!.But a little traditional way you can achieve this!

不幸的是,Laravel Blade 引擎不支持您所期望的!但是您可以通过一些传统的方式来实现这一点!

SOLUTION 1- without Laravel Blade Engine

解决方案 1- 没有Laravel Blade Engine

Step a:

步骤一:

from

file_include.blade.php

to

file_include.php

Step b:

步骤 b:

main.blade.php

主刀片.php

<?php 
     include('app/views/file_include.php')
?>
{{$myvar}}


SOLUTION 2with Laravel Blade Engine

解决方案2Laravel Blade Engine

routes.php

routes.php

$data = array(
'data1'         => "one",
'data2'         => "two",
);

View::share('data', $data); 

Access $data array from Any Viewlike this

Any View像这样访问 $data 数组

{{ $data['data1'] }}

Output

输出

one

回答by Shubhamoy

Blade is a Template Engine for Laravel. So try passing the value from the controller or you may define it in the routes.php for testing purposes.

Blade 是 Laravel 的模板引擎。所以尝试从控制器传递值,或者您可以在 routes.php 中定义它以进行测试。

@include is used to include sub-views.

@include 用于包含子视图。

回答by lukaserat

I think you must understand the variable scope in Laravel Blade template. Including a template using @includewill inherit all variables from its parent view(the view where it was defined). But I guess you can't use your defined variables in your child view at the parent scope. If you want your variable be available to the parent try use View::share($variableName, $variableValue)it will be available to all views as expected.

我认为您必须了解 Laravel Blade 模板中的变量范围。包含模板 using@include将从其父视图(定义它的视图)继承所有变量。但是我猜您不能在父作用域的子视图中使用定义的变量。如果您希望您的变量对父级可用,请尝试使用View::share($variableName, $variableValue)它将按预期对所有视图可用。

回答by cecilozaur

In this scenarion $myvarwould be available only on the local scope of the include call.

在这种情况下,n$myvar将仅在包含调用的本地范围内可用。

Why don't you send the variable directly from the controller?

为什么不直接从控制器发送变量?

回答by Sven P

I suggest you do a classic PHP require if you really want to change your variable (then it's automatically by reference)

如果你真的想改变你的变量,我建议你做一个经典的 PHP 要求(然后它会自动通过引用)