Usage of smarty templates

Source: Internet
Author: User

1. Download the smarty template and decompress it to the project folder;

For example, my project folder is the root directory set by Apache: C: \ Wamp \ www. To make it easy to try, I directly created a folder named smarty, then, extract the file to it;


2. Modify the include_path attribute of the php. ini configuration file and add the smarty library file;

Note: Here there are two indclude_path attributes: one is to modify the Unix environment and the other is to modify the Linx environment. Please modify them as needed;

For example, if I use a Windows platform and add an absolute path, change it to: Export de_path = "C:/Wamp/www/smarty/demo/Libs ".


3. Set the permissions for these four files respectively (not all of them need to wake up this step, as I do not need it ):

Configs: used to store various configuration files;

Cache: used to store the cache;

Template: used to store template files;

Template_c: used to store compiled files;


4. Add the template file for each page you need in smarty/demo/templates: (view layer)

For example, I am only doing an experiment to make a simple output. I entered the following code in index. TPL:

<HTML>

 

<Body>


Hello, {$ name };


</Body>



</Html>


5. Add the following code to the index. php file created in index/home/control: (Controller layer)

<? PHP


// Load the smarty library. INI sets include_path to C:/Wamp/www/smarty/demo/libs, so you can directly use include ("Smarty. class. PHP ");

// If include_path is not set, you can directly copy smarty. Class. php to the website directory without adding an absolute path.

Require ('C:/Wamp/www/smarty/libs/smarty. Class. php ');

$ Smarty = new smarty ();

// The following (your website directory) uses an absolute path, and you can use a relative path (./templates)

$ Smarty-> config_dir = 'C:/Wamp/www/smarty/demo/configs'; // specify (your website) configuration file path (here I use the absolute path)

$ Smarty-> cache_dir = 'C:/Wamp/www/smarty/demo/smartycache '; // specify (your website) cache file path (here I use absolute path)

$ Smarty-> template_dir = 'C:/Wamp/www/smarty/demo/templates '; // specify (your website) path of the template (here I use the absolute path)

$ Smarty-> compile_dir = 'C:/Wamp/www/smarty/demo/templates_c '; // specify (your website) (Here I use the absolute path)

// Required parameter configuration before using smarty for the above four actions

$ Smarty-> assign ('name', 'What is the fuck! '); // The prototype of this number is assign (string varname, mixed var), varname is the template variable used in the template, and VAR indicates the variable name to replace the template variable; the second prototype is assign (mixed var). We will explain in detail how to use this member function in the following example. Assign is one of the core functions of smarty, all replace template variables must use it.

$ Smarty-> display ('index. TPL '); // the original form of this function is display (string varname). It is used to display a template. The template file here does not need to be added with a path. You only need to use a file name, the path is defined in $ smarty-> templates (string path.


?>


Usage of smarty templates

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.