Laravel Blade - 通过 @include 或 @yield 传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33938362/
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-09-14 12:44:46  来源:igfitidea点击:

Laravel Blade - pass variable via an @include or @yield

phplaravellaravel-4

提问by monster

I need to pass a variable to an included Blade file. I have attempted this two-ways; however, neither have been successful.

我需要将一个变量传递给包含的 Blade 文件。我已经尝试了这两种方式;然而,两者都没有成功。

  1. Pass a variable, title, to the included file:

    @section('left')
        @include('modal', ['title' => 'Hello'])
    @stop
    
  2. Use @yieldand set the section:

    @section('left')
        @include('modal')
            @section('title')
            Hello
            @stop
    @stop
    
  1. 将变量 , 传递title给包含的文件:

    @section('left')
        @include('modal', ['title' => 'Hello'])
    @stop
    
  2. 使用@yield和设置部分:

    @section('left')
        @include('modal')
            @section('title')
            Hello
            @stop
    @stop
    

I am using Laravel 4.2. I am unaware if what I am trying to do is possible, but I imagine it is.

我正在使用 Laravel 4.2。我不知道我想要做的事情是否可行,但我想是的。

采纳答案by rdiz

According to the documentation, the include-way should be the way to do it:

根据文档include-way 应该是这样做的方式:

Including Sub-Views

@include('view.name')

You may also pass an array of data to the included view:

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

包括子视图

@include('view.name')

您还可以将一组数据传递给包含的视图:

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

My hunch is that $titleis conflicting with another variable in your nested templates. Just for troubleshooting, try temporarily calling it something else.

我的预感是这$title与嵌套模板中的另一个变量相冲突。仅用于故障排除,请尝试暂时将其称为其他名称。

回答by Imtiaz Pabel

pass an array of data to the included view

将一组数据传递给包含的视图

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

then use this on view/name folder

然后在视图/名称文件夹上使用它

{{ $some }}