当前位置:网站首页>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 .
边栏推荐
- MySQL order by
- cesium 图形标注圆形、正方形、多边形、椭圆等
- [harmony OS] [arkui] ETS development graphics and animation drawing
- 5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate
- 2.吹响半音阶口琴
- 1、项目第二阶段——用户注册和登陆
- 地方/園區產業規劃之 “ 如何進行產業定比特 ”
- Development of trading system (II) -- market data
- Comparison of towe/ JIRA / tapd / Zen collaboration platforms
- [team learning] SQL programming language notes - task04
猜你喜欢
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
代表多样性的彩色 NFT 系列上线 The Sandbox 市场平台
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s accesses the Internet of things platform, uploads real-time collected data /tcp transparent transmission (and how to obtain and write Lu
Read lsd-slam: large scale direct monolithic slam
Hello CTP (II) -- Introduction to CTP
95% 程序员都在这里摸鱼……
Cesium 拖拽3D模型
[harmony OS] [arkui] ETS development graphics and animation drawing
Coinlist how to operate the middle lot number security tutorial
随机推荐
Text keyword extraction: ansj
Zoran community
[openwrt] we recommend a domestically developed version of openwrt, an introduction to istoreos. It is very easy to use. It is mainly optimized. It solves the problem of Sinicization.
Numpy NP tips: squeeze and other processing of numpy arrays
升级cmake
Coinlist queuing tutorial to improve the winning rate
1280_C语言求两个无符号整形的平均值
How to install opencv? Opencv download installation tutorial
[harmony OS] [arkui] ETS development graphics and animation drawing
[team learning] SQL programming language notes - task04
Mathematical analysis_ Notes_ Chapter 3: limits
numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)
A-table mouse over the display hand, the current line can be clicked
opencv是开源的吗?
Cesium 拖拽3D模型
Development of trading system (IX) -- dark pool technology
Flutter Builder & FutureBuilder组件
Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!
讲座记录《惯性导航的新应用——惯性测量》
Simple integration of client go gin -update