05. Laravel 4 模板引擎

本文详细介绍了Blade模板系统的各种特性及用法,包括文本输出、条件判断、循环操作、模板引用与继承等核心功能,并提供了丰富的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模板输出

基本输出
<!-- app/views/example.blade.php -->
<p>{{ date('d/m/y') }}</p>
原样输出
<!-- app/views/example.blade.php -->
<p>{{{ '<script>alert("CHUNKY BACON!");</script>' }}}</p>

特殊字符串将被自动转义,最终结果如下:

<!-- app/views/example.blade.php -->
<p>&lt;script&gt;alert(&quot;CHUNKY BACON!&quot;);&lt;/script&gt;</p>

控制结构

if
<!-- app/views/example.blade.php -->
@if ($something == 'Red Panda')
    <p>Something is red, white, and brown!</p>
@elseif ($something == 'Giant Panda')
    <p>Something is black and white!</p>
@else
    <p>Something could be a squirrel.</p>
@endif
foreach
<!-- app/views/example.blade.php -->
@foreach ($manyThings as $thing)
    <p>{{ $thing }}</p>
@endforeach
for
<!-- app/views/example.blade.php -->
@for ($i = 0; $i < 999; $i++)
    <p>Even {{ $i }} red pandas, aren't enough!</p>
@endfor
while
<!-- app/views/example.blade.php -->
@while (isPretty($kieraKnightly))
    <p>This loop probably won't ever end.</p>
@endwhile
unless
<!-- app/views/example.blade.php -->
@unless (worldIsEnding())
    <p>Keep smiling.</p>
@endunless

模板引用

<!-- app/views/header.blade.php -->
<h1>When does the Narwhal bacon?</h1>

<!-- app/views/footer.blade.php -->
<small>Information provided based on research as of 3rd May '13.</small>

<!-- app/views/example.blade.php -->
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Narwhals</title>
</head>
<body>
    @include('header')
    <p>Why, the Narhwal surely bacons at midnight, my good sir!</p>
    @include('footer')
</body>
</html>

模板继承

<!-- app/views/layouts/base.blade.php -->
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    @section('head')
        <link rel="stylesheet" href="style.css" />
    @show
</head>
<body>
    @yield('body')
    @section('message')
        @parent
        <p>parent message.</p>
    @show
</body>
</html>

<!-- app/views/home.blade.php -->
@extends('layouts.base')
@section('head')
    <link rel="stylesheet" href="another.css" />
@stop
@section('body')
    <h1>Hurray!</h1>
    <p>We have a template!</p>
@stop
@section('message')
    @parent
    <p>Fourth</p>
@stop

模板注释

{{-- This is a pretty, and secret Blade comment. --}}

转载于:https://my.oschina.net/5say/blog/186366

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值