PHP uses smarty considerations and several ways to access variables _php tutorials

Source: Internet
Author: User
Tags smarty template
$TPL =new Smarty ();//Create a new Smarty object, I use the Smarty-3.1.6Version
1. Set the Smarty template path $tpl-> Settemplatedir (); By default templates
2. Set the Smarty template compilation path $tpl-> Setcompiledir (); By default Templates_c
3. Set the left and right separators of the Smarty template engine,

$tpl->left_delimiter= "<{";

$TPL->right_delimiter= "}>";

By default: public $left _delimiter = "{";//smarty source code

Public $right _delimiter = "}";//smarty source code

Why are we changing these separators?

For example, in an earlier version of the Smarty engine template, the error will not be recognized automatically.
Like what:

or JavaScript.
Copy CodeThe code is as follows:


In both cases, there are "left and right braces", and the Smarty engine encounters an error
4. Initialization operation we can create an additional PHP file that initializes the operation externally, such as: smarty.ini.php. Then include it in the PHP file.
Copy CodeThe code is as follows:
Include ". /smarty3.1.6/libs/smarty.class.php ";
$TPL =new Smarty ();
$tpl->settemplatedir ("./tpl");
$tpl->settemplatedir ("./compile");
$TPL->left_delimiter= "<{";
$TPL->right_delimiter= "}>";
?>

5. When using the display function of the Smarty template engine or including other templates, you can smarty the template directory specified in the object (e.g., the TPL directory, the default is the Templates directory) as the base directory.
① Template directory is: TPL, the directory contains a lot of templates, there are default,green,red templates, the default template directory has a lot of template files (INDEX.TPL, HEADER.TPL, FOOTER.TPL), the correct usage of display at this time: $tpl->display ("Default/index.tpl"); The default template directory under the base directory
② when a template file (such as: INDEX.TPL) contains other template files (such as: Header.tpl, FOOTER.TPL), the correct wording of the include should be: <{include "Default/header.tpl"}>, <{include "Default/footer.tpl"}>
Although Index.tpl, HEADER.TPL, Footer.tpl are all in the same directory, but <{include "Header.tpl"}>, <{include "Footer.tpl"}> is the wrong wording, In this case, the Smarty engine looks for headers and footer in the TPL directory, not under Default
6. If you want to allow PHP programs in each directory to load Smarty and use the template directory and compile directory specified by Smarty, the only way is to use absolute paths.
How to access variables in the 7.Smarty template engine (remember to add the "$" symbol before the variables in the template)
① Accessing an array
Indexed arrays:
$tpl->assign ("arr", Array ("AA", "BB", "CC"));
$tpl->assign ("arr2", Array (Array ("Two-dimensional array one by one", "two-dimensional array one or two"), Array ("Two-dimensional array 21", "two-dimensional array 22"));
Access Index array: <{$arr [0]}>, <{$arr [0]}>, <{$arr [0]}>
Access to two-dimensional indexed arrays: <{$arr 2[0][0]}>, <{$arr 2[0][1]}>
Associative arrays: (using. Symbols to access)
Access associative arrays: <{$arr 3.id}>, <{$arr 3.name}>, <{$arr 3.age}>
② Accessing objects
To create an object:
Copy CodeThe code is as follows:
Class human{
Private $sex;
Private $name;
Private $age;
Public function __construct ($s, $n, $a) {
$this->sex= $s;
$this->name= $n;
$this->age= $a;
}
Public Function Print_info () {
return $this->sex. " --". $this->name." --". $this->age;
}
}
$tpl->assign ("Student", New Human ("Male", "Marcofly", 22));

Assigning values to objects in a template: <{$student->print_info ()}>
Mathematical operations in the 8.Smarty template engine can be applied to template variables
Assigning values to variables
$tpl->assign ("Num1", 10);
$tpl->assign ("num2", 5.5);
Template variable Output
<{$num 1}>//Result 10
<{$num 2}>//Result 5.5
<{$num 1+ $num 2}>//Result 15.5
<{$num 1+ $num $num 2/$num 1}>//results 13.025
Original article
Reprint Please specify: Web Development _ Small Fly

http://www.bkjia.com/PHPjc/324671.html www.bkjia.com true http://www.bkjia.com/PHPjc/324671.html techarticle $tpl =new Smarty ();//Create a new Smarty object, I'm using Smarty-3.1.6 version 1. Set Smarty template path $tpl-settemplatedir (); Templates 2 by default. Set 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.