Home > Backend Development > PHP Tutorial > Detailed explanation of smarty variable usage, detailed explanation of smarty variable_PHP tutorial

Detailed explanation of smarty variable usage, detailed explanation of smarty variable_PHP tutorial

WBOY
Release: 2016-07-12 08:52:50
Original
784 people have browsed it

Detailed explanation of Smarty variable usage, detailed explanation of smarty variable

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');

Copy after login

index.tpl:

Hello {$firstname}, glad to see you couldmake it.
<p>
Your last login was on {$lastLoginDate}.

Copy after login

Output:

Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.

Copy after login

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"

Copy after login

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>

Copy after login

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>

Copy after login

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>

Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1125880.htmlTechArticleDetailed explanation of Smarty variable usage, detailed explanation of smarty variable. This article describes the usage of Smarty variable with examples. Share it with everyone for your reference, the details are as follows: 1. Variables allocated from PHP call variables allocated from PHP...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template