Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 709
monster Avatar asked Nov 26 '15 12:11

monster


People also ask

What is the advantage of Laravel blade template?

The blade templates are stored in the /resources/view directory. The main advantage of using the blade template is that we can create the master template, which can be extended by other files.

Is Laravel blade fast?

Blade is very fast, because it is simply a handful of regular expressions that are run against your templates to compile them to pure PHP.

What is @stack in Laravel blade?

One of blade directive @stack('scripts') is very helpful when you have javascript need to execute in the child page. I create a fresh Laravel installation to demo an example. I have to make auth scaffolding with laravel/ui package because I'm using Laravel 6.


1 Answers

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.

like image 94
rdiz Avatar answered Sep 28 '22 03:09

rdiz