Laravel's view operation

One, view operation

1. Where is the view written?

Insert picture description here

Note:
1. Views can be managed by directories.
2. The suffix of views is generally blade.php in laravel.
3. The creation of views cannot be achieved through artisan.

2. Naming and rendering of view files

1. File names are accustomed to lower case.
2. The suffix of the file name is blade.php (because there is a set of template engine in laravel that uses blade, you can directly use the label syntax { { KaTeX parse error: Expected'EOF', got'}' at position 6: title}}, may be used php syntax native ... title display data}} syntax, use only the native syntax show two views of the data files exist, the priority display blade.php suffix. <?php echo $title ?>

Case: Write a routing address, call the test method of the Test controller, display the view test2 file (create 2 and verify the priority) to
create the corresponding route:
Insert picture description here
Method:
Insert picture description here
create a view:
Insert picture description here
Insert picture description here

effect:
Insert picture description here


3. Variable allocation and display

grammar:

1. View (template file name, array) The array is a collection of variables that need to be allocated. The array is an array of key values, and the keys and variable names are as consistent as possible.
2. view(name of template file) -> with(array)
3. view(name of template file) -> withd(name, value) -> with(name, value)...

After rendering a view using the view() method, in the view file of blade.php, use the output variables in the template{ {$变量名}}

Example: Need to transfer the time (year, month, day, minute, second) data of the method in the controller to the view.
Insert picture description here
Insert picture description here
Effect:
Insert picture description here
Format the timestamp:
Insert picture description here
Effect:
Insert picture description here


4. Extension: the use of compact function (passing parameters)

The compact function is a built-in function of php which has nothing to do with the laravel framework. The role is mainly used to pack the array.
Syntax: compact('variable name 1','variable name 2',...);
Insert picture description here
effect:
Insert picture description here
Insert picture description here


5. Loop and branch syntax tags

To traverse the data in the view To
cyclically output data in the template in laravel, you need to follow the syntax:

How to write php:
foreach($variable as $key => $value){}

The writing of the view in laravel:
@foreach($variable as $key => $value)
@endforeach

Example:
Insert picture description here
Effect:
Insert picture description here


6, the judgment label in the view

If syntax in php:

if () {
    
    }
elseif () {
    
    }
elseif () {
    
    }
else {
    
    }

View if statement in laravel:

@if()
@elesif()
@elseif()
@else()
@endif()

Example: It is required to dynamically output today's week number in the php code. Pass the number to the view to show what day of the week is today (convert the number into Chinese characters)
1. First output the current week number in the controller method.
Insert picture description here
2. View
Insert picture description here
effect:
Insert picture description here


7. Template inheritance/include (understanding)

Inheritance not only exists in php classes, it also exists in views. Generally used for pages with public parts.
You can put the head and tail separately in a page (the parent page). The variable area is called a child page. If the child page needs to use something from the parent page, you need to use inheritance.

Inheritance syntax:
Write the following syntax in the child template:
@extends('Template file name to be inherited') If its name is a full path, similar to the view view path, bind the block/component to the parent page through the section tag, and the block name It is the parameter name of the yield tag of the parent page. @section(block name) code @endsection

Question: Can the method in the php parent class be overridden in the child class? (Yes)
Example: Write a parent page (parent class), and then write a child page (child class) and
parent page (parent class):
Insert picture description here


Subpage (subclass):
Insert picture description here
Inheritance and inclusion of templates:
Insert picture description here
Insert picture description here
Effects:
Insert picture description here


The template contains:
Syntax:

@include (template file name) The file name does not contain a suffix, and the syntax is similar to the view method parameter

On the way of learning php, if you think this article is helpful to you, then please pay attention to like and comment Sanlian, thank you, you must be another support for my blog.

Guess you like

Origin blog.csdn.net/weixin_44103733/article/details/113789812