Smarty Basic Summary

Source: Internet
Author: User
Tags smarty template

Prerequisites:1.Deploy the Smarty template directory;2.write subclass of Smarty class, customize Good Template_dir, Compile_dir, Config_dir, Cache_dir, Left_delimiter, Right_delimiter, Compile_check , caching and other configuration information. 3.the class object is defined in the Baseaction class and is then available for use. 4.tentative delimiter using {and}.variables1.PHP variable A. Normal variable: {$var} B. Associative array: {$array. var1.VAR2} C. Array of numbers: {$array[0] [1]} D. Object: {$object-var} E. In quotation marks, use the associative array as an example, other similar: {function var="{$array. var1.var2} "}2.conf variable A. {#var#}B. How to use inside quotation marks: {function var= "{#var #}"}3.smarty reserved variable: slightly. Two.variable Adjuster1. Capitalize/lower/Upper use: Capitalize first letter/Convert to lowercase/To capitalize usage: {$var|capitalize}/{$var|lower}/{$var|Upper}2. Count_characters/count_paragraphs/count_sentences/Count_words uses: Calculates the number of bytes, default does not calculate the whitespace/count the number of paragraphs/count the number of sentences/To calculate the number of words to use: {$var|count_characters} {$var|count_characters:true} / {$article|count_paragraphs}/{$article|count_sentences}/{$sentence|Count_words}3.Cat Use: String connection usage: {$var|cat: "is a boy."}4.NL2BRPurpose: Replace the line break with<br/>usage: {$var|NL2BR}5. Regex_replace/Replace use: Regular replacement/Common substitution usage: {$var|regex_replace: "/\[old\]/": "[New]"}/{$var|replace: "Old": "New"}6.spacify Use: Inserts the specified character usage in each character putting: {$var|spacify: "^_^"}7.date_format use: formatted date, similar"-1 days Ago", "201111010000", timestamps, and so on, similar to the strftime () function: {$time|date_format: "%h:%m:%s"}8.defaultPurpose: When the variable is empty, set the default value usage: {$var|default: "No value"}9.Escape uses: transcoding, including: HTML, Htmlall,url,quotes,hex,hexentity,JavaScript usage: {$articleTitle|escape: "HTML"}10.Indent use: Text indent, you can set the number of indent characters and use what characters instead of usage: {$var|indent} {$var|indent:4} {$var|indent:4: "\ T"}11.String_format use: Similar with sprintf usage: {$var|string_format: "%.2f"}12.strip/Strip_tagsPurpose: Remove extra whitespace to specify the character to be removed/Removal <>and all the character usages that are contained inside: {$var|strip} {$var|strip: "&nbsp;"} / {$var|Strip_tags}13.truncate use: string interception, the default interception of 80 characters, you can specify the appended string usage: {$var|TRUNCATE:40} {$var|TRUNCATE:40: "...":true}14.combination modifier uses: As the name implies, can be used in combination of multiple variable regulators, intermediate|to replace the example: {$articleTitle|lower|spacify|truncate:30: "..."} three.built-in functions1.Capture Purpose: captures the template content to a variable Var, and does not perform output usage: {capture}{The data in the middle of the/capture} is captured and can be used$smarty. Capture.var to use, without specifying name, defaults to default {capture name=Banner} This is a test.   {/Capture}2.foreachPurpose: Cyclic processing usage: from:array, required with $ item:cell element name, no need for $Key:key name, not required with $ name: The name of the loop that can be used to access the loop, for example: {$smarty.foreach. Foreachname.VarName} {foreachItem=contact from=$contacts}   {foreach Key=KeyItem=item from=$contact}   {$key}: {$item}<br>   {/foreach}   {/foreachNote: There are some special variables in foreach that you need to use {$smarty.foreach. foreachname.***} visit: Iteration:represents the number of executions of the current loop, initially 1 first:the loop is set to true last when it is first executed:Ibid . Total:used to show the number of times a loop is executed, either in a loop or after a loop:is a label for foreach that determines whether to display the contents of the foreach3.include/Insert Purpose: include other templates/Unlike include, insert contains content that is not cached, and the function is re-executed each time the template is called.usage: {include file= "Footer.tpl" title= "Main Menu" logo= "Http://my.domain.com/logo.gif"}4.if ElseIf Elseusage: per cent judgment usage: {if $nameEQ "Fred"} Welcome Sir.   {ElseIf $nameEQ "Wilma"} Welcome Ma‘Am.   {Else}   Welcome, whatever. {/if} 5. Ldelim/rdelim use: Represents the opening parenthesis, the closing parenthesis, respectively, because these two symbols are used as identifiers for the Smarty template: {Ldelim} {rdelim}6.   Literal use: In-house data as text processing, not using Smarty template parsing, mainly used in JavaScript scripts and other usages: {literal} ... {/literal} 7. Section Purpose: Cyclic processing usage: Name: The loop is called loop: the array that determines the number of loops, note that the use of this method is particularly useful, you can use the section to handle multiple arrays, but you must first use the array to determine the number of loops to loop assignment { Section Name=customer loop= $custid} ID: {$custid [customer]}<br> name: {$name [customer]}<br> address: {$a ddress[customer]}<br> {section name=contact loop= $contact _type[customer]} {$contact _type[customer][contact]} : {$contact _info[customer][contact]}<br> {/section} <p> {/section} If you want to traverse a multidimensional associative array, you need to use this: {section Nam E=customer loop= $contacts} name: {$contacts [customer].name}<br> home: {$contacts [customer].home}<br> cel L: {$contacts [customer].cell}<br> e-mail: {$contacts [customer].email}<p> {/section} Note: Similar to foreach, in sect There are also some special variables available in ion, using the following method: {$smarty. section.sectionname.***} index: Displays the index of the current loop, starting with the default from 0 or start Index_prev: Displays the previous loop index value, starting from 1 by default Index_next: Ditto until the last big 1 Inerat Ion: With foreach, unlike index, the latter is index first: Same as foreach last: Same as foreach show: Same as foreach total: Same as FOREACH8.    Strip purpose: Clears all spaces and carriage returns that are outside the inner HTML tag of {Strip}{/strip} and starts and ends with an HTML tag: {strip} <table border=0> <tr> <td> <a href= "{$url}" > <font color= "Red" >this is A test</font> </A> </td&gt  ; </tr> </table> {/strip} The above output is: <table border=0><tr><td><a href= "Http://my.domain. com "><font color=" Red ">this is a test</font></a></td></tr></table> four. The programmer needs to be aware of the place 1. Constant Smarty_dir:smarty_dir constants are used to locate the full system path of a SMARTY class file, must end with a slash, or be undefined, and the SMARTY template automatically creates the appropriate value. 2. Class Properties $template _dir: Template directory, which is the HTML file placement directory, the default is "./templates" $compile _dir: The compiled template directory, after the Smarty parsed HTML file placement directory, the default is ". Templates_c "$config _dir: Template profile directory, default to"./configs "$plugins _dir: Plug-in directory, default to Smarty_dir. "Plugins "$cache _dir: The directory that holds the template cache, the default is"./cache "$debugging: You can start the debug console, the default is false, very useful AH!!! $debug _TPL: Defines the template file name for the debug console, which defaults to Smarty_dir. "Libs/debug.tpl" $global _assign: Used to define global variables, for example: php: $this->tpl->global_assign = Array (' My_global_1 '  = ...);   In the template: {$smarty. my_global_1.***} $compile _check: Auto-compile template, set to True by default, and set to false for performance after putting the product.   $force _compile: Forcing the template to recompile on each invocation, default to False, is not restricted by $compile_check, and is forced to recompile once set to true.   $caching: If the template output is cached, the default is False, which facilitates performance enhancement. $caching _lifetime: The cache lifetime is valid only when $caching is true, 1 is always valid, and 0 means that regeneration is always required.   Unit is seconds. Note: The relationship between $compile _check, $force _compile, $caching is as follows: A. If you set up $compile_check, if any template files or configuration files are updated, the cache will be rebuilt, and B. If you set the $force _compile, the $compile_check does not work, and the cache is always regenerated; C. If no $caching is set, there is no cache and performance is affected. Five. Smarty's method1. Assign: The PHP variable used by the template is assigned a value of 2. Assign_by_ref: is also an assignment, but a reference is assigned a value of 3. Clear_all_assign: Clears all assignment 4. Clear_all_cache: Clears all caches, parameters can specify a threshold time of 5. Clear_assign: Clears an assignment, you can specify either a single variable name or an array of 6. Clear_cache: To clear the cache for a template, you need to specify the template name 7. Clear_config: Clears all configuration variables and clears the specific configuration variable 8 if specified. Config_load: Loads a configuration file and outputs the data to template 9. Display: Show a template 10.fetch: Capture the output of a template, unlike display, the template output is not directly displayed (what is the use??? 11.get_config_vars: Gets the value of all configuration variables, or you can specify a specific variable name 12.get_template_vars: Gets the value of all template variables, or you can specify a specific variable name 13.is_cached: Determine if a template's cache exists, only in $cachOnly valid when ING is set to true 14.template_exists: Determine if a template exists 

Smarty Basic Summary

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.