Use Smarty in PHP: use of variables. All the accesses to Smarty are variable-based. The following describes how to use an instance. Example: the main file introduces the template to initialize the configuration file (init. inc. php) and a class. all access to Smarty is based on variables. The following describes how to use an instance.
Example: the main file introduces the template initialization configuration file (init. inc. php) and a class, and assigns values to the variables in the template.
First, set the init. inc. php file as the initialization configuration file of the Smarty template.
Init. inc. php
Define ('root _ path', dirname (_ FILE _); // defines the website ROOT directory
Require ROOT_PATH. '/libs/Smarty. class. php'; // load the 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 compilation directory to the com directory under the root directory.
$ _ Tpl-> left_delimiter = '<{'; // reset the left delimiter to '<{'
$ _ Tpl-> right_delimiter = '}>'; // reset the left delimiter to '}>'
?>
Index. php of the main file
Require 'init. inc. php'; // introduce the template initialization file
Require 'persion. class. php'; // load the object file
Global $ _ tpl;
$ Title = 'This is a title! ';
$ Content = 'This is body content! ';
/*
* 1. allocate Template variables from PHP;
* Dynamic data (PHP variables generated from databases, files, and algorithms)
* Any type of data can be allocated from PHP, including the following:
* Scalar: string, int, double, boolean
* Combination: array and object
* NULL
* Index arrays are directly accessed through indexes.
* Join an array. instead of using [join subscript], use the. submark method.
* Objects are directly accessed through->.
**/
$ _ Tpl-> assign ('title', $ title );
$ _ Tpl-> assign ('content', $ content); // variable assignment
$ _ Tpl-> assign ('ar1', array ('ABC', 'def', 'ghi'); // assign values to the index array
$ _ Tpl-> assign ('arr2 ', array ('ABC', 'def', 'ghi'), array ('jkl', 'mno ', 'pqr '); // index the value of a two-dimensional array
$ _ Tpl-> assign ('arr3 ', array ('one' => '123', 'two' => '123 ', 'Three '=> '123'); // assign values to the correlated array
$ _ Tpl-> assign ('arr4 ', array ('one' => '000000', 'two' => '000000 '), 'two' => array ('Three '=> '20140901', 'four' => '20160901'); // associate the values of two-dimensional arrays.
$ _ Tpl-> assign ('arr5 ', array ('one' => array ('000000', '000000'), array ('Three' => '000000 ', '20140901'); // assign values to the joined and indexed Hybrid Arrays
$ _ Tpl-> assign ('object', new Persion ('xiaoyi', 10); // assign values to objects
// The value in Smarty can also be calculated (+-*/^ ......)
$ _ Tpl-> assign ('num1', 10 );
$ _ Tpl-> assign ('num2', 20 );
$ _ Tpl-> display ('index. tpl ');
?>
Index. tpl, template file of index. php in the main file (put in the/tpl/directory)
">
<{$ Title}>
Variable access: <{$ content}>
Access to the index array: <{$ arr1 [0] }>< {$ arr1 [1] }>< {$ arr1 [2]}>
Access to an indexed two-dimensional array: <{$ arr2 [0] [0] }>< {$ arr2 [0] [1] }>< {$ arr2 [0] [2] }>< {$ arr2 [1] [0] }>< {$ arr2 [1] [1] }>< {$ arr2 [1] [2]}>
Access to the associated array: <{$ arr3.one }><{$ arr3.two }><{$ arr3.three}>
Access to a two-dimensional array: <{$ arr4.one. one}> <{$ arr4.one. two}> <{$ arr4.two. three}> <{$ arr4.two. four}>
Access to join and index Hybrid Arrays: <{$ arr5.one [0] }>< {$ arr5.one [1] >>< {$ arr5 [0]. three }> <{$ arr5 [0] [0]}>
Access to member variables in the object: <{$ object-> name }>< {$ object-> age}>
Access to methods in the object: <{$ object-> hello ()}>
Variable calculation: <{$ num1 + $ num2}>
Mixed variable operation: <{$ num1 + $ num2 * $ num2/$ num1 + 44}>
Persion. class. php
Class Persion {
Public $ name; // Set to public for convenient access
Public $ age;
// Define a constructor
Public function _ construct ($ name, $ age ){
$ This-> name = $ name;
$ This-> age = $ age;
}
// Define a hello () method and output the name and age
Public function hello (){
Return 'Hello! My name is '. $ this-> name.'. $ this-> age. ';
}
}
?>
Execution result:
Variable access: This is body content!
Access to the index array: abc def ghi
Access to the two-dimensional array indexed: abc def ghi jkl mno pqr
Join array access: 111 222 333
Join two-dimensional array access: 111 222 333 444
Join and index Hybrid array access: 111 222 333 444
Access to member variables in the object: Xiaoyi 10
Object method access: Hello! My name is Xiaoyi. I am 10 years old.
Variable calculation: 30
Mixed operation of variables: 94
From: lee's column
All accesses to the http://www.bkjia.com/PHPjc/478576.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478576.htmlTechArticleSmarty are variable-based, which is illustrated by an instance below. Example: the main file initializes the configuration file (init. inc. php) by introducing the template and a class ,...