SoFunction
Updated on 2025-04-09

Notes on using Smarty in php and several ways to access variables

$tpl=new Smarty();// Create a new smarty object, I useSmarty-3.1.6Version
1. Set the smarty template path $tpl->setTemplateDir(); by default it istemplates
2. Set the smarty template compilation path $tpl->setCompileDir(); By default it istemplates_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 do we need to change these separators?

Because for example, in the earlier version of the smarty engine template, an error will be reported and cannot be automatically recognized.
for example:
<style>
div{margin:0;}
</style>
Or in javascript

Copy the codeThe code is as follows:

<script>
function show(){
alert("smarty");
}
</script>

In both cases, there are "left and left braces", and if the smarty engine encounters it, it will report an error.
4. Initialization operation We can create another php file for initialization operation externally, such as:. Then include it in the php file
Copy the codeThe code is as follows:

<?php
include "../Smarty3.1.6/libs/";
$tpl=new Smarty();
$tpl->setTemplateDir("./Tpl");
$tpl->setTemplateDir("./Compile");
$tpl->left_delimiter="<{";
$tpl->right_delimiter="}>";
?>

5. When using the display function or include other templates of the smarty template engine, the template directory specified in the smarty object (such as: Tpl directory, default is the templates directory) as the base directory.
① The template directory is: Tpl. There are many templates stored in this directory, including default, green, red templates. There are many template files (, ,) in the default template directory. At this time, the correct usage of display: $tpl->display("default/"); that is, the default template directory in the base directory
② When other template files are included in template files (such as:), the correct way to write include should be: <{include "default/"}>, <{include "default/"}>
Although , , are all in the same directory, <{include ""}> and <{include ""}> are the wrong writing methods. In this case, the smarty engine will look for the header and footer in the Tpl directory, rather than searching under default
6. If you want 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 template engine (remember to add the "$" symbol before variables in the template)
①Accessing the array
Index array:
$tpl->assign("arr",array("aa","bb","cc"));
$tpl->assign("arr2",array(array("two-dimensional array one-one","two-dimensional array one-two"),array("two-dimensional array two-one","two-dimensional array two-two")));
Access index array: <{ $arr[0] }>, <{ $arr[0] }>, <{ $arr[0] }>
Accessing a 2D index array: <{ $arr2[0][0] }>, <{ $arr2[0][1] }>
Associative array: (using . symbols to access)
Access associative arrays: <{$}>, <{$}>, <{$}>
②Access Object
Create an object:
Copy the 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));

Assign values ​​to objects in the template: <{$student->print_info()}>
Mathematical operations in the template engine can be applied to template variables
Assign values ​​to variables
$tpl->assign("num1",10);
$tpl->assign("num2",5.5);
Template variable output
<{$num1}> //Result 10
<{$num2}> //Result 5.5
<{$num1+$num2}> //Result 15.5
<{$num1+$num2*$num2/$num1}>//Result 13.025
Original article
Please indicate: WEB Development_Xiaofei