There are also some criticisms on Template engines such as Smarty. Critics often complain that the engine features are integrated into a certain degree of logic. After all, the purpose of the template engine is to completely separate the presentation layer from the logic layer, right? Although this is our ideal, it is not necessarily the most feasible solution. For example, if there is no iteration logic, how can I output a MySQL result set in a certain format? It is impossible to get a perfect solution. In the face of this dilemma, Smarty developers integrate some simple but very effective application logic in the engine. This seems to be an ideal compromise because website designers are generally not programmers (and vice versa ).
This section describes the characteristics of Smarty: Variable modifiers, control structures, and statements. First, give a brief introduction to the annotations.
19.5.1 Note
The rest of this chapter uses annotations when necessary. Therefore, it seems necessary to introduce the annotation Syntax of Smarty first. Comments are enclosed between the boundary mark {* and *} and can contain one or more rows. The following is a valid Smarty note:
19.5.2 Variable Modifier
As you can see in Chapter 9th, PHP provides many functions that can process text in any way you can imagine. However, you may want to use such features in the presentation layer-for example, make sure that the author's surname and name are capitalized in the description. To this end, Smarty developers have integrated many unique features in the library. This section describes some interesting features.
Before the introduction, it is necessary to describe a less traditional variable modifier Syntax of Smarty. Of course, the delimiters are used to indicate the requested variable output. Before the output, the variable value to be modified can be followed by a vertical line, followed by a modifier Command, as shown below:
You will see that the modifier is repeatedly used in this section.
1. Initial letter size
The capitalize function converts the first letter of all words in the variable to uppercase. Example:
The article. tpl template contains:
The returned result is as follows:
2. Word Count
The count_words function counts the total number of words in a variable. Example:
The article. tpl template contains:
This will return:
3. format the date
The date_format function is the wrapper of the PHP strftime () function. It can convert any date/time format string that can be parsed by strftime () to a special format. Because the formatting mark has been introduced in the manual and Chapter 12th, it is unnecessary to list it again here. The following is an example:
The article. tpl template contains:
This will return:
4. Assign the default value
When no return value is returned at the application layer, the default function provides a simple way to indicate the default value of a specific variable. For example:
The default. tpl template contains:
This will return:
5. delete a tag
The strip_tags function deletes the token from the variable string. For example:
The striptags. tpl template contains:
This will return:
6. truncate a string
The truncate function truncates a variable string to a specified number of characters. Although the default value is 80 characters, you can change the truncation length by providing an input parameter (as shown in the following example. You can specify a string (optional) and append it to the end of the intercepted string, such as a ellipsis (...). In addition, you can specify to immediately intercept the specified number of characters, or consider the word boundary (if the parameter is TRUE, intercept according to the exact limit, FALSE: truncates the last word boundary after the limit is reached ). For example:
The truncate. tpl template contains:
This will return:
19.5.3 Control Structure
Smarty provides several control structures that can process incoming data by conditions and iteration. This section describes these structures.
1.IfFunction
The if function of Smarty is the same as the if function in PHP. Like PHP, some conditional delimiters can be used, as listed below:
The following is a simple example:
Consider another example. Suppose you want to insert a message by month. The following example uses the condition qualifier, elseif, and else statements to complete this task:
Note: It is optional to enclose conditional statements in parentheses, but this is required in standard PHP code.
2.ForeachFunction
The foreach function serves the same purpose as the commands in PHP statements. But as shown below, the syntax is quite different. It has four parameters, two of which are necessary.
Q from. This required parameter specifies the name of the target array.
Q item. This required parameter determines the name of the current element.
Q key. This optional parameter determines the name of the current key.
Q name. This optional parameter determines the name of the Section. This name is arbitrary and should be set to a descriptive name that you think matches the program.
Consider an example. Assume that you want to process every day of the week cyclically:
The daysofweek. tpl file contains:
The following result is returned:
You can use the key attribute to iteratively process an associated data. Consider the following example:
The daysofweek. tpl template includes:
This will return:
Although foreach functions are very useful, it is absolutely necessary to take some time to learn another function, such as the section function. This function is more powerful. We will introduce it later.
3.ForeachelseFunction
The foreachelse function is used with foreach, which is similar to the default tag used for strings. When the array is empty, the foreachelse tag can generate a candidate result. The following is an example Template Using foreachelse:
Note that foreachelse does not use ending brackets. It is embedded in foreach, which is similar to embedding elseif in the if function.
4.SectionFunction
The operation of the section function is like an improved for/foreach statement. It iteratively processes and outputs data arrays, but its syntax varies greatly. The word "improvement" here refers to the fact that it provides the same loop feature as the for/foreach structure, and also provides many additional options to control loop execution more. These options are supported by function parameters. The following describes the available option parameters through several examples.
Two parameters are required, as shown below.
Q name. Determine the name of the Section. The node name can be any and should be set to any name that can describe the node's purpose.
Q loop. Set the number of iterations. It should be set to the same name as the array variable.
There are also several optional parameters, as shown below.
Q start. Determine the index position at the beginning of the iteration. For example, if the array contains five values and start is set to 3, the iteration starts from index 3 of the array. If a negative value is given, the starting position is determined by subtracting the number from the end of the array.
Q step. Determine the step size to move in the array. By default, this value is 1. For example, setting step 3 will result in iteration in the array index 0, 3, 6, 9, and so on. If you set step to a negative value, the iteration starts from the end of the array.
Q max. Determine the maximum number of iterations.
Q show. Check whether this section is displayed. You can use this parameter for debugging (set to TRUE first), and then set this parameter to FALSE during deployment.
Consider two examples. The first example iteratively processes a simple index array:
The titles. tpl template includes:
This will return:
In this strange syntax, note that the node name must be referenced like the index value in the array. Note that the $ titles variable name has a dual responsibility. It is both a loop indicator and an actual variable reference.
Here is an example of using an associated array:
Section2.tpl Templates include:
This will return: