In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to talk to you about the use of smarty tutorials and matters needing attention, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
MVC is a development mode that emphasizes that the input, processing and display of data are forcibly separated.
Smarty usage tutorial
1. How to configure our smarty
After decompressing, put the libs folder in the first level directory of the website, and then create two folders.
Templates stores template files
Templates_c stores compiled files
Then create the initialization file smarty.ini.php
Note:
1. The identification delimiter of the replacement variable is generally used
Two ways to change the delimiter:
1. Change the source code: Smarty.class.php $left_delimiter is not recommended
two。 Dynamic modification:
$Smarty- > left_delimiter= ""
Write before display
2.smarty allocation data
$smarty- > assign ("Address", $address)
Used to assign values to the template. You can specify a pair of names / values, or you can specify an associative array that contains names / values.
$smarty- > assign ("aa", true); / / output 1
$smarty- > assign ("aa", false); / / output empty
Note:
Allocation of two-dimensional associative arrays
Allocation name of the object} >
Master- > name} > exposed by the properties of the object
Extract data from configuration files, common configuration styles, etc.
Load the configuration file first
The data in the configuration file can be used directly in the template file
The built-in function foreach of 3.smarty template
One-dimensional array loop fetch
=
Two-dimensional indexed array loop fetch
Two-dimensional associative array loop fetch
=
4. Custom function
Function test ($args) {
$str= ""
For ($iTun0witteri ". $args ['con']."
}
Return $str
}
/ / the function needs to be registered
$smarty- > register_function ("self", "test")
Calling form
Custom function (block registration mode)
Function test2 ($args,$con) {
$str= ""
For ($iTuno con. ".
}
Return $str
}
/ / register block functions
$smarty- > register_block ("block", "test2")
Calling form
Hello
Add custom functions in the form of plug-ins (placed in the libs/plugins folder)
Must follow the file name format: function. Custom function name. php
Function name format:
Function smarty_function_ custom function name ($params,&$smarty) {
$str= ""
For ($iTun0witteri ". $args ['con']."
}
Return $str
}
/ / No registration is required
Add custom block functions in the form of plug-ins
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 ($iTuno con. ".
}
Return $str
}
/ / No registration is required
Basic syntax of 5.smarty
Use comments in a template
Call a function in a template file
The referenced variables can be mathematically operated in the template file, but the operation order cannot be changed with ().
Assign a constant and take out
Define ("RATE", "0.08")
Use a variable regulator in a template
Basic usage
The variable regulator can be customized and given in the form of a plug-in
File name: modifier. Variable name. php
Function smarty_modifier_XXX () {
/ / Code
}
/ / A new variable regulator requires the first letter of a sentence to be uppercase and lowercase.
File name: modifier. Variable name. php
Function smarty_modifier_mycapitalize () {
Return strtoupper (substr ($string,0,1)) .strtolower (substr ($string,1))
}
Commonly used variable operators
Date_formate time format
Escape transcoding
Replace the nl2br newline character with
Regex_replace regular substitution
String_formate string formatting
Truncate interception
Other built-in functions of 6.samrty
Capture capture
Config_load loads variables from the configuration file
Include contains other templates in the current template
Insert is similar to include, but the content it contains is not cached
If elseif else
Ldelim rdelim left and right boundary character
Count count
Data in literal tags are treated as text
Php
Section sectionelse
7. Custom function
Counter keeps the current value of each count. Users can adjust this value by adjusting 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 different names for them. If no name is specified, the template engine uses "default" as the default.
Cycle
Use a set of values for rotation. This feature makes it easy to alternately output colors in a table or rotate values in an array.
Html_checkboxes
Creates a check button group based on the given data. This function can specify which elements are selected. Either the values and ouput properties must be specified, or the options override must be specified. All outputs are compatible with XHTML.
Html_options
Create an option group based on the given data. This function can specify which elements are selected. Either the values and ouput properties must be specified, or the options override must be specified.
Html_radios
Creates a radio button group based on the given data. This function specifies which element is selected. Either the values and ouput properties must be specified, or the options override must be specified. All outputs are compatible with XHTML.
Html_select_date
Used to create the date drop-down menu, you can display any year, month and day.
Html_select_time
Used to create time drop-down menu
Mailto
Used to create date drop-down menu
Popup-init,popup
Popup is used to create javascript pop-up windows. Need to be mapped into overLib.js
Textformat
Used to format text
8. Variables (focus on)
$config_dir
$template_dir
$compile_dir
$plugins_dir
$caching
$cache_dir
$cache_lifetime
$debugging starts the debugging console
Caching Mechanism of 9.smarty
The default is that when the template file is changed, the controller will judge
1. Whether the template compilation file exists
two。 Is the template file change time greater than the compilation file change time?
The compiled file will only be regenerated when the result is true, thus re-rendering the page.
Disadvantages:
1. The generated compilation file is a PHP dynamic data file, which affects the browser's reading performance.
two。 When the controller file changes, the compiled file is not regenerated
After modification:
1. The generated compilation file is a HTML static data file, which is convenient for browsers to read.
two。 When the controller file changes, the cache_lifetime is set to control the regeneration of the compiled file.
Local caching technology
A template file is partially cached and partly updated in real time.
The insert function is not cached by default
/ / generate different cache pages according to 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 time in seconds, after which all caches will be cleared.
$smarty- > clear_cache ('template file', cache ID); / / clear the cache file of the specified id
The process of passing append by value and having a copy
Append_by_ref reference add
Assign is passed by value by default
Assign_by_ref is reference passing
10.smarty filter
Prefilters pre-filter
Run before the controller reads the template file, and the function is encapsulated in the display function
It is used to delete unwanted content and monitor operations on the template
Filter after Postfilters can be used to add comment version number.
Run before the controller outputs the compiled file
Outputfilters output filter
Execute the compiled file before it is read by the browser
After reading the above, do you have any further understanding of the tutorials and precautions for using smarty? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.