问题描述
我一直在阅读Laravel 4文档,并正在开发一个演示应用程序来帮助学习.
I've been reading through the Laravel 4 documentation and have been making a demo application to help with learning.
我找不到有关刀片和控制器视图模板的大量文档.哪种方法正确,还是取决于个人喜好?
I couldn't find much documentation on the templating of views with blade and controllers.Which is the correct method or does it come down to personal preference?
例如1
Controllers/HomeController.php
protected $layout = 'layouts.main';
public function showWelcome()
{
$this->layout->title = "Page Title";
$this->layout->content = View::make('welcome');
}
视图/布局/main.blade.php
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
{{ $content }}
</body>
</html>
视图/welcome.blade.php
<p>Welcome.</p>
例如2
Controllers/HomeController.php
protected $layout = 'layouts.main';
public function showWelcome()
{
$this->layout->content = View::make('welcome');
}
视图/布局/main.blade.php
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
视图/welcome.blade.php
@section('title', 'Welcome')
@section('content')
// content
@stop
以上内容的最佳约定和/或优势是什么?
What is the best convention and/or advantages of the the above?
推荐答案
我没有在控制器中存储任何布局信息,而是通过
I don't store any layout information in the controller, I store it in the view via
@extends('layouts.master')
当我需要在控制器中返回视图时,使用:
When I need to return a view in the controller I use:
return \View::make('examples.foo')->with('foo', $bar);
我更喜欢这种方法,因为视图确定要使用的布局,而不是要确定重构的控制器.
I prefer this approach as the view determines what layout to use and not the controller - which is subject to re-factoring.
这篇关于Laravel 4控制器模板/刀片-正确的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!