This article mainly introduces to you about the Laravel Learning Tutorial of the View module of the relevant information, the text through the sample code to introduce the view module is very detailed, to everyone's study or work has a certain reference learning value, the need for friends below with the small series to learn together.
Objective
This article is mainly to introduce to you about the Laravel in the view module of the relevant information, share out for everyone to reference the study, the following words do not say, come together to see the detailed introduction bar.
This article is based on the Laravel 5.4 version of the routing module code for analysis and writing;
File structure
The file layout and functions of the view module are as follows:
Approximate flow in view rendering:
1, through the view () method call, the beginning of the rendering of views;
2, first, find the view file;
(1) Loop through the path, if the file name with a namespace (that is: the previous part), the namespace corresponds to the registered path array, otherwise take the global path array (the paths variable in the Illuminate\view\fileviewfinder class);
(2) Combining the current path, filename, suffix (the default order is blade.php, PHP, CSS), to determine whether the file exists;
(3) If the file does not exist, the report exception: the corresponding view file does not exist, if the file exists, then the corresponding engine is called according to the suffix name to parse;
3, if the CSS suffix, using the file engine, the core call method is file_get_contents;
4, if the PHP suffix, using the PHP engine, the core call method is
Ob_start (); include $__path;ob_get_clean ();
5, if the blade.php suffix, the use of blade engine;
This engine will take the initiative to cache processing, if the cache file is not expired, then call the cache file directly, otherwise recompile, and generate the cache file via SHA1 (located in the Storage/framework/views directory);
Blade Engine Compilation
The blade engine compiles files by a large number of regular matches and replacements;
protected $compilers = [ ' Comments ',//comment section ' Extensions ',//EXTENSION section ' statements ',//statement block (@ at the beginning of the instruction) ' Echos ',// output]; protected function Parsetoken ($token) {list ($id, $content) = $token; if ($id = = t_inline_html) { foreach ($this->compilers as $type) { $content = $this->{"compile{$type}"} ($ content); } }}
During parsing, Blade uses the Token_get_all function to get the portion of the view file that is considered HTML (t_inline_html) by the PHP interpreter, and then sequentially comments, Extensions, statements, and The regular substitution of the echos part;
Comments section
The core code is as follows, replacing the code wrapped in the comment symbol "{{----}}" with an empty string;
Preg_replace ("/{{--(. *?) --}}/s ",", $value);
Extended section
The Extend method is used to add a custom processing callback function to Bladecompiler, and the template content is replaced by a custom text match;
The core code is in the Illuminate\view\bladecompiler file, as follows:
Custom text substitution extended array protected $extensions = [];p rotected function compileextensions ($value) {foreach ($this->extensions A S $compiler) { $value = Call_user_func ($compiler, $value, $this);} return $value;}
Directive substitution
This part is to replace the instructions with the framework of @if and the instructions registered by the Directive method.
The directives provided by the framework have the following 10 parts:
View\compilers\concerns\compilesauthorizations: Permission Check
Directives include: @can, @cannot, @elsecan, @elsecannot, @endcan, @endcannot
Concerns\compilescomponents: Related to components, slots
Directives include: @component, @endcomponent, @slot, @endslot
Concerns\compilesconditionals: Related to judgment statement
Directives include: @if, @unless, @else, @elseif, @endif, @endunless, @isset, @endisset, @hassection
Concerns\compilesincludes: Embedding files
Directives include: @each, @include, @includeif, @includewhen
Concerns\compilesinjections: Service Injection
Directives include: @inject
Concerns\compileslayouts: and layout-related
Directives include: @extends, @section, @parent, @yield, @show, @append, @overwrite, @stop, @endsection
Concerns\compilesloops: Related to Loops
Directives include: @forelse, @empty, @endforelse, @endempty, @for, @foreach, @break, @continue, @endfor, @endforeach, @while, @endwhile
Concerns\compilesrawphp: Related to native PHP statements
Directives include: @php, @endphp, @unset
Concerns\compilesstacks: and Stack-related
Directives include: @stack, @push, @endpush, @prepend, @endprepend
Concerns\compilestranslations: Related to localization translation
Directives include: @lang, @endlang, @choice
Echo replacement
The echo output is a regular replacement for {!!!!}, {{}}, {{{}}} three parentheses;
{!! !!} Outputs an escaped character that is used to output a native HTML tag value;
{{}} normal output, support three mesh operator substitution;
{{{}}} output escape character, support three mesh operator substitution;
The trinocular operator substitution means: {{$a?: ' Default '}} (or {{$a or ' default value '}}}) for {{isset ($a)? $a: "Default"}}