Example of how variables are used in PHP template engine smarty, template Smarty
This article describes how to use variables in PHP template engine smarty. Share to everyone for your reference, as follows:
First, overview:
Smarty is one of the many template engines in PHP, which is a class library written according to PHP.
Advantages of Smarty:
1, optimize the speed of website access;
2, the design of the front page and the separation of programs;
Second, the installation of Smarty
1, need to Smarty official website http://www.smarty.net/download.php Download the latest Smarty version, such as the download version is: Smarty-2.6.18.tar.tar;
2, decompression Smarty-2.6.18.tar.tar compression package, will find a lot of files and folders, in addition to the Libs folder, all other deletions, are no use;
3. When invoking the Smarty template engine, you should first load the libs/smarty.class.php file using PHP's require statement.
Third, the default settings of the Smarty class library
After require the Smarty.class.php file, if you need to set up members in the Smarty class library, there are two ways to do this: one is to modify it directly in the Smarty.class.php file, one to re-specify after the class library is initialized, and the latter for general use. The following is a description of the member properties in the Smarty class library:
1. $template _dir: Set the directory where the template files in the website are stored, the default directory is templates
2, $compile _dir: Set the directory where the compiled files are stored in the site, the default directory is Templates_c
3, $config _dir: Define the directory for the template special configuration file, the default is Configs
4, $left _delimiter: For the left Terminator variable in the template, the default is ' {'
5, $right _delimiter: For the right terminator variable in the template, the default is '} '
Iv. Use of variables:
All accesses in Smarty are based on variables, which are described in one instance below.
Example idea: The Master file initializes the configuration file (init.inc.php) and a class by introducing a template, and assigns a value to the variables in the template.
First, set the init.inc.php file as the initialization configuration file for the Smarty template
init.inc.php
<?php define (' Root_path ', DirName (__file__));//define the site root directory require Root_path. ' /libs/smarty.class.php '; Load Smarty file $_tpl = new Smarty (); Instantiate an object $_tpl->template_dir = Root_path. ' /tpl/'; Reset the template directory to the TPL directory under the root directory $_tpl->compile_dir = Root_path. /com/'; Reset the compiled directory to the COM directory under the root directory $_tpl->left_delimiter = ' <{'; Reset left delimiter to ' <{' $_tpl->right_delimiter = '}> '; Reset left delimiter to '}> '?>
Master File index.php
<?php require ' init.inc.php '; Introduction of template initialization file require ' Persion.class.php '; Load object file global $_tpl; $title = ' This is a title! '; $content = ' This is Body content! '; /* * One, assigned to template variables from PHP; * Dynamic Data (PHP from database or file, and algorithm generated variables) * Any type of data can be allocated from PHP, mainly including the following * scalar: string, int, double, Boolean * Compound: Array, Object * NULL * Indexed arrays are an associative array that is accessed directly through an index, rather than using [associative subscript]. The Subscript Way * object is accessed directly by * */$_tpl->assign (' title ', $title); $_tpl->assign (' content ', $content); Variable assignment $_tpl->assign (' arr1 ', Array (' abc ', ' Def ', ' Ghi ')); An indexed array of assignment $_tpl->assign (' arr2 ', Array (' abc ', ' Def ', ' Ghi '), Array (' JKL ', ' mno ', ' PQR ')); Index two-dimensional array assignment $_tpl->assign (' Arr3 ', Array (' one ' = ' = ' 111 ', ' two ' = ' 222 ', ' three ' = ' 333 ')); The assignment of the associative array is $_tpl->assign (' ARR4 ', Array (' One ' =>array (' one ' = ' 111 ', ' "'" = ') ' 222 '), ' one ' and ' =>array ' (' Three ' = ' 333 ', ' four ' = ' 444 ')); Associative two-dimensional array assignment $_tpl->assign (' ARR5 ', Array (' One ' =>array (' 111 ', ' 222 '), Array (' three ' = ' 333 ', ' 444 ')); Assignment of associative and indexed mixed arrays $_tpl->assign (' OBject ', New persion (' small Easy ', 10)); The values in the object assignment//smarty can also be calculated (+-*/^ ...) $_tpl->assign (' Num1 ', 10); $_tpl->assign (' num2 ', 20); $_tpl->display (' Index.tpl ');? >
The template file for the main file index.php index.tpl (shelved in the/tpl/directory)
<{$title}> Variable access: <{$content}>
Access to indexed arrays: <{$arr 1[0]}> <{$arr 1[1]}> <{$arr 1[2]}>
Access to an indexed two-dimensional array: <{$arr 2[0][0]}> <{$arr 2[0][1]}> <{$arr 2[0][2]}> <{$arr 2[1][0]}> <{$arr 2[1][1] }> <{$arr 2[1][2]}>
Access to associative arrays: <{$arr 3.one}> <{$arr 3.two}> <{$arr 3.three}>
Access to associative two-dimensional arrays: <{$arr 4.one.one}> <{$arr 4.one.two}> <{$arr 4.two.three}> <{$arr 4.two.four}>
Access to associative and indexed mixed arrays: <{$arr 5.one[0]}> <{$arr 5.one[1]}> <{$arr 5[0].three}> <{$arr 5[0][0]}>
Access to member variables in the object: <{$object->name}> <{$object->age}>
Access to methods in object: <{$object->hello ()}>
Operation of variables: <{$num 1+ $num 2}>
Mixed operation of variables: <{$num 1+ $num $num 2/$num 1+44}>
Persion.class.php
<?php class Persion {public $name;//For easy access, set to public public $age; Defines a constructor method public function __construct ($name, $age) { $this->name = $name; $this->age = $age; } Define a Hello () method, output name and age public function Hello () { return ' Hello! My name is '. $this->name ', this year '. $this->age ' years old. '; } }?>
Execution Result:
Access to variables: This is body content! access to an indexed array: ABC def GHI access to two-dimensional arrays: ABC def GHI JKL mno pqr Associative array access: 111 222 333 access to the associated two-dimensional array: 111 222 333 4 44 access to associative and indexed mixed arrays: 111 222 333 444 access to member variables in objects: Small easy 10 access to methods in objects: Hello! My name is Xiao Yi, 10 years old this year. Operation of variables: 30 mixed operation of variables: 94
More about PHP related content readers can view the topic: "Smarty Template Primer Basic Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " PHP Basic Grammar Introductory tutorial, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL database Operations Primer and PHP Common database operations Tips Summary
It is hoped that this article will be helpful to everyone based on smarty template PHP program design.
Articles you may be interested in:
- PHP template engine Smarty built-in functions
- PHP template engine Smarty built-in variable mediator usage
- PHP template engine smarty custom variable mediator usage
- Analysis of the usage of reserved variables in PHP template engine Smarty
- PHP template engine Smarty built-in function foreach,foreachelse usage analysis
- PHP template Engine Smarty Example of how a configuration file is used in template variables
- Smarty how the template engine obtains data from PHP
- thinkphp How to use the Smarty template engine
- A method of generating random numbers in the PHP template engine smarty and an explanation of the math function
- Summary of cache Usage for PHP template engine Smarty
- 6 Tips for PHP smarty template engine
- [PHP] Template engine Smarty Introduction
- PHP template engine Smarty built-in functions section,sectionelse usage
http://www.bkjia.com/PHPjc/1119979.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119979.html techarticle php template engine smarty Examples of how variables are used, template Smarty This article describes how variables are used in the PHP template engine smarty. Share to everyone for your reference, as follows: ...