简体   繁体   中英

pass variable from controller to template laravel

adminController.php

<?php

class AdminController extends BaseController {

    protected $layout = 'layouts.admin';

    /**
     * Show the profile for the given user.
     */
    public function register()
    {
        $this->layout->title = 'admin title';
        $this->layout->menu_active = 'register';
        $this->layout->content = View::make('pages.admin.register');
    }
}

layout/admin.blade.php

<!doctype html>
<html>
<head>
    @include('includes.head')

</head>
<body>
<div class="container">

    <header class="row">
        @include('includes.header')
    </header>

    <div id="main" class="row">

            @yield('content')

    </div>

    <footer class="row">
        @include('includes.footer')
    </footer>

</div>
</body>
</html>

includes/head.blade.php

<meta charset="utf-8">
<meta name="description" content="">
<meta name="author" content="Scotch">

 <title>@yield('title', 'default title')</title>

<!-- load bootstrap from a cdn -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/3.0.3/css/bootstrap-combined.min.css">

question is

  1. how do i get admin title on @yield rather then default title that i set on the controller?

  2. how can i use the $this->layout->variable to pass variables in templates?

How do I get default title on @yield rather then admin title that i set on the controller?

In your register method you are setting the title using following line

$this->layout->title = 'admin title';

You should use {{ $title or 'Default Title' }} in your view . Here, or used to print the default value when the given variable is undefined.

How can I use the $this->layout->variable to pass variables in templates?

The way you are setting the variables right now, for example:

$this->layout->variable = 'value';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM