Examples of how to use variables in PHP template engine Smarty _php instance

Source: Internet
Author: User
Tags mixed php download php template smarty template

This example describes how to use variables in the PHP template engine smarty. Share to everyone for your reference, specific as follows:

First, overview:

Smarty is one of PHP's many template engines, which is a library of classes written from PHP.
Advantages of Smarty:
1, optimize the speed of website visit;
2, the Web page front-end design and program separation;

Second, the installation of Smarty

1, need to Smarty's official website http://www.smarty.net/download.php Download the latest Smarty version, such as the download version for: Smarty-2.6.18.tar.tar;

2, decompression Smarty-2.6.18.tar.tar compression package, will find a lot of files and folders, in addition to Libs folder, all the other delete, no use;

3, when calling the Smarty template engine, you should first use the PHP require statement to load libs/smarty.class.php this file.

The default settings for the Smarty class library

After require into the Smarty.class.php file, if you need to set up members in the Smarty class library, there are two ways to do this: either directly in the Smarty.class.php file or after the class library is initialized, typically using the latter. The following is a description of the member properties in the Smarty class library:

1, $template _dir: Set the site template files stored in the directory, the default directory is templates
2. $compile _dir: Set up the directory where the compiled files are stored in the Web site, the default directory is Templates_c
3, $config _dir: Defines the directory for storing template special configuration files, by default configs
4. $left _delimiter: The left terminator variable used in the template, the default is ' {'
5, $right _delimiter: For the right terminator variable in the template, the default is '} '

Four, the use of variables:

All accesses in Smarty are based on variables, as described below by an instance.

Example idea: The main file initializes the configuration file (init.inc.php) and a class by introducing a template and assigns a value to the variables in the template.

First, set the init.inc.php file as the initialization profile for the Smarty template
init.inc.php

<?php
  define (' Root_path ', DirName (__file__));//define site root directory
  require Root_path. ' /libs/smarty.class.php '; Load 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 COM directory at the root of the compilation directory
  $_tpl->left_delimiter = ' <{';   Reset the left delimiter to ' <{'
  $_tpl->right_delimiter = '}> ';    Reset left delimiter to '}> '
?>

Primary file index.php

<?php require ' init.inc.php '; Introduction of template initialization file require ' Persion.class.php ';
  Load object file global $_tpl;
  $title = ' is a title! ';
  $content = ' This is the body content! ';
  * * One, from PHP to assign to template variables; * Dynamic Data (PHP from database or file, and algorithm generated variables) * Any type of data can be distributed from PHP, mainly including the following * scalar: string, int, double, Boolean * Composite: Array, Object * NULL * Index array is an associative array that is accessed directly through the index, not using the [association subscript].
  Subscript the way * object is directly through the-> to access the * * * * * $_tpl->assign (' title ', $title); $_tpl->assign (' content ', $content);  Variables are assigned $_tpl->assign (' arr1 ', Array (' abc ', ' Def ', ' Ghi ')); The assignment of an indexed array $_tpl->assign (' arr2 ', Array (' abc ', ' Def ', ' Ghi '), Array (' JKL ', ' mno ', ' PQR ')); The assignment of an indexed two-dimensional array $_tpl->assign (' Arr3 ', Array (' One ' => ', ' two ' => ' 222 ', ' three ' => ' 333 ')); Associative array assignment $_tpl->assign (' ARR4 ', Array (' One ' =>array (' One ' => ', ' two ' => '), ' 222 ' two ') (' Three ' => ' 333 ', ' four ' => ' 444 ')); Assignment of associative two-dimensional arrays (' ARR5 ', Array (' One ' =>array (' $_tpl->assign ', ' 222 '), Array (' Three ' => ' 333 ', ' 444 ')); Associative and indexed mixed arraysThe assignment $_tpl->assign (' object ', New Persion (' small Easy ', 10));
  The value in an object assignment//smarty can also be calculated (+-*/^ ...).
  $_tpl->assign (' Num1 ', 10);
  $_tpl->assign (' num2 ', 20);

 $_tpl->display (' Index.tpl ');?>

Master file index.php template file Index.tpl (placed in/tpl/directory)

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

Persion.class.php

<?php
  class Persion {public
   $name;//For easy access, set to public
   $age;
   Defines a construction method public
   function __construct ($name, $age) {
     $this->name = $name;
     $this->age = $age;
   }
   Define a Hello () method, output name and age public
   function Hello () {return
     ' Hello! My name is ' $this->name. ', this year '. $this->age. ' Years old. ';
   }
 }
? >

Execution results:

Variable access: This are body content!
Access to indexed arrays: ABC def GHI
indexed two-dimensional array access: ABC def ghi JKL mno pqr associative array access: D 222 333
access to two-dimensional arrays:
222 333 Access to mixed-index hybrid arrays: 222 333 444
access to member variables in objects: Access to methods in small-easy
objects: Hello! My name is Xiao Yi, 10 years old this year.
operation of a variable:
mixed operation of a variable: 94

More about PHP Interested readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on Smarty template.

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.