[英]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');
}
}
布局/ 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>
包括/ 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">
問題是
我如何在@yield上獲得admin title
,而不是我在控制器上設置的default title
?
如何使用$ this-> layout-> variables傳遞模板中的變量?
如何在@yield上獲得默認標題,而不是在控制器上設置的管理員標題?
在您的register
方法中,您使用以下行設置title
$this->layout->title = 'admin title';
您應該在view
使用{{ $title or 'Default Title' }}
。 在此處, or
用於在未定義給定變量時打印默認值。
如何使用$ this-> layout-> variable傳遞模板中的變量?
現在設置變量的方式,例如:
$this->layout->variable = 'value';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.