Example of how to use variables in the PHP template engine Smarty _ php instance

Source: Internet
Author: User
Tags php define php template
This article describes how to use variables in the PHP template engine Smarty. It describes in detail the principles of the Smarty template, downloading, configuration methods, and usage of variables, for more information about how to use variables in the PHP template engine Smarty, see the example below. We will share this with you for your reference. The details are as follows:

I. Overview:

Smarty is one of the many PHP template engines. It is a class library written according to PHP.
Advantages of Smarty:
1. Optimize Website access speed;
2. webpage front-end design and program separation;

Ii. Install Smarty

1, need to go to the official site of Smarty http://www.smarty.net/download.php to download the latest Smarty version, such as download version: Smarty-2.6.18.tar.tar;

2, unzip the Smarty-2.6.18.tar.tar compressed package, will find a lot of files and folders, except the libs folder, all other Delete, are useless;

3. When the Smarty template engine is called, use the PHP require statement to load the libs/Smarty. class. php file.

Iii. default settings of the Smarty class library

Require into Smarty. class. after the php file, if you need to set members in the Smarty class library, there are two methods: one is directly in the Smarty. class. modify in the php file. One is to re-specify the class library after initialization. The latter is generally used. The following describes the member attributes in the Smarty class Library:

1. $ template_dir: Set the directory where template files are stored on the website. The default directory is templates.
2. $ compile_dir: Set the directory for storing compiled files on the website. The default directory is templates_c.
3. $ config_dir: defines the directory used to store the special configuration files of the template. The default value is configs.
4. $ left_delimiter: Used for the left Terminator variable in the template. The default value is '{'
5. $ right_delimiter: Used for the right Terminator variable in the template. The default value is '}'

Iv. 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 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

<? 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 $ _ tpl-> compile_dir = ROOT_PATH under the root directory. '. /com/'; // reset the compilation directory to the com directory $ _ tpl-> left_delimiter =' <{'; // reset the left delimiter to '<{' $ _ tpl-> right_delimiter = '}>'; // reset the left delimiter to '}>'?>

Index. php of the main file

<? Php 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 (variables generated by PHP from databases or files and algorithms) * Any type of data can be allocated from PHP, mainly including * scalar: string, int, double, boolean * combination: array and object * NULL * index arrays are * associated arrays directly accessed through indexes, rather than [correlated subscripts. the following method is used: * The object is directly accessed through-> **/$ _ tpl-> assign ('title', $ title ); $ _ tpl-> assign ('content', $ content); // variable assignment $ _ tpl-> assign ('arr1 ', array ('abc ', 'def ', 'ghi'); // the value of the index array $ _ tpl-> assign ('arr2', array ('abc', 'def ', 'ghi'), array ('J Kl ', 'mno', 'pqr'); // The value of the two-dimensional Index Array $ _ tpl-> assign ('arr3 ', array ('one' => '123', 'two' => '123', 'three '=> '123 ')); // assign a value to the associated array $ _ tpl-> assign ('arr4 ', array ('one' => '123 ', 'two' => '123'), 'two' => array ('Three '=> '123', 'four' => '123 '))); // assign a value to the correlated two-dimensional array $ _ tpl-> assign ('arr5 ', array ('one' => array ('123456', '123456 '), array ('Three '=> '20140901', '20160901'); // assign values to join and index hybrid arrays $ _ tpl-> assign ('object ', new Persion ('xiaoyi', 10); // object Value assignment // Smarty You can also perform operations on values (+-*/^ ......) $ _ 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

<? Php class Persion {public $ name; // set to public $ age for convenient access; // define a constructor public function _ construct ($ name, $ age) {$ this-> name = $ name; $ this-> age = $ age;} // defines a hello () method, and outputs 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: access to the abc def ghi Index two-dimensional array: access to the abc def ghi jkl mno pqr associated array: 111 222 333 access to the associated two-dimensional array: 111 222 333 444 join and index hybrid array access: 111 222 333 444 access to member variables in an object: access to methods in a small and easy 10 object: Hello! My name is Xiaoyi. I am 10 years old. Variable operation: 30 variable mixed operation: 94

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.