2013-04-03 106 views
3

我laravel视图结构是这样的:Laravel视图,布局和变量传递

/views/layouts/default.blade.php 

含有

<html> 
@yield('header') 
<body> 
    @yield('navigation') 
    @yield('content') 
    @yield('footer') 
</body> 

以下通过

/views/partials/footer.blade.php 
/views/partials/header.blade.php 
/views/partials/navigation.blade.php 

以我头视图我有一个var $ title 我试图在主页上动态设置控制器。

所以在位于/pages/index.blade.php 我认为我得到了这个

@layout('layouts.default') 
@section('header') 
    @render('partials.header') 
@endsection 

@section('navigation') 
    @render('partials.menu') 
@endsection 

@section('footer') 
    footer 
@endsection 

@section('content') 
@endsection 

我读的地方,标题var目录应该throught控制器传递,但我不能做到这一点: (

我想这没有任何成功。$标题在头谐音意见未定义..

class Home_Controller extends Base_Controller { 
    public $layout = 'layouts.default'; 
    public function action_index() 
    { 
     $this->layout->nest('header', 'home.index')->with('title', 'James'); 
     $posts = Post::with('author')->all(); 
     return View::make('home.index'); 

    } 

回答

1

在Laravel之刃模板引擎,你可以使用@render@include来渲染视图。

但是,如果您使用@render,呈现的视图将不会从当前视图继承数据。所以,如果你需要继承变量等,你需要使用@include。有关更多信息,请参阅docs on templating