当前位置:网站首页>Laravel document sorting 9. Blade template
Laravel document sorting 9. Blade template
2022-06-25 04:19:00 【Angry devil】
Preface :Laravel Document sorting , Only for record , Nothing else .
1、blade What is it? ?
Blade yes Laravel Provided template engine
2、blade What is the view file of ?
The suffix is .blade.php The file of , Stored in resources/views Catalog
3、blade The advantages of templates are ?
Template inheritance And block
4、blade Template example :
<!-- The document is kept in resources/views/layouts/master.blade.php -->
<html>
<head>
<title> Application name - @yield('title')</title>
</head>
<body>
@section('sidebar')
This is the main sidebar .
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
@section Define a content block ,@yield Used to display the contents of a specified block .
5、 Inherited layout of the page
example , Pick up 4
<!-- Keep in resources/views/child.blade.php -->
@extends('layouts.master')
@section('title', ' The page title ')
@section('sidebar')
@parent
<p> This side will be attached to the main sidebar .</p>
@endsection
@section('content')
<p> This is my main content .</p>
@endsection
explain :
1、@extend The command specifies which layout the child page inherits , here , inherited master.blade.php Layout
2、master Layout , Defined <title> Application name - @yield('title')</title>, therefore , Subpages can be accessed through @section Define the contents of a block , The others are the same .
3、@parent command , There's a little bit of caution here , It is increasing , Instead of covering the content [email protected] The command is displayed when the view is output , Replaced with the contents of the layout .
6、blade Page assignment
Route::get('greeting', function () {
return view('welcome', ['name' => 'Samantha']);
});
Page shows :
Hello, { { $name }}.
Ps: Any code you want can be put into blade In the template
Such as UNIX Timestamp { { time() }}.
Be careful :blade Template { { }} The syntax will automatically call PHP Of htmlentites Function to defend against XSS attack
7、blade And js Grammatical conflict
A great deal of js There is also the habit of using curly braces to display the specified expression , To avoid conflict ,blade The template engine uses @ Symbols to distinguish .
<h1>Laravel</h1>
Hello, @{ { name }}.
8、blade The trinocular operation of the template
{ { isset($name) ? $name : 'Default' }}
It can also be used as an alternative to :
{ { $name or 'Default' }}
9、 Some variables , Assign values to the blade Templates , Will be htmlentites() Function escape , however , You don't want to be escaped , What do I do ?
The following example :
Hello, {!! $name !!}.
Warning : For user provided data , Please always use { { }} Syntax to parse , To prevent XSS attack
10、if expression
@if (count($records) === 1)
I have a record !
@elseif (count($records) > 1)
I have multiple records !
@else
I don't have any records !
@endif
11、@unless Unless
@unless (Auth::check())
You are not signed in .
@endunless
Parentheses are false Show “ You are not signed in ”
12、 loop
@for ($i = 0; $i < 10; $i++)
The current value is { { $i }}
@endfor
@foreach ($users as $user)
<p> This user is { { $user->id }}</p>
@endforeach
@forelse ($users as $user)
<li>{ { $user->name }}</li>
@empty
<p> No users </p>
@endforelse
@while (true)
<p> I'm always running a cycle .</p>
@endwhile
13、@include Bring in subviews
<div>
@include('shared.errors')
<form>
<!-- Form content -->
</form>
</div>
Ps: The child view inherits all the data of the parent view , But if , You also want to pass additional data to the subview , Add one more array parameter , that will do . Do the following :
@include('view.name', ['some' => 'data'])
Be careful : Avoid using... In views __DIR__ And __FILE__ constant
14、@each command
@each('view.name', $jobs, 'job')
Ps: Parameter one 、 Detail view name ; Parameter two 、 Variables to pass ; Parameter 3 、 The name of the variable value received in the view
let me put it another way , What does this command mean , to view.name View passing variables $jobs Value of to page job.
15、 notes
{ {-- This comment will not appear after rendering HTML --}}
Ps: Just do as above .
16、@inject With services
@inject('metrics', 'App\Services\MetricsService')
<div>
Monthly income :{ { $metrics->monthlyRevenue() }}.
</div>
Ps:@inject The command can take out laravel Container to page in server .
Parameter one : Name of the service ; Parameter two : The full name of the class or interface of the service .
17、 Customize blade command
Blade Allow custom commands , Use directive() Methods registration , Examples are as follows :
<?php
namespace App\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Run the startup process after service registration .
*
* @return void
*/
public function boot()
{
Blade::directive('datetime', function($expression) {
return "<?php echo with{$expression}->format('m/d/Y H:i'); ?>";
});
}
/**
* Register the binding in the container .
*
* @return void
*/
public function register()
{
//
}
}
stay blade page , Call this custom command , For example, page transmission $val Parameters , Call the following :
@datetime($var)
Ps: Pay attention to... In the custom method with() Method , It simply returns the value of the specified object , And allow chain operation .
边栏推荐
- 【LeetCode】148. 排序链表
- uniapp 制作手机app程序, 使用uni.chooseVideo录制视频,视频播放模糊分辨率低的原因
- Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
- Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
- 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
- 如何绘制产业招商地图
- Color NFT series representing diversity launched on the sandbox market platform
- BSC parsing input data of transaction
- Exercise: how difficult is it to simulate the blessing lottery two-color ball - China 500W grand prize? Just run the code.
- Mysql的order by
猜你喜欢
![[harmony OS] [ark UI] basic ETS context operations](/img/fb/a1b8463ba160e6c5aa23d671a0c245.png)
[harmony OS] [ark UI] basic ETS context operations

Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand

How to install opencv? Opencv download installation tutorial

SEO的5大关键指标:排名+流量+会话+停留时长+跳出率

How many images can opencv open?

如何绘制产业招商地图

【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示

冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市

Acmstreamopen return value problem

Cesium loading display thermal diagram
随机推荐
Cesium 加载显示热力图
DAP data scheduling function improvement description
《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台
Development of trading system (XIII) -- Analysis of quickfix source code
【LeetCode】148. Sort linked list
Watch out for the stolen face! So many risks of face recognition used every day?
What is the difference between learning code, rolling code and fixed code? The number of repeated codes, coding capacity and the principle of rolling code
acmStreamOpen返回值问题
Vigilance against over range collection of privacy - ten mobile app violations
Development of trading system (V) -- Introduction to Sinovel counter
【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
Smart wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
讲座记录《捷联惯导解算的历史及发展》
Where is the red area of OpenCV?
opencv 红色区域在哪里?
Hello CTP (III) - CTP quotation API
La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox
Mathematical analysis_ Notes_ Chapter 3: limits
How to use ide to automatically sign and debug Hongmeng application
AI quantitative transaction (II) -- tushare financial data framework