PHP框架laravel中Blade模板引擎的使用

2023-03-2421:26:59后端程序开发Comments802 views字数 2939阅读模式

①Blade是Laravel提供的一个简单强大的模板引擎。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

②和其他流行的PHP模板引擎不一样,Blade并不现在你在视图view中使用原生PHP代码。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

③所有Blade视图页面都将被编译成原生PHP代码并缓存起来,除非模板文件被修改,否则不会被重写编译。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

本博文主要记录了如下功能点:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@yield文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@section文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@extends文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@stop文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@parent文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@include和传参文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@if文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@elseif文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@else文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@endif文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@unless文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@endunless文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@for文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@endfor文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@foreach文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@endforeach文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@forelse文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@empty文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@endforelse文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

以及URL文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

程序结构如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

路由web.php添加:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

Route::prefix('student')->group(function(){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

......
......
......文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

Route::get('section1', 'StudentController@section1');
Route::get('urlTest', 'StudentController@urlTest')->name('url');
});
url.blade.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

<a href="{{url('url')}}">url()</a>
<br/>
<a href="{{action('StudentController@urlTest')}}">action()</a>
<br/>
<a href="{{route('url')}}">route()</a>
layouts.blade.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>轻松学会Laravel - @yield('title')</title>
<style>
.header{
width:1000px;
height:150px;
margin:0 auto;
background:#f5f5f5;
border: 1px solid #ddd;
}
.main{
width:1000px;
height:300px;
margin:0 auto;
clear:both;
margin-top:15px;
}
.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>
<body>
<div class = "header">
@section('header')头部@show
</div>
<div class = "main">
<div class = "sidebar">
@section('sidebar')
侧边栏
@show
</div>
<div class = "content">
@yield('content','主要内容区域')
</div>
</div>
<div class = "footer">
@section('footer')
底部
@show
</div>
</body>
</html>
common1.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

<p>我是include {{$message}}</p>
section1.blade.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@extends('layouts')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@section('title')
哼哼哈嘿
@stop文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@section('header')
@parent
header
@stop文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@section('sidebar')
@parent
sidebar
@stop文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@section('content')
content
<!-- 模板输出php变量 -->
<p>{{$name}}</p>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

<!-- 模板调用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>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

<p>{{isset($name) ? $name : 'default'}}</p>
<p>{{$name or 'default'}}</p>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@include('student.common1', ['message' => '我是错误信息'])
<br/>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@if($name == '小明')
我是小明
@elseif($name == '小黑')
我是小黑
@else
我是谁
@endif文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

<!-- if中可以用php函数 -->
<br/>
@if (in_array($name, $arr))
true
@else
false
@endif文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

<!-- unless的使用,属于if的取反 -->
<br/>
@unless($name != '小明')
我是小明
@endunless文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@unless($name == '小明')
我不是小明
@endunless文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

{{-- @for的使用--}}
<br/>
@for($i = 0; $i < 10; $i++)
<p>{{$i}}</p>
@endfor文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

{{-- @foreach的使用--}}
<br/>
@foreach($students as $student)
<p>{{$student->name}}</p>
@endforeach文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

{{-- foreach的变种forelse--}}
@forelse($students as $student)
<p>{{$student->name}}</p>
@empty
<p>null</p>
@endforelse文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

@stop
StudentController.php关键代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

<?php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

namespace App\Http\Controllers;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

use App\Student;
use Illuminate\Support\Facades\DB;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

class StudentController extends Controller{文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

......
......文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

public function section1(){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

$students = Student::get();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

$name = '小明';
$arr = ['小白', '小黑'];
return view('student.section1', [
'name' => $name,
'arr' => $arr,
'students' => $students
]);
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

public function urlTest(){文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

return view("url");
}
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

————————————————
版权声明:本文为CSDN博主「IT1995」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq78442761/article/details/124881673文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/31578.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/31578.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定