In smarty, how does one modify text and variables? Of course, you can use php functions to process text and allocate it to the template through The assign () method. In fact, smarty provides a variable regulator that can easily process text. The syntax is as follows: {$ va
In smarty, how does one modify text and variables? Of course, you can use php functions to process text and allocate it to the template through The assign () method. In fact, smarty provides a variable regulator that can easily process text. The syntax is as follows:
{$ Var | modifier1 | modifier2 | modifier3 | ...}
Note: 1. no matter how many functions are applied, we should use | to separate them.
Note: 2. the parameters are separated by ":", for example, {$ str | truncate: 40 :"... "} // The length of the truncated string is 40, and... end.
Common variable mediation functions in the Smarty Template
Member method name |
Description |
Capitalize |
Capital the first letter of all words in the variable. the parameter value is boolean. Whether or not a word with a number is capitalized. it is not capitalized by default. |
Count_characters |
Calculates the number of characters in the variable value. The boolean value determines whether to count. Number of spaces. space is not calculated by default. |
Cat |
Connect the parameter values in cat to the end of the given variable. the default value is null. |
Count_paragraphs |
Calculates the number of paragraphs in a variable. |
Count_sentences |
Calculates the number of sentences in a variable. |
Count_words |
Calculate the number of words in a variable |
Date_format |
Date format. The first parameter controls the date format. if it is passed to date_format The data of is null. The second parameter is used as the default time. |
Default |
Set a default value for a null variable. when the variable is empty or not assigned, Replace output with the given default value |
Escape |
For html transcoding and url transcoding, convert single quotes, Hexadecimal transcoding, hexadecimal beautification, or javaScript transcoding. The default format is html transcoding. |
Indent |
In each line of the indent string, the first parameter specifies the number of characters indented, The default value is four characters. The second parameter specifies the characters used to replace indentation. |
Lower |
Lowercase variable string |
Nl2br |
All linefeeds will be replaced The. function is the same as the nl2br () function in PHP. |
Regex_replace |
To search for and replace regular expressions, there must be two parameters. parameter 1 is replaced For a regular expression, parameter 2 is replaced by a text string. |
Replace |
A simple search and replacement string must have two parameters. parameter 1 is String to be replaced. parameter 2 is the text to be replaced. |
Spacify |
Insert spaces or other strings between each character of a string ., The parameter indicates the string to be inserted between two characters. the default value is a space. |
String_format |
Is a method for formatting floating point numbers, such as decimal numbers, using sprintf Syntax format. Parameters are required, and the format is required. % D Indicates an integer. %. 2f indicates two floating point numbers are truncated. |
Strip |
Replace all repeated spaces. line breaks and tabs are single or specified strings. If there is a parameter, it is the specified string |
Strip_tags |
Remove all html tags |
Truncate |
Truncates a certain length of characters starting from a string. the default value is 80. |
Upper |
Change the variable to uppercase |
Php code example:
Capitalize (uppercase)
<{$ A | capitalize}>
Count_characters (character count)
<{$ B | count_characters}>
Cat (connection string)
<{$ C | cat: world}>
Count_paragraphs (number of calculated segments) ---- (linefeed \ n)
<{$ D | count_paragraphs}>
Count_sentences (calculate the number of periods)
<{$ E | count_sentences}>
Count_words)
<{$ F | count_words}>
------ Date_format (format date )-----
<{$ Smarty. now | date_format: "% y-% m-% d % H: % I: % S"}>
--- Default (default )---------
<{$ Biaoti | default: "no biaoti"}>
---- Escape (encoding )-----
<{$ Dz | escape}>
------ Indent (first line indent )-----
<{$ In | indent}>
<{$ In | indent: 1: "\ t"}>
Replace the nl2br line break
<{$ Con | nl2br}>
----------- Regex_replace regular expression replacement ------
<{$ H | regex_replace: "/[\ r \ t \ n]/":"
"}>
Replace)
<{$ H | replace: "hello": "hello"}>
Reminder:
In development, sometimes it cannot meet the requirements, so you can directly write these variable regulators. In fact, the so-called variable regulator is a function. here we create one by ourselves, first, create a php file in the plugins folder. the name must follow its specifications ,.
Modifier. capita. php
Function Smarty_modifier_capita ($ string) {// the function name is also standardized.
Return strtoupper (substr ($ string, 0, 1). strtolower (substr ($ string, 1 ));
}
Template File call:
<{$ A | capita}>