Smarty Template Use Notes summary

Source: Internet
Author: User
Tags foreach array length string format smarty template

Recently used in the project to Smarty, first summed up a few commonly used functions, in case of a rainy days.

1, Smarty cycle count:


foreach (array_expression as $key => $value)
From <=> array_expression;item <=> $value key <=> $key.
The Name property can be arbitrarily specified (a combination of letters, numbers, and underscores).
foreach can be nested, but it must be guaranteed that the foreach name in the nesting is unique.
The From property (usually an array) determines the number of loops.
The Foreachelse statement is executed when the From property has no value. (The From property specifies a null value, you can specify with the Foreachelse statement--otherwise--what?)
The Foreach loop has its own variable name that you can use to access the loop. Use the method {$smarty. Foreach.foreachname.varname}, where Foreachname is the Name property specified in foreach.

Example

{foreach from= $variable key=key name=name Iteam=value}
{$variable | @count}//Get array length
{$smarty. Foreach.loop.iindex}//Get subscript for the current loop array element, starting at 0
{$smarty. foreach.loop.iteration}//Get the current number of loops, starting at 1
{$smarty. Foreach.loop.first} is true, the token loop executes for the first time
{$smarty. Foreach.loop.last} is true, the token loop is last executed
{$smarty. Foreach.name.last} is true, the token loop is last executed
{$smarty. Foreach.loop.show}//is currently displayed
{$smarty. Foreach.loop.total}//Cycle times
{/foreach}

2, smarty format Date:

{$smarty. Now|date_format: "%y-%m-%d%h:%m:%s"}

3.smarty template Variable

Capitalize [First letter], example: {$smarty. get.name|capitalize}

Count_characters [Count characters], example: {$smarty. get.name|count_characters}

Cat [Connection string], example: {$smarty. Get.name|cat: ' Welcome to Www.111cn.net '}, connect two variables such as: {$smarty. get.name|cat:$ Smarty.get.password}

count_paragraphs [calculation of paragraphs], example: {$smarty. get.name|count_characters}

count_sentences [number of calculated sentences], example: {$smarty. get.name|count_sentences}

count_words [number of words calculated], 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 "}, display format such as: 2012-7-31 11:01:21

default [Defaults], example: {$smarty. Get.name|default: ' 111cn.net '}, which means replacing this value with 111cn.net when the name parameter is not accepted or the value of the name parameter is received is empty.

Escape [transcoding], example: {$smarty. get.name|escape: ' url '}?

indent [indent], example: {$smarty. get.name|indent:10: ' &nbsp; '}, indent the first line of a paragraph, where the example is a character that is indented 10 spaces. function is similar to the text-indent inside the CSS.

lower [lowercase], example: {$smarty. Get.name|lower}

Upper [uppercase], example: {$smarty. Get.name|upper}

NL2BR [replaces the N (newline character) in the variable with <br/&gt], example: {$smarty. GET.NAME|NL2BR}

Regex_replace [Replacement], example: {$smarty. Get.name|replace: "/[rtn]/": "111cn.net"}, replace the contents of the variable with the specified content

Replace [replacement], example: {$smarty. Get.name|replace: "baidu.com": "111cn.net"}, replace the required content in the variable with the specified content, The example means to replace all baidu.com in a variable with 111cn.net.

spacify [interpolation], example: {$smarty. get.name|spacify: "^^"}, insert variable characters and characters between the specified content, including spaces.

String_format [string format], example: {$smarty. Get.namestring_format: "%d"}, which formats the contents of the variable and formats the parameters as PHP's printf.

Strip [Remove (extra space)], example: {$smarty. Get.name|strip: "&nbsp;"}, if the first argument is present, replace the space with the specified content, and the example means to replace the spaces with "&nbsp;";

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, the excess of the part with "..." instead of, the second argument is empty, the outside part of the direct omitted.

wordwrap [row width constraint], example: {$smarty. get.name|wordwrap:30: "n": true} to force a newline over a specified length

This is the list today, later will be gradually supplemented and improved, more specific smarty template variable operator knowledge you can refer to the Smarty manual!

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.