模板


控制器布局

在Laravel框架中使用模板的一种方法就是通过控制器布局。通过在控制器中指定layout属性,指定的视图就会被创建,并作为默认数据,在actions中返回。

在控制器中定义布局(Layouts)

  1. class UserController extends BaseController {
  2. /**
  3. * The layout that should be used for responses.
  4. */
  5. protected $layout = 'layouts.master';
  6. /**
  7. * Show the user profile.
  8. */
  9. public function showProfile()
  10. {
  11. $this->layout->content = View::make('user.profile');
  12. }
  13. }

Blade模板

Blade是Laravel框架下的一个简单但又强大的模板引擎。 不同于控制器布局,Blade模板引擎由 模板继承模板片段 驱动。所有的Blade模板文件必须使用 .blade.php 文件扩展名。

定义一个Blade布局

  1. <!-- Stored in app/views/layouts/master.blade.php -->
  2. <html>
  3. <body>
  4. @section('sidebar')
  5. This is the master sidebar.
  6. @show
  7. <div class="container">
  8. @yield('content')
  9. </div>
  10. </body>
  11. </html>

使用一个Blade布局

  1. @extends('layouts.master')
  2. @section('sidebar')
  3. @parent
  4. <p>This is appended to the master sidebar.</p>
  5. @stop
  6. @section('content')
  7. <p>This is my body content.</p>
  8. @stop

注意视图中片段只是简单的替换其extend的Blade布局中相应片段。通过在模板片段中使用@parent指令,布局的内容可以包含一个子视图,这样你就可以在布局片段中添加诸如侧边栏、底部信息等内容。

有时候,有些片段可能不能确定被定义了,你可以使用@yield结构给出一个默认值。如下,第二个值即是默认值。

  1. @yield('section', 'Default Content')

其他 Blade模板 控制结构

输出数据

  1. Hello, {{{ $name }}}.
  2. The current UNIX timestamp is {{{ time() }}}.

检测是否存在后输出数据

有时,你可能希望输出一个变量,但又不能确定这个变量是否被设置。直接点,你可能想这么做:

  1. {{{ isset($name) ? $name : 'Default' }}}

然而,除了写一个三目运算符,Blade有如下简写方法:

  1. {{{ $name or 'Default' }}}

显示带有大括号的文本

如果你想显示带有大括号的字符串,你可以在文本前放@符号,这样会忽略Blade解析行为

  1. @{{ This will not be processed by Blade }}

当然,用户提供的全部字符串都应该都被转义(主要对html标签,利用htmlentities编码转义)。如果想转义输出,你可以使用三个大括号语法:

  1. Hello, {{{ $name }}}.

如果不希望数据被转义,可以使用双大括号语法:

  1. Hello, {{ $name }}.

注意: 一定要小心输出的用户提供的内容。使用三个大括号的语法能够直接输出内容中的HTML标签。

If标签

  1. @if (count($records) === 1)
  2. I have one record!
  3. @elseif (count($records) > 1)
  4. I have multiple records!
  5. @else
  6. I don't have any records!
  7. @endif
  8. @unless (Auth::check())
  9. You are not signed in.
  10. @endunless

循环

  1. @for ($i = 0; $i < 10; $i++)
  2. The current value is {{ $i }}
  3. @endfor
  4. @foreach ($users as $user)
  5. <p>This is user {{ $user->id }}</p>
  6. @endforeach
  7. @while (true)
  8. <p>I'm looping forever.</p>
  9. @endwhile

包含子视图

  1. @include('view.name')

你也可以传递数组数据到被包含的视图

  1. @include('view.name', array('some'=>'data'))

覆盖片段

如果想覆盖一整个片段,可以使用overwrite指令:

  1. @extends('list.item.container')
  2. @section('list.item.content')
  3. <p>This is an item of type {{ $item->type }}</p>
  4. @overwrite

输出多语言

  1. @lang('language.line')
  2. @choice('language.line', 1)

注释

  1. {{-- This comment will not be in the rendered HTML --}}

扩展Blade

Blade允许用户定义自己的控制结构。当一个Blade文件被编译后,会调用用户自定义的扩展,用来处理视图内容,从简单的str_replace操作,到很复杂的表达式,总之,你可以做任何事情。

Blade的编译器附带了帮助函数createMatchercreatePlainMatcher,这两个函数可以生成自定义指令。

createPlainMatcher函数主要用于没有参数传递的指令,类似@endif@stop,而createMatcher则用于那些有参数传递的指令。

下面的例子创建@datetime($var)指令,它只是简单的对$var调用->format()方法:

  1. Blade::extend(function($view, $compiler)
  2. {
  3. $pattern = $compiler->createMatcher('datetime');
  4. return preg_replace($pattern, '$1<?php echo $2->format('m/d/Y H:i'); ?>', $view);
  5. });