Variable operators and methods of use in PHP smarty template engine

  • 2020-03-31 20:14:11
  • OfStack

Smarty commonly used 20 variable operators * use syntax: {variable name | operator:}
* capitalize -- capitalize the first letter
* count_characters -- counts the number of characters
* cat --- concatenates strings
* count_paragraphs - calculation of the paragraph
* count_sentences -- count the number of sentences
* count_words - counts words
* date_format -- -- time format
* default -- -- default
* escape --- transcoding
* indent -- -- indent
* lower -- -- lower case
* nl2br --- newline replaced with
* regex_replace -- regular replace
* replace --
* spacify -- insert
* string_format --- string format
* strip -- remove excess space
* strip_tags -- remove HTML tags
* truncate -- intercept
* upper -- uppercase
* wordwrap -- constrained row width

Usage:
index.php
 
include("smarty_inc.php"); 

$name = "My name is MaJi,age 22,sex boy.<a href=>aaaaaa</a>."; 
$smarty->assign("title", $name); 
$smarty->assign("row", $row); 
$smarty->assign("d",strtotime("-0")); 
$smarty->assign("nubmer", 342345.736524); 
$smarty->display("index.html"); 
?> 

index.html
Original data: {$title}

After using the capitalize variable operator: {$title|capitalize}
After using the count_characters variable operator: {$title|count_characters}
After using the cat variable operator: {$title|cat:"wwww.baidu.com"}
After using count_paragraphs variable operator: {$title | count_paragraphs}
After using the count_sentences variable function operator: {$title|count_sentences}
After using the count_words variable function: {$title|count_words}
Original time data: {$d}
Date_format: {$d|date_format:"%Y-%m-%d"}
Use smarty.now call time: {$smarty.now|date_format:"%Y-%m-%d"}
Operation using the default variable function: {$title1|default:" no such variable "}
Escape: {$title|escape:" HTML "}
Use indent variable function operation: {$title|indent:2:" "}
Use the lower variable function to operate: {$title|lower}
Operation using upper variable function: {$title|upper}
{$title|replace:"is":"@@"}
Use spacify variable function operation: {$title|spacify:"_"}
Using the string_format variable function: {$nubmer|string_format:"%.2f"}
{$title|strip:"_"}
Using the strip_tags variable function: {$title|strip_tags}
Action with truncate variable function: {$title|truncate:30:"..." }
Wordwrap variable function operation: {$title|wordwrap:10:"< Br>" }

Related articles: