一、概述
Blade是Laravel提供的一个既简单又强大的模板引擎
和其他流行的PHP模板引擎不一样,Blade并不限制你在视图view中使用原生的PHP代码
所有的Blade视图页面都将被编译成原生的PHP代码并缓存起来,除非你的的模板文件修改,否则不会重新编译
模板继承:section,yield,extends,parent.
二、实例
1、定义布局模板 views/people/layout/layout.blade.php
Laravel @yield('title')
.body{margin:px;padding:px;}
.header{width:100%;height:60px;background:red;margin-bottom:10px;}
.sidebar{width:20%;height:500px;background:green;
margin-right:20px;margin-bottom:10px;}
.content{width:72%;height:500px;background:blue;margin-bottom:10px;}
.footer{width:100%;height:60px;background:black}
div{float:left;}
@section('header')
头部
@show
@section('sidebar')
侧边栏
@show
@yield('content','主要内容区域')
@section('footer')
底部
@show
2、控制器方法分配数据
//模板技术
publicfunctionsection1(){
//.和/都可以
$name='helloiu';
$data=['sss','zzz'];
$students=Student::get();
returnview('people.student.section1',[
'name'=>$name,
'data'=>$data,
'students'=>$students,
]);
}
publicfunctionurlTest(){
return'urlTest';
}
3、子模板 views/people/student/section1.blade.php
@extends('people.layout.layout')
@section('header')
@parent
HEADER
@stop
@section('sidebar')
sideBar
@stop
@section('content')
Content
{{$name}}
{{time()}}
{{date('Y-m-d H:i:s',time())}}
{{in_array($name,$data)?'true':'false'}}
{{var_dump($data)}}
{{isset($name)?$name:'default'}}
{{$nameor'default'}}
@{{$name}}
{{-- 我是注释 --}}
@include('people.common.common',['message'=>'我是error信息']);
@if($name=='helloJiu')
I'm {{$name}}
@elseif($name == 'helloHu')
I'm helloHu
@else
who am i?{{$name}}
@endif
@if(in_array($name,$data))
true
@else
false
@endif
@unless($name=='helloJiu')
I'm {{$name}}
@endunless
@for($i=0;$i
{{ $i }}
@endfor
@foreach($students as $student)
{{ $student->created_at }}
@endforeach
@forelse($students as $student)
{{ $student->name }}
@empty
null
@endforelse
url
urlAction
route
@stop
4、people/common/common.blade.php
5、整个路由系统
Route::get('/',function(){
returnview('welcome');
});
//路由群组
Route::group(['prefix'=>'people'],function(){
Route::get('member/index','People\MemberController@index');
Route::get('member/model','People\MemberController@model');
Route::get('student/index','People\StudentController@index');
Route::get('student/add','People\StudentController@insert');
Route::get('student/update','People\StudentController@update');
Route::get('student/select','People\StudentController@select');
Route::get('student/delete','People\StudentController@delete');
//查询构造器路由
Route::get('student/qbAdd','People\StudentController@qbAdd');
Route::get('student/qbUpdate','People\StudentController@qbUpdate');
Route::get('student/qbDelete','People\StudentController@qbDelete');
Route::get('student/qbSelect','People\StudentController@qbSelect');
Route::get('student/qbSelect','People\StudentController@qbSelect');
Route::get('student/qbSelectJH','People\StudentController@qbSelectJH');
//ORM路由
Route::get('student/ormIndex','People\StudentController@ormIndex');
Route::get('student/ormAddByAttr','People\StudentController@ormAddByAttr');
Route::get('student/ormAddByCreate','People\StudentController@ormAddByCreate');
Route::get('student/ormSelect','People\StudentController@ormSelect');
Route::get('student/ormUpdate','People\StudentController@ormUpdate');
Route::get('student/ormDelete','People\StudentController@ormDelete');
//模板路由
Route::get('student/section1','People\StudentController@section1');
Route::get('student/urlTest',[
'as'=>'url',
'uses'=>'People\StudentController@urlTest',
]);
Route::get('index',function(){
returnview('People.layout.layout');
});
});
//路由中输出视图
Route::get('view',function(){
returnview('welcome');
});
//绑定控制器MemberController::info()
Route::get('member/info','MemberController@info');
Route::any('member/info',[
'uses'=>'MemberController@info',
'as'=>'memberinfo',]
);
Route::any('getID/',['uses'=>'MemberController@getID'])
->where('id','[\d]*');
});
看完本文有收获?点赞、分享是最大的支持!
领取专属 10元无门槛券
私享最新 技术干货