Smarty Detailed Usage Tutorials (Hanshunping smarty Template Technical notes)

Source: Internet
Author: User

MVC is a development pattern that emphasizes the input, processing, and display of data as forced separation.

Smarty Using Tutorials
1. How to configure our Smarty
After decompression, put the Libs folder in the first level of the site directory, and then create two folders
Templates storing template files
Templates_c storing the compiled file
Then create the initialization file smarty.ini.php

Precautions:
1. Replacement variable identification delimiter general use <{}>
Two ways to change a delimiter:
1. Change Source: Smarty.class.php $left _delimiter Not recommended
2. Dynamic modification:
$Smarty->left_delimiter= "<{";
$Smarty->right_delimiter= "}>";
Written before the display

2.smarty Assigning data
$smarty->assign ("Address", $address);
Used to assign values to the template. You can specify a pair of names/values, or you can specify a union array that contains a name/value.
$smarty->assign ("AA", true); Output 1
$smarty->assign ("AA", false); Output null
Attention:
Allocation <{$arr .num.email}> of two-dimensional associative arrays
Object Assignment <{$dog 1->name}>
The <{of an object's properties are exposed $dog 1->master->name}>
Remove data from configuration file common configuration styles, etc.
Load the configuration file <{config_load file= first. /configs/foo.conf "}>
The data in the configuration file can be used directly in the template file using the <{#Bgcolor #}>

3.smarty template built-in function foreach
One-dimensional array loops out
<{foreach form= $arr item=temp key=k}>
<{$k}>=<{$temp}>
<{/foreach}>
Two-dimensional indexed array loop out
<{foreach form= $arr item=temp key=k}>
<{foreach form= $temp item=val}>
<{$val}>
<{/foreach}>
<{/foreach}>
Cyclic extraction of two-dimensional associative arrays
<{foreach form= $arr item=item1}>
<{foreach form= $item 1 item=item2 key=k}>
<{$k}>=<{$item 2}>
<{/foreach}>
<{/foreach}>

4. Custom Functions
function test ($args) {
$str = "";
for ($i =0; $i < $args [' Times ']; $i + +) {
$str. = "<br/><font color=". $args [' Color ']. "' > "size=". $args [' Size ']. "' > ". $args [' con ']." </font> ";
}
return $str;
}
Required Registration function
$smarty->register_function ("Self", "test");
Invocation form
<{self times= "4" size= "5" con= "Hello" color= "Red"}>

Custom Functions (block registration method)
function Test2 ($args, $con) {
$str = "";
for ($i =0; $i < $args [' Times ']; $i + +) {
$str. = "<br/><font color=". $args [' Color ']. "' > "size=". $args [' Size ']. "' > ". $con." </font> ";
}
return $str;
}
Registering block functions
$smarty->register_block ("block", "test2");
Invocation form
<{block times= "4" size= "5" color= "Red"}>hello<{/block}>

Add custom functions as plug-ins (placed in the Libs/plugins folder)
The file name format must be followed: function. Custom function name. php
Function name format:
Functions Smarty_function_ Custom Function name ($params,& $smarty) {
$str = "";
for ($i =0; $i < $args [' Times ']; $i + +) {
$str. = "<br/><font color=". $args [' Color ']. "' > "size=". $args [' Size ']. "' > ". $args [' con ']." </font> ";
}
return $str;
}
No registration required

Add custom block functions as plug-ins
You must follow the file name format: function. Custom function block name. PHP
Function name format:
Function smarty_block_ Custom Function block name ($params, $con, & $smarty) {
$str = "";
for ($i =0; $i < $args [' Times ']; $i + +) {
$str. = "<br/><font color= '". $args [' Color ']. "' > "size=". $args [' Size ']. "' > ". $con." </font> ";
}
return $str;
}
//No registration required

5.smarty Basic Syntax
Use comments <{* the content in the template *}>
Calling a function in a template file
<{Function Name Property 1 = "Property Value" Property 2 = "Property value" ...} >
A reference variable can be mathematically operated on a template file, but cannot be used to change the order of operations
assigning constants and removing them
Define ("rate", "0.08");
<{$samrty .conts.rate}>
Using a variable adjuster in a template
Basic usage
<{variable/function/string | Variable adjuster name: Parameter 1: Parameter 2}>
The variable adjuster can be customized and given in the form of a plug-in
File name: Modifier. variable name. php
function Smarty_modifier_xxx () {
Code
}
Creating a new variable adjuster requires the first letter of the sentence to uppercase other lowercase
File name: Modifier. variable name. php
function Smarty_modifier_mycapitalize () {
Return Strtoupper (substr ($string, 0,1)). Strtolower (substr ($string, 1));
}
Common variable Operators
Date_formate Time Format
Escape transcoding
NL2BR line break to <br/>
Regex_replace Regular Replacement
String_formate string Formatting
Truncate intercept

6.samrty other built-in functions
Capture captures
Config_load loading variables from a configuration file
Include contains additional templates in the current template
Insert is similar to include, but contains content that is not cached
If ElseIf else
Ldelim Rdelim left and right boundary character
Count counts
Literal in-tag data as text processing
Php
Section Sectionelse

7. Custom Functions
The counter holds the current value at each count. The user can adjust the value by adjusting the interval and direction. You can also decide whether to output the value. If you need to run multiple counters at the same time, you must specify a different name for them. If no name is specified, the template engine uses "default" as the default value.
Cycle
Use a set of values for rotation. This feature makes it easy to alternately output colors or rotate the values in an array in a table.
Html_checkboxes
Creates a group of check buttons based on the given data. The function can specify which elements are selected. You must either specify the values and Ouput properties, or specify options overrides. All outputs are compatible with XHTML.
Html_options
Creates an option group based on the given data. The function can specify which elements are selected. You must either specify the values and Ouput properties, or specify options overrides.
Html_radios
Creates a radio button group based on the given data. The function can specify which element is selected. You must either specify the values and Ouput properties, or specify options overrides. All outputs are compatible with XHTML.
Html_select_date
Used to create a date drop-down menu, which can display any month or day.
Html_select_time
Used to create a time drop-down menu
mailto
Used to create a date drop-down menu
Popup-init,popup
Popup is used to create a JavaScript pop-up window. Need to map into overlib.js
TextFormat
Used to format text

8. Variables (key use)
$config _dir
$template _dir
$compile _dir
$plugins _dir
$caching
$cache _dir
$cache _lifetime
$debugging Start the Debug console

9.smarty caching mechanism
The default is that when a template file is changed, the controller will determine
1. Does the template compile file exist
2. Whether the template file changes time is greater than the compilation file change time
When the result is True, the compilation file is regenerated to render the page again
Disadvantages:
1. Generated compilation file is a PHP Dynamic Data file, affecting the browser read performance
2. The compilation file is not regenerated when the controller file is changed
After modification:
1. The generated compilation file is an HTML static data file, which facilitates the browser to read
2. When the controller file changes, control the rebuild of the compiled file by setting the Cache_lifetime

Local Cache Technology
A template file part of the cache, part of the real-time update
The Insert function is not cached by default

Generate different cache pages based on the ID number
$smarty->display (' template file ', cache ID)
$smarty->clear_all_cache (int expire time);//Clear All caches
As an optional parameter, "Expire Time", you can specify a minimum of seconds, and the cache will be erased.
$smarty->clear_cache (' template file ', cache ID);//clears the cache file for the specified ID

Append is passed as a value, with a copy of the process
Append_by_ref Reference Additions
Assign is the value passed by default
Assign_by_ref is a reference pass

10.smarty Filter
Prefilters Pre-Filter
Run before the controller reads the template file, the function is encapsulated in the display function
Use to delete unwanted content and monitor actions on the template
Postfilters after filter can be used to add Comment version number
Run before the controller output compiles the file
Outputfilters Output Filter
The compiled file is executed before being read by the browser

Smarty Detailed Usage Tutorials (Hanshunping smarty Template Technical notes)

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.