Laravel框架学习(视图模板引擎)

1、模板继承

//layouts.blade.php
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>哈哈 @yield('title')</title>
</head>
<style type="text/css">
.header{height: 80px;background: red;}
.sidebar{height: 100px;background: green;}
.content{height: 100px;background: yellow;}
.footer{height: 80px;background: red;}
</style>
<body>
	<div class="header">
	@section('header')
	头部
	@show
	</div>
	<div class="sidebar">
		@section('sidebar')
		侧边栏
		@show
	</div>
	<div class="content">
		@yield('content','主要内容区域')
	</div>
	<div class="footer">
		@section('footer')
		底部
		@show
	</div>
</body>
</html>
//student/section1.blade.php
@extends('layouts')

@section('header')
	@parent
	喀喀喀
@stop

@section('content')
	content
@stop
class StudentController extends Controller
{
	public function index()
	{
		return view('student.section1');
	}
}

2、基础语法及include的使用

//student/section1.blade.php
@extends('layouts')

@section('header')
	@parent
	喀喀喀
@stop

@section('content')
	content
	<!-- 1.模板中输出PHP变量 -->
	{{$name}}
	<!-- 2.调用PHP代码 -->
	{{time()}}
	{{ isset($laravel)?$laravel:'default' }}
	<!-- 3.原样输出 -->
	@{{ $name }}
	{{-- 4.模板注释 --}}
	<!-- 引入子视图 -->
	@include('student.common1')
@stop
//common1.blade.php
<p>I am Include  {{$name}}</p>

3、流程控制

@extends('layouts')

@section('header')
	@parent
	喀喀喀
@stop

@section('content')
	content
	
	@if($name=='Leroi')
		I am Leroi
	@elseif($name=="llL")
		I am llL
	@else
		who am I?
	@endif

	@if(isset($kaka))
		yes
	@else
		no
	@endif

	@unless($name!='Leroi')
		{{$name}}
	@endunless

	@for($i=0;$i<10;$i++)
		{{$i}}
	@endfor

	@foreach($students as $student)
		{{$student->name}}
	@endforeach

	@forelse($students as $student)
		{{$student->name}}
	@empty
		null
	@endforelse

@stop

4、模板中的URL

 	<a href="{{url('studenttest')}}">url</a>
 	<a href="{{action('StudentController@index')}}">action</a>
 	<a href="{{route('studenttest')}}">route</a>

猜你喜欢

转载自blog.csdn.net/Leroi_Liu/article/details/83308177
今日推荐