0. introduction in the PHP world, there have been a variety of template classes, but in terms of efficacy and speed, Smarty is still in the leading position, because Smarty is relatively powerful, therefore, the application is a little more complex than some other template classes. Now
0. IntroductionIn the PHP world, there have been a variety of template classes, but in terms of efficacy and speed, Smarty is still in the leading position, because Smarty is relatively powerful, therefore, the application is a little more complex than some other template classes. Now it takes 30 minutes for you to enter the door quickly.
1. InstallationFirst open the webpage http://smarty.php.net/download.php and download the latest smarty. Decompress the downloaded file (the directory structure is quite complex ). Next, I will demonstrate how to install an instance.
(1) I created a new directory learn/under the root directory, and then created a directory named smarty/In learn /. Copy the libs/from the extracted directory to smarty/, create the templates directory in smarty/, and create cache/, templates/, templates_c/, config /, for example:
(2) create a new template file: index. tpl. Place the file in the learn/smarty/templates Directory. the code is as follows:
Smarty{$ Hello}
Create index. php and put this file under learn:
$ Smarty-> template_dir = 'smarty/templates/templates ';
$ Smarty-> compile_dir = 'smarty/templates/templates_c ';
$ Smarty-> config_dir = 'smarty/templates/config ';
$ Smarty-> cache_dir = 'smarty/templates/cache ';
// The smarty template has the high-speed cache function. If this parameter is set to true, caching is enabled, but the title of the webpage is not updated immediately, of course, you can also solve $ smarty-> caching = false; $ hello = 'Hello World! '; // Value $ smarty-> assign ('hello', $ hello); // reference the template file $ smarty-> display ('index. tpl');?> (3) execute index. php to see Hello World! .
II. AssignmentIn the template file, replace the value with braces {}, and add the $ sign before the value. For example, {$ hello }. This can be an array, for example, {$ hello. item1}, {$ hello. item2 }... In the PHP source file, only a simple function assign (var, value) is required ). A simple example: *. tpl:
Hello, {$ exp. name }! Good {$ exp. time}
*. Php:
$ Hello [name] = "Mr. Green ";
$ Hello [time] = "morning"; $ smarty-> assign ("exp", $ hello); output: