How do I pass a variable to the layout using Laravel' Blade templating?

后端 未结 11 2000
猫巷女王i
猫巷女王i 2021-01-30 03:30

In Laravel 4, my controller uses a Blade layout:

class PagesController extends BaseController {
    protected $layout = \         


        
相关标签:
11条回答
  • 2021-01-30 04:11

    For future Google'rs that use Laravel 5, you can now also use it with includes,

    @include('views.otherView', ['variable' => 1])
    
    0 讨论(0)
  • 2021-01-30 04:11

    In the Blade Template : define a variable like this

    @extends('app',['title' => 'Your Title Goes Here'])
    @section('content')
    

    And in the app.blade.php or any other of your choice ( I'm just following default Laravel 5 setup )

    <title>{{ $title or 'Default title Information if not set explicitly' }}</title>
    

    This is my first answer here. Hope it works.Good luck!

    0 讨论(0)
  • 2021-01-30 04:15

    if you want to get the variables of sections you can pay like this:

    $_view      = new \View;
    $_sections  = $_view->getFacadeRoot()->getSections();
    dd($_sections);
    /*
    Out:
    array:1 [▼
      "title" => "Painel"
    ]
    */
    
    0 讨论(0)
  • 2021-01-30 04:15

    just try this simple method: in controller:-

     public function index()
       {
            $data = array(
                'title' => 'Home',
                'otherData' => 'Data Here'
            );
            return view('front.landing')->with($data);
       }
    

    And in you layout (app.blade.php) :

    <title>{{ $title }} - {{ config('app.name') }} </title>
    

    Thats all.

    0 讨论(0)
  • 2021-01-30 04:16

    Simplest way to solve:

    view()->share('title', 'My Title Here');
    

    Or using view Facade:

    use View;
    
    ...
    
    View::share('title', 'My Title Here');
    
    0 讨论(0)
  • 2021-01-30 04:17

    If you're using @extends in your content layout you can use this:

    @extends('master', ['title' => $title])
    
    0 讨论(0)
提交回复
热议问题