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

天大地大妈咪最大 提交于 2019-12-10 02:43:35

问题


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

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

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

    @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.


回答1:


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

Including Sub-Views

@include('view.name')

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

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

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




回答2:


pass an array of data to the included view

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

then use this on view/name folder

{{ $some }}


来源:https://stackoverflow.com/questions/33938362/laravel-blade-pass-variable-via-an-include-or-yield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!