php USES the relevant considerations of Smarty and several ways to access variables

  • 2020-05-10 17:47:06
  • OfStack

$tpl = new Smarty (); // create a new smarty object. I'm using Smarty-3.1.6
1. Set smarty template path $tpl- > setTemplateDir (); The default is templates
2. Set the smarty template compilation path $tpl- > setCompileDir (); The default is 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 do we change these delimiters?

For example, in earlier versions of smarty engine templates, errors were reported and could not be automatically recognized.
Such as:
< style >
div{margin:0;}
< /style >
Or javascript

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

In both cases, there are "left and right curly braces," and the smarty engine will report an error when it touches it
4. Initialization we can create an php file for another initialization operation externally, such as smarty.ini.php. Then include it in the php file
 
<?php 
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 smarty template engine's display function or include's other templates, the template directory specified in the smarty object (e.g. Tpl directory, templates by default) is the base directory.
Tpl, default, green, red templates, default templates, index.tpl, header.tpl, footer.tpl, display > display (" default/index. tpl "); The default template directory in the base directory
When the template file (e.g. index.tpl) contains other template files (e.g. header.tpl, footer.tpl), the correct way to write include is: < {include "default/header tpl}" > , < {include "default/footer tpl}" >
index.tpl, header.tpl, footer.tpl are all in the same directory, but < {include "header. tpl}" > , < {include "footer. tpl}" > In this case, the smarty engine will look for header and footer in the Tpl directory, rather than under default
6. If you want the PHP program in each directory to be able to load Smarty and use the template directory and compile directory specified by Smarty, the only way is to use the absolute path.
7. How to access variables in the Smarty template engine (remember to add "$" before variables in the template)
Access array
Indexed array:
$tpl - > assign("arr",array("aa","bb","cc"));
$tpl - > assign("arr2",array(array(" 2-d array 11"," 2-d array 12"),array(" 2-d array 21"," 2-d array 22"));
Access index array: < { $arr[0] } > , < { $arr[0] } > , < { $arr[0] } >
Access to a 2-dimensional indexed array: < { $arr2[0][0] } > , < { $arr2[0][1] } >
Associative array :(accessed using the. Symbol)
Access associative array: < {$arr3.id} > , < {$arr3.name} > , < {$arr3.age} >
(2) access object
Create object:
  
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()} >
8. The mathematics of the Smarty template engine can be applied to template variables
Assign a value to a variable
$tpl - > assign("num1",10);
$tpl - > assign("num2",5.5);
Template variable output
< {$num1} > 10 / / results
< {$num2} > / / 5.5 as a result
< {$num1+$num2} > / / 15.5 as a result
< {$num1+$num2*$num2/$num1} > / / 13.025 as a result
Original articles
Please note: WEB development _ xiaofei


Related articles: