Variable operators and usage in PHP smarty template engine

Source: Internet
Author: User
Tags smarty template

Common 20 variable operators in smarty * use Syntax: {variable name | Operator :}
* Capitalize --- uppercase letters
* Count_characters --- calculates the number of characters
* Cat --- connection string
* Count_paragraphs --- calculate the number of paragraphs
* Count_sentences --- calculate the number of periods
* Count_words --- calculate the number of words
* Date_format --- time format
* Default --- default
* Escape --- Transcoding
* Indent --- indent
* Lower --- lower case
* Nl2br --- Replace the linefeed
* Regex_replace --- Regular Expression replacement
* Replace --- replace
* Spacify --- enter
* String_format --- string formatting
* Strip --- Remove unnecessary Spaces
* Strip_tags --- Remove HTML tags
* Truncate --- truncate
* Upper --- uppercase
* Wordwrap -- constrain the row width

Usage:
Index. phpCopyCodeThe Code is as follows: 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
Raw 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 the 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}
raw time data: {$ d}
use the date_format Variable Function operation: {$ d | date_format: "% Y-% m-% d"}
Use smarty. now call time: {$ smarty. now | date_format: "% Y-% m-% d"}
use the default Variable Function operation: {$ title1 | default: "No such variable"}
function operation with escape variable: {$ title | escape: "html"}
function operation with indent variable: {$ title | indent: 2: ""}
use lower Variable Function operation: {$ title | Lower}
use upper Variable Function operation: {$ title | Upper}
replace Variable Function operation: {$ title | Replace: "is": ""}
Use spacify Variable Function operation: {$ title | spacify: "_"}
Use string_format Variable Function operation: {$ nubmer | string_format: "%. 2f "}
Use strip Variable Function operation: {$ title | Strip:" _ "}
Use strip_tags Variable Function operation: {$ title | strip_tags}
use the truncate Variable Function operation: {$ title | truncate: 30 :"... "}
function operation using the wordwrap variable: {$ title | wordwrap: 10:"
"}

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.