The examples in this article describe the usage of Smarty variables. Share it with everyone for your reference, the details are as follows:
1. Variables allocated from PHP
When calling variables allocated from PHP, you need to add the "$" symbol in front. (Annotation: the same as PHP)
The same is true for variables allocated by calling the assign function in the template. (Annotation: It is also called with $ plus the variable name)
Example:
index.php:
$smarty = new Smarty; $smarty->assign('firstname', 'Doug'); $smarty->assign('lastLoginDate', 'January11th, 2001'); $smarty->display('index.tpl');
index.tpl:
Hello {$firstname}, glad to see you couldmake it. <p> Your last login was on {$lastLoginDate}.
Output:
Hello Doug, glad to see you could make it. <p> Your last login was on January 11th, 2001.
2. Variables read from configuration file
The variables in the configuration file need to be called by using two "#" or smarty's reserved variable $smarty.config. (will be discussed later)
The second syntax is useful when the variable is used as an attribute value and is enclosed in quotes.
(Annotation: For example {include file="#includefile#"} In this way, #includefile# will be treated as a character and does not represent a configuration file variable, but it can be expressed as {include file="`$smarty. config.includefile`"}Don't forget to add ``)
Example:
foo.conf:
pageTitle = "This is mine" bodyBgColor = "#eeeeee" tableBorderSize = "3" tableBgColor = "#bbbbbb" rowBgColor = "#cccccc"
index.tpl:
{config_load file="foo.conf"} <html> <title>{#pageTitle#}</title> <body bgcolor="{#bodyBgColor#}"> <table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}"> <tr bgcolor="{#rowBgColor#}"> <td>First</td> <td>Last</td> <td>Address</td> </tr> </table> </body> </html>
index.tpl:
{config_load file="foo.conf"} <html> <title>{$smarty.config.pageTitle}</title> <body bgcolor="{$smarty.config.bodyBgColor}"> <table border="{$smarty.config.tableBorderSize}"bgcolor="{$smarty.config.tableBgColor}"> <tr bgcolor="{$smarty.config.rowBgColor}"> <td>First</td> <td>Last</td> <td>Address</td> </tr> </table> </body> </html>
The above two template writing methods both output:
<html> <title>This is mine</title> <body bgcolor="#eeeeee"> <table border="3" bgcolor="#bbbbbb"> <tr bgcolor="#cccccc"> <td>First</td> <td>Last</td> <td>Address</td> </tr> </table> </body> </html>
Configuration file variables can only be used after they are loaded.
Readers who are interested in more Smarty-related content can check out the special topics on this site: "Basic Tutorial for Getting Started with Smarty Templates", "Summary of PHP Template Technology", "Summary of PHP Database Operation Skills Based on PDO", "PHP Operations and Operators" Usage summary", "PHP network programming skills summary", "PHP basic syntax introductory tutorial", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "Summary of Common Database Operation Skills in PHP"
I hope this article will be helpful to everyone’s PHP program design based on smarty templates.