Smarty installation and example, smarty installation example _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Smarty installation and example, and smarty installation example. Smarty installation and example. smarty installation environment: smarty1.download the latest smartypackage at www.smarty.netdownload. select zips for Windows and tar.gz for Linux. Take windows as an example, and install smarty.

Environment:

Smarty

1. download the latest smartypackage at http://www.smarty.net/download. for Windows, select zips and linuxto select tar.gz. Take windows as an example. download and decompress the package, for example, f: \ smarty.

2. copy the lib directory in the extracted smarty directory to test and rename it as smarty. Create the tpls directory under the test directory, and create the templates, templates_c, configs, and cache directories under the tpls Directory. these directories are template directories (required ), resolution Directory (required), configuration Directory (optional), cache Directory (optional ),

The php code of smarty is the same level as the four directories, and the html code is stored in templates.

The directory tree is as follows:

Code section:

1. create main. php without bom in UTF-8 in test/smarty and configure some member attributes of smarty.

1
 Template_dir = SMARTY_ROOT. "/templates/"; // set the template file storage directory 6 $ tpl-> compile_dir = SMARTY_ROOT. "/templates_c/"; // Set the directory where the compiled files are stored. 7 $ tpl-> config_dir = SMARTY_ROOT. "/configs/"; // Set the configuration file storage directory 8 $ tpl-> cache_dir = SMARTY_ROOT. "/cache/"; // Set the cache file storage directory 9 $ tpl-> caching = 1; // enable cache 10 $ tpl-> cache_lifetime = 60*60*24; // The validity period is 11 $ tpl-> left_delimiter = '['; // the left and right Terminator of the smarty Language 12 $ tpl-> right_delimiter = ']'; 13?>

We know that braces are the default delimiters of smarty, but they may conflict with javascript, css, and so on. So here we set them to [and].

2. create an html. tpl template file under test/tpls/templates, that is, add the smarty variable to html. The template is equivalent to the presentation layer.

The code for html. tpl is as follows:

 1  2  3 
  4   5 [$title] 6   7   8   9 [$content]10  11 

3. create smarty. php in the "test" directory. the file is equivalent to the driver layer. assign a value to the variables in the above presentation layer and then display them.

The code for smarty. php is as follows:

1
 Assign ("title", "late"); 4 $ tpl-> assign ("content", "$500! "); 5 $ tpl-> display (" tpls/templates/html. tpl "); 6?>

4. run smarty. php in the browser.

Environment: smarty 1. download the latest smartypackage at http://www.smarty.net/download. in windows, select zips, and in Linux, select tar.gz. Take windows as an example...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.