Phalcon template engine (volt) custom filter

Source: Internet
Author: User
Tags php template

Introduction:Today, I encountered a problem where some variables in the template are sometimes not defined. If php enables the e_all error level, a notice will appear, prompting that the variable is not defined.
The idea at the beginning was to use a ternary expression () to set a default value for each variable. Later, it was found that this writing method was too cumbersome and incompatible with the volt label.
Later, I thought of blocking the e_notice report, but it was not appropriate in the development environment.
Finally, we found that the volt engine has a filter filters function to solve this problem. The statement is as follows: {var | default ('str')}. When $ VaR is empty, this field will be set to str. Of course, STR can be a null string.
This article explores what filters are and how to customize filters.

First, what is template rendering?
How is the template rendered when the volt template engine is used? As we all know, after the Controller method is executed, the framework will automatically render the view. At this time, because you use the volt template engine, the unique set of tag syntax in it is not the PHP syntax, therefore, the view cannot directly include the template file and can be interpreted as a PHP script. At this time, the view will do one thing. It will first Replace the tag in the template code with the PHP code and save it to the template cache file, the cache location is set by yourself when you configure the view in Di. Each cache file corresponds to a template file, and the cached file will be directly obtained during the second rendering of the view, this avoids parsing volt labels every time, so there is no performance gap with the native PHP template. In some cases, it may be better (when layout and template inheritance, multiple layers of templates will be merged into one, reduces the number of file reads ).

Second, the tag 'prototype'
{Var | default ('str')} Why is the conversion? We can observeTemplate CacheAs you can see, this label is probably converted

<?php echo (empty($var) ? (‘str‘) : ($var)); ?>

Third, understand MPs queue '|'
If you are familiar with shell, the tag function is to pass the $ VaR value to the default filter for processing. Other parameters can be added to the filter, you only need to add parameters as long as you call functions, as shown in the preceding ('str ');The variable type label is eventually converted to the echo statement.

Fourth, custom filters
You can see from the official manual that when registering the volt template engine in Di, you can add a custom filter method. First read the following code:

// Volt template engine service $ di-> set ('voltservice', function ($ view, $ di) {$ volt = new \ Phalcon \ MVC \ view \ engine \ volt ($ view, $ di); $ Volt-> setoptions (Array ("compiledpath" => app_path. "/runtime/volt/", "compiledextension" => ". compiled "); // custom filter $ compiler = $ Volt-> getcompiler (); $ compiler-> addfilter ('int', function ($ resolvedargs, $ exprargs) {return 'intval ('. $ resolvedargs. ')' ;}); return $ volt ;});

First, we get the "compiler object" $ compiler through getcompiler, and add a custom filter through addfilter in $ compiler. The above Code adds a filter called "int, the function is to convert the value of $ var into an integer.

Fifth, understanding
When parsing a volt template, if there is a tag {var | int}, $ compiler will check whether there is an int filter in the object. Obviously, there are some, then the anonymous function will be called, put the returned value string in Echo. The Int filter defined above will return intval ($ Val), and the tag will be replaced

<?php echo intval($val);?>

Then, it is explained by PHP and the HTML code is returned to the browser.
Details: $ resolvedargs?
It is the concatenation of the variable name and the additional parameter. For example, if no additional parameter is added to the int above, the value of the parameter is '$ var'. Note that the value is a literal string and cannot be used as a variable.
Suppose we have defined a filter trim, and we want the result of {var | trim ('%')} to be the value of trim ($ Val, '%, this is what we need to write when defining an anonymous filter function.

$compiler->addFilter(‘concat‘, function($resolvedArgs, $exprArgs) {    return ‘trim(‘.$resolvedArgs.‘)‘;});return $volt;

That is, the value of $ resolvedargs is:$ Val, '%'Note: Here is the original literal string. For convenience, the single quotation marks on both sides are omitted, and the $ var variable name is always at the beginning.
The final tag is replaced with the PHP code:<?php echo trim($val,‘%‘);?>
So what is $ exprargs? It is an array that stores the type, literal string, and script of each parameter. It is generally not used, but may be used in special cases, such as the built-in default filter, when the default ('') parameter is specifiedEmpty ($ var )? '': $ VaRHere we need to get the information of each parameter from $ exprargs.

The same is true for custom template functions. The purpose is to convert tags into PHP code and save them to the template cache.

Comments:
The template engine is not mysterious. As long as you understand its working mechanism and code implementation principle, everyone can implement their own template engine.

Phalcon template engine (volt) custom filter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.