Use Smarty in PHP: Use of Variables

Source: Internet
Author: User
Tags smarty template

 

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

 

<? 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 (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)

 

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "<a href =" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd </a>">

<Html xmlns = "<a href =" http://www.w3.org/1999/xhtml "> http://www.w3.org/1999/xhtml </a>">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> <{$ title}> </title>

</Head> <body>

Variable access: <{$ content}>

<Br/>

Access to the index array: <{$ arr1 [0] }>< {$ arr1 [1] }>< {$ arr1 [2]}>

<Br/>

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]}>

<Br/>

Access to the associated array: <{$ arr3.one }><{$ arr3.two }><{$ arr3.three}>

<Br/>

Access to a two-dimensional array: <{$ arr4.one. one}> <{$ arr4.one. two}> <{$ arr4.two. three}> <{$ arr4.two. four}>

<Br/>

Access to join and index hybrid Arrays: <{$ arr5.one [0] }>< {$ arr5.one [1] >>< {$ arr5 [0]. three }> <{$ arr5 [0] [0]}>

<Br/>

Access to member variables in the object: <{$ object-> name }>< {$ object-> age}>

<Br/>

Access to methods in the object: <{$ object-> hello ()}>

<Br/>

Variable calculation: <{$ num1 + $ num2}>

<Br/>

Mixed Variable operation: <{$ num1 + $ num2 * $ num2/$ num1 + 44}>

<Br/>

</Body>

</Html>

 

 

Persion. class. php

 

<? 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

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.