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

后端 未结 11 2001
猫巷女王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:22
    $data['title'] = $this->layout->title = 'The Home Page';
    $this->layout->content = View::make('home', $data);
    

    I've done this so far because I needed in both the view and master file. It seems if you don't use $this->layout->title it won't be available in the master layout. Improvements welcome!

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

    It appears as though I can pass variables to the entire layout using attributes on the layout object, for example to solve my problem I was able to do the following:

    $this->layout->title = 'Home page';
    
    0 讨论(0)
  • 2021-01-30 04:27
    class PagesController extends BaseController {
        protected $layout = 'layouts.master';
    
        public function index()
        {
            $this->layout->title = "Home page";
            $this->layout->content = View::make('pages/index');
        }
    }
    

    At the Blade Template file, REMEMBER to use @ in front the variable.

    ...
    <title>{{ $title or '' }}</title>
    ...
    @yield('content')
    ...
    
    0 讨论(0)
  • 2021-01-30 04:29

    You can try:

    public function index()
    {
        return View::make('pages/index', array('title' => 'Home page'));
    }
    
    0 讨论(0)
  • 2021-01-30 04:33

    I was able to solve that problem by adding this to my controller method:

        $title = 'My Title Here';
        View::share('title', $title);
    

    $this->layout->title = 'Home page'; did not work either.

    0 讨论(0)
提交回复
热议问题