当前位置:网站首页>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 .
边栏推荐
- AI quantitative transaction (II) -- tushare financial data framework
- 【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
- 95% of programmers fish here
- 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
- 95% 程序员都在这里摸鱼……
- opencv最大能打开多少图像?
- 无法安装redis接口
- Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'
- AI quantitative transaction (I) -- Introduction to quantitative transaction
- 微信小程序中的列表渲染
猜你喜欢
![[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.](/img/62/6152d5a30c92a340cb286c7b1cbc54.png)
[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.

Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'

讲座记录《多种空间大地测量技术的数据处理方法和应用》

NFT insider 63: the sandbox reached a cooperation with Time magazine, and YGG established Spain's subdao

Development of trading system (V) -- Introduction to Sinovel counter

La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox

1280_C语言求两个无符号整形的平均值

Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly

警惕超范围采集隐私-移动APP违规十宗罪

Lecture record: data processing methods and applications of various spatial geodetic techniques
随机推荐
5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate
The 5th series of NFT works of missing parts was launched on the sandbox market platform
论文阅读《LSD-SLAM: Large-Scale Direct Monocular SLAM》
Smart contract learning materials
DAP data scheduling function improvement description
2.吹响半音阶口琴
Development of trading system (V) -- Introduction to Sinovel counter
文本关键词提取:ansj
Qt编译数据库插件通用步骤说明
Development of trading system (VI) -- HFT high frequency trading
[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.
"How to carry out industrial positioning" in local / Park industrial planning
佐喃社区
Lecture record: new application of inertial navigation - inertial measurement
OBS Browser+浏览器的基本使用
Cesium graphic annotation circle, square, polygon, ellipse, etc
Uniapp makes mobile app programs, using uni Choosevideo record video, video playback is fuzzy, and the resolution is low
BSC parsing input data of transaction
kenlm
opencv怎么安装?opencv下载安装教程