1. using smarty
1.1 Project Introduction
3, connect the database, extract the relevant data
$title = "Smarty template engine";
$content = "Smarty template engine is one of the most used template engines in the industry today";
4, instantiating the Smarty object
$smarty = new Smarty;
5. Change Smarty Related default properties
6, assigning variables to template files by assign method
$smarty->assign (' title ', $title);
$smarty->assign (' content ', $content);
7. Replace the special tags in the template with the display method and output the template
$smarty->display (' Index1.tpl ');
1.2 Note Whether to change the default properties of the Smarty object
The default delimiter for the Smarty is {}, which can be modified in fact! How to modify?
You can modify the two properties of the Smarty:
Left_delimiter: Left delimiter, default = {
Right_dilimiter: Right delimiter, default =}
1.3 If you need to change the name of the default directory
In Smarty, the name of the core directory cannot be changed casually! For example, the name of the template directory defaults to templates,
But can also be modified!
In smarty2.0, you can make changes with the following four properties:
Template_dir: Template directory, default is for templates
Compile_dir: Compile directory, default is Templates_c
Config_dir: Configuration directory, default to Configs
Cache_dir: Cache directory, defaults to cache
2. Template notes Note: Server-side comments are not displayed in the browser's source code!
Basic syntax:
{* Comment content *}
3. Variable Adjuster
The basic syntax is:
such as: $str 4 = "hello,world!";
$smarty->assign (' Str4 ', $str 4);
Cat
{$ variable name 1|cat:$ variable name 2|cat:$ variable name 3}
Cat variable adjuster: {$str 1|cat: $str 2|cat: $str 3}
Date_format
{$ variable name (timestamp) |date_format: '%y-%m-%d%h:%m:%s '}
Date_format variable regulator: {$time |date_format: '%y-%m-%d%h:%m:%s '}
default
{$ variable name |default: value}
Default variable adjuster: {$personal _sign|default: ' This guy is lazy '}
Lower and Upper
{$ variable name |lower}
{$ variable name |upper}
Lower variable regulator: {$str 4|lower}
Upper variable regulator: {$str 4|upper}
Escape
{$ variable name |escape}
Escape variable adjuster: {$str 5|escape}
Strip_tags
{$ variable name |strip_tags}
strip_tags variable regulator: {$str 5|strip_tags}
NL2BR
{$ variable name | nl2br}
NL2BR variable regulator: {$str 6|NL2BR}
Replace
{$ variable name | Replace: String 1: String 2}
Replace variable adjuster: {$str 7|replace: ' League of Legends ': ' Masturbate and masturbate '}
String_format
{$ variable name | String_format: '%d '}
String_format variable regulator: {$price |string_format: '%d '}
truncate
{$ variable name |truncate: string length: ' ... '}
truncate variable regulator: {$introduce |truncate:900: ' ... ' |upper|nl2br}
Smarty Template engine