Common smarty variable operators

Source: Internet
Author: User
Tags php template
  1. The variable operator of the PHP template engine smarty can be used to operate variables, user-defined functions, and characters.
    The syntax uses "|" to apply the variable operator. for multiple parameters, use ":"?? Refers to memory? /Div>
    Capitalize [uppercase letters]
    Count_characters [count characters]
    Cat [connection string]
    Count_paragraphs [calculate the number of paragraphs]
    Count_sentences [calculate the number of periods]
    Count_words [count words]
    Date_format [time format]
    Default [Default]
    Escape [transcoding]
    Indent [indent]
    Lower [lower case]
    Nl2br [Replace line breaks with <br/>]
    Regex_replace [Regular Expression replacement]
    Replace [Replace]
    Spacify [Blank]
    String_format [String formatting]
    Strip [remove (extra space)]
    Strip_tags [remove HTML tags]
    Truncate [truncate]
    Upper [uppercase]
    Wordwrap [row width constraints]
    Use multiple operators in combination
    Instance:
    {* Uppercase title *}
    <H2 >{$ title | Upper} </H2>

    {* Take the first 40 characters *}
    Topic: {$ topic | truncate: 40 :"..."}

    {* Format text string *}
    {"Now" | date_format: "% Y/% m/% d "}

    {* Apply the regulator in a custom function *}
    {Mailto | Upper address = "main@cn-web.com "}
    Capitalize (uppercase)
    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', 'police begin campaign to rundown jaycyclers .');
    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}
    {$ Articletitle | capitalize}

    Output:

    Police begin campaign to rundown jaywalkers.
    Police begin campaign to rundown jaywalkers.


    Count_characters (count the characters in the variable)

    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', 'cold wave linked to temperatures .');
    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}
    {$ Articletitle | count_characters}

    Output:

    Cold Wave linked to temperatures.
    32
    CAT (connection string)
    Connect the value in cat to the end of the given variable
    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', 'sychics predict world did' t end ');
    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle | Cat: "Yesterday ."}

    Output:

    Psychics predict world didn't end yesterday.

    Count_paragraphs (number of calculated segments)
    Calculates the number of paragraphs in a variable.
    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', 'War dims hope for peace. Child's death ruins couple's holiday .');
    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}
    {$ Articletitle | count_paragraphs}

    Output:

    War dims hope for peace. Child's death ruins couple's holiday.

    Man is fatally slain. Death Causes loneliness, feeling of isolation.
    2

    Count_sentences (calculate the number of periods)
    Calculates the number of sentences in a variable.
    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', 'two Soviet ships collide-one dies. Enraged cow injures farmer with axe .');
    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}
    {$ Articletitle | count_sentences}

    Output:

    Two Soviet ships collide-one dies. Enraged cow injures farmer with axe.
    2
    Count_words)
    Calculate the number of words in a variable
    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', 'dealers' will hear car talk at noon .');
    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}
    {$ Articletitle | count_words}

    Output:

    Dealers will hear car talk at noon.
    7
    Date_format (Date Format)
    Parameter position
    Parameter location type required default description
    1 string no % B % E, % Y this is the format for the outputted date.
    Output string format
    2 string no N/A This is the default date if the input is empty.
    Default setting when the input is null

    Format the date and time in the given function serftime.
    Timestamp (parsable by strtotime) of Unix or MySQL can be passed to smarty.
    The designer can use date_format to completely control the date format.
    If the data passed to date_format is empty, the second parameter is used as the time format.
    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('yesterday', strtotime ('-1 Day '));
    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Smarty. Now | date_format}
    {$ Smarty. Now | date_format: "% A, % B % E, % Y "}
    {$ Smarty. Now | date_format: "% H: % m: % s "}
    {$ Yesterday | date_format}
    {$ Yesterday | date_format: "% A, % B % E, % Y "}
    {$ Yesterday | date_format: "% H: % m: % s "}

    Output:

    Feb 6, 2001
    Tuesday, February 6, 2001
    14:33:00
    Feb 5, 2001
    Monday, February 5, 2001
    14:33:00

    Default)
    Parameter position type required default description
    1 string no empty this is the default value to output if the variable is empty.
    This is the default output when the variable is null.

    Set a default value for null variables.
    When the variable is null or unallocated, the output is replaced by the given default value.
    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', 'dealers' will hear car talk at noon .');
    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle | default: "No title "}
    {$ Mytitle | default: "No title "}

    Output:

    Dealers will hear car talk at noon.
    No title

    Escape (transcoding)
    Parameter position type required possible values default description
    1 string no HTML, htmlall, URL, quotes, Hex, hexentity, JavaScript HTML this is the escape format to use.
    It is used for HTML transcoding and URL transcoding. It is used to convert single quotes, hexadecimal transcoding, hexadecimal beautification, or Javascript transcoding on variables without transcoding.
    The default format is HTML transcoding.
    Index. php:

     

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', "'stiff opposition expected to casketless funeral plan '");

    $ Smarty-> display ('index. TPL ');

     

    Index. TPL:

     

    {$ Articletitle}

    {$ Articletitle | escape}

    {$ Articletitle | escape: "html"} {* escapes & "'<> *}

    {$ Articletitle | escape: "htmlall"} {* escapes all HTML entities *}

    {$ Articletitle | escape: "url "}

    {$ Articletitle | escape: "quotes "}

    <

    Href = "{$ emailaddress | escape:" hexentity "} mailto: {$ emailaddress | escape:" hex "}" >{$ emailaddress | escape: "hexentity"} </a>

     

    Output:

     

    'Stiff opposition expected to casketless funeral plan'

    'Stiff % 20 opposition % 20 expected % 20to % 20 casketless % 20 funeral % 20plan'

    'Stiff % 20 opposition % 20 expected % 20to % 20 casketless % 20 funeral % 20plan'

    'Stiff % 20 opposition % 20 expected % 20to % 20 casketless % 20 funeral % 20plan'

    'Stiff + opposition + expected + to + casketless + funeral + plan'

    'Stiff opposition expected to casketless funeral plan'

    <

    Href = "bob@me.netmailto: % 62% 6f % 62% 40% 6D % 65% 2e % 6e % 65% 74"> & # x62; & # x6f; & # x62; & # X40; & # x6d; & # X65; & # x2e; & # x6e; & # X65; & # x74; </a>

    Indent)
    Parameter position type required default description
    1 integer No 4 This determines how character characters to indent.
    2 string no (one space) This is the character used to indent.

    Indent the string in each line. The default value is 4 characters (the pear standard is also used ).
    As an optional parameter, you can specify the number of indentations.
    As the second optional parameter, you can specify the characters used to replace indentation.
    Index. php:

     

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', 'nj judge to rule on nude beach .');

    $ Smarty-> display ('index. TPL ');

     

    Index. TPL:

     

    {$ Articletitle}

     

    {$ Articletitle | indent}

     

    {$ Articletitle | indent: 10}

     

    {$ Articletitle | indent: 1: "T "}

     

    Output:

     

    NJ judge to rule on nude beach.

    Sun or rain expected today, dark tonight.

    Statistics show that teen pregnancy drops off significantly after 25.

     

    NJ judge to rule on nude beach.

    Sun or rain expected today, dark tonight.

    Statistics show that teen pregnancy drops off significantly after 25.

    NJ judge to rule on nude beach.

    Sun or rain expected today, dark tonight.

    Statistics show that teen pregnancy drops off significantly after 25.

     

    NJ judge to rule on nude beach.

    Sun or rain expected today, dark tonight.

    Statistics show that teen pregnancy drops off significantly after 25.

    Lower (lower case)
    Lowercase variable string
    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', 'two convicts evade noose, jury hung .');

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}

    {$ Articletitle | Lower}

    Output:

    Two Convicts evade noose, jury hung.

    Two Convicts evade noose, jury hung.

    Nl2br (replace line breaks with <br/>)
    All linefeeds will be replaced with <br/>. The same as the nl2br () function in PHP.
    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', "Sun or rain expectedntoday, dark tonight ");

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle | nl2br}

    Output:

    Sun or rain expected <br/> today, dark tonight

    Regex_replace (Regular Expression replacement)
    Find and replace regular expressions.
    Parameter position type required default description
    1 string Yes N/A This is the regular expression to be replaced.
    Replace the regular expression.

    2 string Yes N/A This is the string of text to replace.
    Used text string to replace

    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', "infertility unlikely tonbe passed on, experts say .");

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {* Replace each carriage return, Tab & New Line with a space *} {* replace each carriage return, tab, and line break with Spaces *}

    {$ Articletitle}

    {$ Articletitle | regex_replace: "/[RTN]/": ""}

    Output:

    Infertility unlikely

    Be passed on, experts say.

    Infertility unlikely to be passed on, experts say.

    Replace)
    Simple search and replacement of strings
    Parameter position type required default description
    1 string Yes N/A This is the string of text to be replaced.
    String to be replaced
    2 string Yes N/A This is the string of text to replace.
    Text to replace

    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', "Child's stool great for use in garden .");

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}

    {$ Articletitle | Replace: "Garden": "vineyard "}

    {$ Articletitle | Replace :"":""}

    Output:

    Child's stool great for use in garden.

    Child's stool great for use in vineyard.

    Child's stool great for use in garden.

    Spacify
    Is to insert spaces or other characters (strings) between each character of a string ).
    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', 'something went wrong in jet crash, experts say .');

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}

    {$ Articletitle | spacify}

    {$ Articletitle | spacify: "^ "}

    Output:

    Something went wrong in jet crash, experts say.

    S o m e t h I n g w e n t w R o n g I n J E T C R A S H, e x P E R T s a Y.

    S ^ o ^ m ^ e ^ t ^ H ^ I ^ n ^ G ^ w ^ e ^ n ^ t ^ ^ w ^ r ^ o ^ n ^ G ^ I ^ n ^ J ^ e ^ t ^ C ^ r ^ A ^ s ^ H ^, ^ e ^ x ^ P ^ e ^ r ^ t ^ s ^ A ^ y ^.

    String_format (string formatting)
    Parameter position type required default description
    1 string Yes N/A This is what format to use. (sprintf)
    Format used

    It is a method for formatting floating-point numbers, such as decimal numbers. It is formatted using the sprintf syntax.

    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('number', 23.5787446 );

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Number}

    {$ Number | string_format: "%. 2f "}

    {$ Number | string_format: "% d "}

    Output:

    23.5787446

    23.58

    24

    Strip (remove (extra space)
    Replace all repeated spaces. The line feed and tab are single.
    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', "grandmother ofneight makest Hole in one .");

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}

    {$ Articletitle | strip}

    {$ Articletitle | Strip: "& nbsp ;"}

    Output:

    Grandmother

    Eight makes hole in one.

    Grandmother of eight makes hole in one.

    Grandmother & nbsp; of & nbsp; eight & nbsp; makes & nbsp; hole & nbsp; In & nbsp; one.

    Strip_tags (remove HTML tags)
    Remove all labels between <and>, including <and>.
    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle ', "Blind Woman gets <font face =" Helvetica "> New Kidney </font> from dad she hasn't seen in <B> years </B>. ");

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}

    {$ Articletitle | strip_tags}

    Output:

    Blind woman gets <font face = "Helvetica"> New Kidney </font> from dad she hasn't seen in <B> years </B>.

    Blind woman gets new kidney from dad she hasn't seen in years.

    Truncate)
    Parameter position type required default description
    1 integer No 80 This determines how character characters to truncate.
    Specifies the number of characters to intercept.
    2 string no... This is the text to append if truncation occurs.
    Truncates the string followed by the intercepted word.
    3 Boolean no false this determines whether or not to truncate at a word boundary (false), or at the exact character (true ).
    Check whether the word boundary is intercepted

    The first part of the string to be truncated. The default value is 80.
    You can specify the second parameter as the character after the string to be intercepted.
    By default, smarty captures the end of a word,
    If you want to accurately intercept the number of characters, change the third parameter to "true"

    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', 'two sisters reunite after eighteen years at checkout counter .');

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}

    {$ Articletitle | truncate}

    {$ Articletitle | truncate: 30}

    {$ Articletitle | truncate: 30 :""}

    {$ Articletitle | truncate: 30 :"---"}

    {$ Articletitle | truncate: 30: "": true}

    {$ Articletitle | truncate: 30: "...": true}

    Output:

    Two sisters reunite after eighteen years at checkout counter.

    Two sisters reunite after eighteen years at checkout counter.

    Two sisters reunite after...

    Two sisters reunite after

    Two sisters reunite after ---

    Two sisters reunite after Eigh

    Two sisters reunite after e...

    Upper (uppercase)
    Change the variable to uppercase
    Index. php:

    $ Smarty = new smarty;

    $ Smarty-> assign ('articletitle', "If strike isn' t settled quickly it may last a while .");

    $ Smarty-> display ('index. TPL ');

    Index. TPL:

    {$ Articletitle}

    {$ Articletitle | Upper}

    Output:

    If strike isn't settled quickly it may last a while.

    If strike isn't settled quickly it may last a while.

    Wordwrap (row width constraint)
    You can specify the width of a paragraph (that is, the number of characters in a row, exceeding the number of characters in a line). The default value is 80.
    The second parameter is optional. You can specify the characters used at the constraint point (the default is the linefeed N ).
    By default, smarty captures the end of a word. You can specify the exact number of characters to intercept.
    Parameter position type required default description
    1 integer No 80 This determines how many columns to wrap.
    Specifies the width of a paragraph (sentence ).
    2 string no n this is the string used to wrap words.
    What character constraints are used
    3 Boolean no false this determines whether or not to wrap at a word boundary (false), or at the exact character (true ).
    Exact constraints to characters

    Index. php:
    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', "blind woman gets new kidney from Dad She hasn' t seen in years .");
    $ Smarty-> display ('index. TPL ');
    Index. TPL:
    {$ Articletitle}
    {$ Articletitle | wordwrap: 30}
    {$ Articletitle | wordwrap: 20}
    {$ Articletitle | wordwrap: 30: "<br> N "}
    {$ Articletitle | wordwrap: 30: "N": true}
    Output:
    Blind woman gets new kidney from dad she hasn't seen in years.
    Blind woman gets new kidney
    From Dad She hasn' t seen in
    Years.
    Blind woman gets new
    Kidney from dad she
    Hasn' t seen in
    Years.
    Blind woman gets new kidney <br>
    From Dad she hasn't seen in years.
    Blind woman gets new kidney fr
    Om dad she hasn't seen in Year
    S.

    Use multiple operators in combination
    You can apply operators to a variable. They are combined from left to right. Multiple operators must be separated by the "|" symbol.
    Index. php:

    $ Smarty = new smarty;
    $ Smarty-> assign ('articletitle', 'smokers are productive, but death cuts efficiency .');
    $ Smarty-> display ('index. TPL ');

    Index. TPL:
    {$ Articletitle}
    {$ Articletitle | Upper | spacify}
    {$ Articletitle | Lower | spacify | truncate}
    {$ Articletitle | Lower | truncate: 30 | spacify}
    {$ Articletitle | Lower | spacify | truncate: 30 :"..."}

    Output:

    Smokers are productive, but death cuts efficiency.
    S m o k e r s a r e p r o d u C t I V E, B u t d e a t h c u t s e f I c I e n C Y.
    S m o k e r s a r e p r o d u C t I v e, B U T d e a t h c u t s...
    S m o k e r s a r e p r o d u C t I v e, B u t...
    S m o k e r s a r e p...

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.