Detailed explanation of Smarty template usage example of php

  • 2021-07-07 06:46:22
  • OfStack

This article analyzes the use of smarty template in detail, for learning smarty friends have a certain reference value. Details are as follows:

1. Comments in templates

Every Smarty template file is developed by Web foreground language (xhtml, css and javascript, etc.) combined with the syntax of Smarty engine.
Used in the web foreground development language and the original complete 1 sample, the annotation has not changed.
Smarty annotation syntax is' left terminator variable value * 'and' * right terminator variable value '. The content between these two delimiters is annotation content, which can contain 1 or more lines. When users browse the web page to view the original code, they will not see annotations. It is just annotations inside the template. Here are examples of annotations.


$smarty->left_lelimiter = '<{';
$smarty->right_delimiter = '}>';

Note:


<{* this a note *}>

2. Variable declarations in templates

In Smarty, Cut 1 is dominated by variables, and all rendering logic is left to the template to control itself. There are several different types of variables, and the type of a variable depends on what symbol it is prefixed with or surrounded by. Variables can be output directly or as arguments to job function properties and modifiers or for internal conditional expressions, etc


<{$name}> <{* Regular type variable, you need to call the assign Function assignment value *}>
<{$contacts[row].phone}> <{* Array type variable, you need to call the assign Function assignment value *}>
<body bgcolor="<{#bgcolor#}>"> <{* The value of the variable read from the configuration file and output *}>

If you output a variable assigned from php in the Smarty template, you need to add a $symbol before the variable and enclose it with a delimiter. The naming method is the same as that of php, and the delimiting symbol is a bit like that of php < ?php ? > (In fact, they do get replaced with this.)

3. Output variables assigned from php in template

There are two kinds of variables commonly used in Smarty templates: 1 is a variable allocated from php; The other is the variable read from the configuration file
Note: You can only output the variables allocated from php in the template. You cannot re-assign values to these variables in the template. Variables are all global. You only need to allocate them once. If you allocate them more than twice, the content of the variables will be dominated by the last one
The foreach or section statements provided in Smarty are used to traverse each element in the output array. The index array and associative array are output in a slightly different way in the template. The access mode of the index array in the template is the same as the reference in the php script, and the element in the associative array is accessed in the way specified in the template.
Objects are accessed in templates and in php scripts through '- > 'This operator completes.

4. Mathematical calculation of variables in Smarty template

In the template, variables cannot be assigned directly, but they can participate in mathematical operations. As long as the mathematical operations that can be performed in php script can be applied in the template, as follows:


<{$foo + 1}> <{*  Variable addition 1 *}>
<{$foo * $bar}> <{*  Multiplying two variables  *}>
<{$foo->bar - $bar[1] * $foo->bar - 3 * 7}> <{*  Compound type variables participate in operations  *}>
<{if($foo + 2 == 10)}> <{*  Application of Mathematical Operation in Program Logic  *}>

Variables embedded in double quotation marks can be recognized in Smarty templates, but some variables must be enclosed in reverse quotation marks' ` '(this symbol and' ~ 'are on the same key), as follows:


<{"test $foo test"}> <{*  Using variables in double quotation marks  *}>
<{"test `$foo[0]` test"}> <{*  Array variables enclosed in reverse quotation marks in double quotation marks  *}>
<{"test `$foo.bar` test"}> <{*  Object member variables enclosed in reverse quotation marks in double quotation marks  *}>

5. Example of smarty template usage:

Step 1: Load the Smarty template engine, such as require 'libs/Smarty. class. php'
Step 2: Create an Smarty object, such as: $smarty = new Smarty ();
Step 3: Modify the default behavior of Smarty, such as opening cache, storing path of template, etc.
Step 4: Assign the data obtained in the program to the corresponding variables in the template through the assign () method of the Smarty object
Step 5: Output the template content with the display () method of the Smarty object

assign () method:

This method is used to assign values to variables in templates, which is easy to use
Prototype: void assign (string varname, mixed var)
This method assigns data of types supported by php to template variables including arrays and objects
There are two ways to use it:


// Specify 1 Right ' Name / Numerical value '
$smarty->assign('question',' How are you ');
$smarty->assign('answer',' Not so good ');
// Specifies that the ' Name / Numerical value '
$smarty->assign(array('question' => ' How are you ','answer' => ' Not so good '));// This method is less used 

display () method:

This method must be used in Smarty-based scripts, and can only be used once in a script. It is responsible for retrieving and displaying templates referenced by the Smarty engine
Prototype: var display (string template [, string cache_id] [, string compile_id])
Parameter 1: template is required, specifying the type and path of 1 valid template resource
Parameter 2: cache_id Specifies the name of 1 cache identifier
Parameter 3: compile_id is used when maintaining multiple caches for 1 page
Use as follows
$smarty- > display('tpl/template_dir/template.html');

Simple example:

1. libs: Is the Smarty class library
2. tpl/cache_dir: Store cache template
3. tpl/compile_dir: Store the compiled template file
4. tpl/config_dir: Store special configuration files
5. tpl/template_dir: Store template files
6. In smarty. php file, new gives an Smarty class object, and sets the attribute value of each object, as follows


<?php
require 'libs/Smarty.class.php';// Loading Smarty.class.php Documents         
define('SITE_ROOT','./tpl/');// Definition 1 Constants 
$tpl = new Smarty();
$tpl->template_dir = SITE_ROOT . 'template_dir';// Save template file   
$tpl->compile_dir = SITE_ROOT . 'compile_dir';// Save the compiled template file 
$tpl->config_dir = SITE_ROOT . 'config_dir';// Save special configuration files 
$tpl->cache_dir = SITE_ROOT . 'cache_dir';// Deposit Smarty Cache file   
$tpl->caching = 1;// Enable caching 
$tpl->cache_lifetime = 60*60*24;// Cache time 1 Days   
$tpl->left_delimiter = '<{';// Left Terminator 
$tpl->right_delimiter = '}>';// Right Terminator 

7. The first page code of index. php file is as follows


<?php      
 require 'smarty.php';
 $tpl->assign('title','title Test ');
 $tpl->assign('content','content Test ');
 $tpl->display('template.html');

8. tpl/template_dir/template. html This is a template file code as follows


 <html>        
 <head>        
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>
   <{$title}>
 </title>
 </head>
 <body>
 <{$content}>
 </body>
</html>

Related articles: