Detailed description of the View module of Laravel learning tutorial, laravelview
Preface
This article mainly introduces you to the relevant information about the View module in Laravel and shares it for your reference. I will not talk about it much below. Let's take a look at the detailed introduction.
This article analyzes and writes the routing module code based on Laravel 5.4;
File structure
Shows the file structure and functions of the View module:
General process for visualized presentation:
1. Call the view () method to start rendering the view;
2. First, find the View File;
(1) traverse the path in sequence. If the file name contains a namespace (that is, the previous part), use the corresponding registered path array of the namespace, otherwise, use the Global Path array (the paths variable in the Illuminate \ View \ FileViewFinder class );
(2) check whether the file exists by combining the current path, file name, and suffix (the default sequence is blade. php, php, and css;
(3) If the file does not exist, an exception is reported: The corresponding view file does not exist; if the file exists, the corresponding engine is called Based on the suffix for parsing;
3. If the css suffix is used, the file engine is used. The core call method is file_get_contents;
4. If the php suffix is used and the php engine is used, the core call method is
ob_start();include $__path;ob_get_clean();
5. If the suffix is blade. php, use the blade Engine;
This engine will take the initiative to perform cache processing. If the cache file has not expired, it will directly call the cache file; otherwise, it will re-compile, and the cache file is generated through sha1 (located in the directory of storage/framework/views );
Blade Engine Compilation
The Blade Engine compiles files through a large number of regular expression matching and replacement;
Protected $ compilers = ['comments', // comment 'extensions', // extend 'statements', // statement block (command starting with @) '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 obtain the part of the view file that is considered as HTML (T_INLINE_HTML) by the PHP interpreter, then, replace Comments, Extensions, Statements, and Echos in sequence;
Comments
The core code is as follows. Replace the code of the "{{--}" Annotation with an empty string;
preg_replace("/{{--(.*?)--}}/s", '', $value);
Extensions
Use the extend method to add a callback function for custom processing to BladeCompiler, and replace the template content with custom text matching;
The core code is in the Illuminate \ View \ BladeCompiler file, as follows:
// Custom text replacement extension array protected $ extensions = []; protected function compileExtensions ($ value) {foreach ($ this-> extensions as $ compiler) {$ value = call_user_func ($ compiler, $ value, $ this);} return $ value ;}
Command replacement
This Part replaces the commands that come with the frameworks like @ if and those registered using the directive method;
The framework provides the following 10 commands:
- View \ Compilers \ Concerns \ CompilesAuthorizations: permission check
Commands include @ can, @ cannot, @ elsecan, @ elsecannot, @ endcan, and @ endcannot.
- Concerns \ CompilesComponents: Related to components and slots
Commands include @ component, @ endcomponent, @ slot, and @ endslot.
- Concerns \ CompilesConditionals: Related to judgment statements
Commands include: @ if, @ unless, @ else, @ elseif, @ endif, @ endunless, @ isset, @ endisset, @ hassection
- Concerns \ CompilesIncludes: Embedded file
Commands include @ each, @ include, @ includeif, and @ includewhen.
- Concerns \ CompilesInjections: Service Injection
Commands include: @ inject
- Concerns \ CompilesLayouts: Related to layout
Commands include @ extends, @ section, @ parent, @ yield, @ show, @ append, @ overwrite, @ stop, and @ endsection.
- Concerns \ CompilesLoops: cycle-related
Commands include: @ forelse, @ empty, @ endforelse, @ endempty, @ for, @ foreach, @ break, @ continue, @ endfor, @ endforeach, @ while, @ endwhile
- Concerns \ CompilesRawPhp: Related to native PHP statements
Commands include @ php, @ endphp, and @ unset.
- Concerns \ CompilesStacks: Stack-related
Commands include @ stack, @ push, @ endpush, @ prepend, and @ endprepend.
- Concerns \ CompilesTranslations: related to local Translation
Commands include @ lang, @ endlang, and @ choice.
Echo replacement
Echo output is {!! !!} , {{}}, And {{}} brackets are replaced with regular expressions;
- {!! !!} Outputs unescaped characters, used to output native values with html tags;
- {} Normal output, support replacement of the Three-object operator;
- {{}}} Outputs escape characters, which can be replaced by the three-object operator;
The replacement of the Three-object operator refers to: {$? : "Default Value" }}( or {$ a or "Default Value" }}) to {isset ($ )? $ A: "Default Value "}}
References
Laravel template engine (Blade) Principle Analysis
Laravel 5.4 document front-end-Blade Template
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.