当前位置:网站首页>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 .
边栏推荐
- 智慧风电:数字孪生 3D 风机智能设备运维
- SQL, CTE, FLG CASE问题
- numpy np tips: numpy数组的squeeze等处理
- 如何绘制产业招商地图
- Upgrade cmake
- Openmmlab environment configuration
- 【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
- LeetCode 剑指Offer II 091 粉刷房子[动态规划] HERODING的LeetCode之路
- opencv是开源的吗?
- 1280_ C language to find the average value of two unsigned integer
猜你喜欢
Zoran community

DAP数据调度功能完善说明

Development of trading system (VI) -- HFT high frequency trading

"Comment positionner l'industrie" dans la planification industrielle locale / parc

Serious PHP defects can lead to rce attacks on QNAP NAS devices

UCLA | 用于黑盒优化的生成式预训练

讲座记录《惯性导航的新应用——惯性测量》

Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!
![Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding](/img/ad/69fce7cf064479a0ddd477fb935de2.png)
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding

智慧风电:数字孪生 3D 风机智能设备运维
随机推荐
2.吹响半音阶口琴
Is opencv open source?
【LeetCode】22. bracket-generating
DAP data scheduling function improvement description
Shutter fittedbox component
Cesium loading display thermal diagram
IntStream API介绍
How to draw an industry investment map
(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
navicat可不可以直接操作安卓数据库SQLite
95% of programmers fish here
Win10 environment phpstudy2016 startup failure record
Mathematical analysis_ Notes_ Chapter 3: limits
Hello CTP (III) - CTP quotation API
【LeetCode】143. Rearrange linked list
Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
Development of trading system (VIII) -- Construction of low delay network
Development of trading system (I) -- Introduction to trading system
Cesium 加载显示热力图