当前位置:网站首页>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 .

原网站

版权声明
本文为[Angry devil]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210535177662.html