我用Google搜索了两个小时,但未找到答案.也许你可以帮忙.
当我在MyController中定义时:
class MyController extends Base_Controller {
public $layout = 'layouts.default';
public function get_index() {
$entries = Entry::all();
return View::make('entries.index')
->with('entries', $entries);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在entries\index.blade.php中:
@section('content')
<h1>Test</h1>
@endsection
Run Code Online (Sandbox Code Playgroud)
在layouts\default.blade.php中:
<!DOCTYPE html>
<html>
<body>
@yield('content')
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
什么都没有显示出来.我不明白为什么.当我在MyController返回部分替换时:
$this->layout->nest('content', 'entries.index', array(
'entries' => $entries
));
Run Code Online (Sandbox Code Playgroud)
然后一切正常,但是..它看起来不干净,我不喜欢它.在每个视图中添加时,一切@layout('layouts.default')都运行良好,但它不是DRY.例如,在RoR中,我不需要在Controller中执行此类操作.
如何在MyController一个布局中定义和使用return View::make(我认为这是正确的方法)或如何做得更好?
Joe*_*son 15
要在控制器中使用布局,您必须指定:
public $layout = 'layouts.default';
Run Code Online (Sandbox Code Playgroud)
您也不能在方法中返回,因为它将覆盖$ layout的使用.相反,要将您的内容嵌入您使用的布局中:
$this->layout->nest('content', 'entries.index', array('entries' => $entries));
Run Code Online (Sandbox Code Playgroud)
现在无需在方法中返回任何内容.这将解决它.
编辑:
"美丽的方式?"
$this->layout->nest('content', 'entries.index')->with('entries', $entries);
$this->layout->content = View::make('entries.index')->with('entries', $entries);
$this->layout->entries = $entries;
$this->layout->nest('content', 'entries.index');
Run Code Online (Sandbox Code Playgroud)