我用谷歌搜索了两个小时,但没有找到答案。也许你能帮忙。
当我定义在我的控制器:
class MyController extends Base_Controller {
public $layout = 'layouts.default';
public function get_index() {
$entries = Entry::all();
return View::make('entries.index')
->with('entries', $entries);
}
}
}
In 条目\index.blade.php:
@section('content')
<h1>Test</h1>
@endsection
And in 布局\default.blade.php:
<!DOCTYPE html>
<html>
<body>
@yield('content')
</body>
</html>
什么也没有显示。我不明白为什么。当我更换时我的控制器返回部分:
$this->layout->nest('content', 'entries.index', array(
'entries' => $entries
));
然后一切正常,但是..它看起来不干净,我不喜欢它。在每个视图中添加时@layout('layouts.default')
一切都运转良好,但并不干燥。例如,在RoR中我不需要在Controller中做这样的事情。
如何定义在MyController
一种布局和使用return View::make
(我认为这是正确的方法)或者如何做得更好?
要在控制器中使用布局,您必须指定:
public $layout = 'layouts.default';
您也不能在方法中返回,因为它将覆盖 $layout 的使用。相反,要将内容嵌入到您使用的布局中:
$this->layout->nest('content', 'entries.index', array('entries' => $entries));
现在不需要在您的方法中返回任何内容。这会修复它。
Edit:
“美丽的方式?”
$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');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)