Laravel中的模板引擎Blade
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>模板继承@yield('title')</title> <style> .header{ width: 1000px; height:150px; margin: 0 auto; background: #f5f5f5; border: 1px solid #dddddd; } .main{ width: 1000px; height:300px; margin: 0 auto; margin-top: 15px; clear:both; } .main .sidebar{ float:left; width:20%; height:inherit; background: #f5f5f5; border:1px solid #ddd; } .main .content{ float: right; width: 75%; height: inherit; background: #f5f5f5; border:1px solid #ddd; } .footer{ width:1000px; height:150px; margin: 0 auto; margin-top:15px; background: #f5f5f5; border:1px solid #ddd; } </style> </head> <body> <div class="header"> @section('header') 头部 @show </div> <div class="main"> <div class="sidebar"> <!--section是用来定义一个视图片段的--> @section('sidebar') 侧边栏 @show </div> <div class="content"> <!--yield表示某个指定section所要显示的内容,可以想象成一个占位符--> @yield('content','主要内容区域') </div> </div> <div class="footer"> @section('footer') 底部 @show </div> </body> </html>
@extends('layouts') @section('header') @parent header @stop @section('sidebar') sidebar @stop @section('content') content <!--模板中输出PHP变量--> <p>{{$name}}</p> <!--模板中调用php代码--> <p>{{time()}}</p> <p>{{date('Y-m-d H:i:s',time())}}</p> <p>{{in_array($name,$arr)?'true':'false'}}</p> <p>{{var_dump($arr)}}</p> <p>{{isset($name)?$name:'default'}}</p> <p>{{$name or 'default'}}</p> <!--原样输出--> <p>@{{ $name }}</p> <!--模板中的注释--> <!--引入子视图include--> @include('user.common1',['message'=>'这里是错误信息']) <!--流程控制--> @if($name=='yxh') I'm yxh @elseif($name=='imooc') I'm imooc @else who am I? @endif @if(in_array($name,$arr)) true @else false @endif @unless($name!='yxh') I'm yxh; @endunless @for($i=0;$i<3;$i++) {{$i}} @endfor {{--@foreach($user as $user)--}} {{--{{$user->username}}--}} {{--@endforeach--}} @forelse($user as $user) {{$user->username}} @empty null @endforelse <a href="{{url('url')}}">url()</a> <a href="{{action('UserController@urlTest')}}">action()</a> <a href="{{route('url')}}">route()</a> @stop
<p>这里是include{{$message}}</p>
public function section1(){ $name = 'yxh'; $arr = ['yxh','imooc']; $user = Admin::get(); return view('user.section1',[ 'name'=>$name, 'arr' =>$arr, 'user'=>$user ]); } public function urlTest(){ return 'urlTest'; }