Home >Backend Development >PHP Tutorial >Summary of Smarty template variable operators_PHP tutorial
I haven’t used Smarty for a project for a long time. Today I suddenly have to change some things in my website, because my website was made with the smarty template engine a long time ago. After changing some parts, I feel that it is not enough. I searched the Internet for answers. It seems that I am already unfamiliar with smarty, which I haven’t seen for a long time. I need to catch up on it quickly. Let’s briefly summarize the variable operators of Smarty templates for future use.
Introduction to Smarty template variable operators
capitalize [capitalize the first letter], example: {$smarty.get.name|capitalize}
count_characters [Count the number of characters], example: {$smarty.get.name|count_characters}
cat [connection string], example: {$smarty.get.name|cat:'welcome to www.phpernote.com'}, connect two variables such as: {$smarty.get.name|cat:$ smarty.get.password}
count_paragraphs [Count number of paragraphs], example: {$smarty.get.name|count_characters}
count_sentences [Count number of sentences], example: {$smarty.get.name|count_sentences}
count_words [Count the number of words], example: {$smarty.get.name|count_words}
date_format [time format], example: {$smarty.server.request_time|date_format:"%Y-%m-%d %H:%M:%S"} or {$smarty.now|date_format:" %Y-%m-%d %H:%M:%S"}, the display format is as follows: 2012-7-31 11:01:21
default [default], example: {$smarty.get.name|default:'phpernote.com'}, which means that when the name parameter is not received or the received name parameter value is empty, phpernote is used. com instead of this value.
escape [transcoding], example: {$smarty.get.name|escape:'url'}?
indent [indent], example: {$smarty.get.name|indent:10:' '}, the first line of the paragraph is indented. The example here is to indent the characters represented by ten spaces. The function is similar to text-indent in CSS.
lower [lowercase], example: {$smarty.get.name|lower}
upper [uppercase], example: {$smarty.get.name|upper}
nl2br [Replace n (line break) in the variable with
], example: {$smarty.get.name|nl2br}
regex_replace [Regular replacement], example: {$smarty.get.name|replace:"/[rtn]/":"phpernote.com"}, replace the regular content in the variable with the specified content
replace [replace], example: {$smarty.get.name|replace:"baidu.com":"phpernote.com"}, replace the required content in the variable with the specified content, the example means to replace the variable Replace all baidu.com with phpernote.com.
spacify [insert blank], example: {$smarty.get.name|spacify:"^^"}, inserts specified content, including spaces, between variable characters.
string_format [String formatting], example: {$smarty.get.namestring_format:"%d"}, this operation is to format the content in the variable, and the formatting parameters are similar to PHP's printf.
strip [remove (excess spaces)], example: {$smarty.get.name|strip:" "}, if the first parameter exists, replace the spaces with the specified content, the meaning of the example Replace all spaces with " ";
strip_tags [Remove html tags], example: {$smarty.get.name|strip_tags}
truncate [Interception], example: {$smarty.get.name|truncate:10:'...'}, the example means to intercept 10 characters, and replace the excess with "...", No. If the two parameters are empty, the excess part will be omitted directly.
wordwrap [line width constraint], example: {$smarty.get.name|wordwrap:30:"n":true}, force content that exceeds the specified length to wrap
That’s it for today. It will be slowly added and improved in the future. For more specific knowledge of Smarty template variable operators, you can refer to the Smarty manual!