Defining constants is very simple
The method is:
| The code is as follows |
|
|
{$smarty. Const. The constant name you define} |
How do you define a variable?
Define variables in Smarty.
{* The following paragraph is equivalent to defining a variable within the template username*}
| The code is as follows |
|
|
{Assign var= "UserName" value= "Myz"} |
A variable defined inside the template is shown here: UserName = {$UserName}
Example
| code is as follows |
&nbs P; |
|
<*assign var= "i" VALUE=0*>&NBSP;
<*foreach from= $r _article item=row*>
<div class= "Scont stecont<* $i *>" >&NBSP;
<ul>
<*foreach from= $row ITEM=R*>&NBSP
<li>
<* $r [' title ']*><a href=] article.php? aid=<* $r [' id ']*> ' target= ' _blank ' > ' online reading </a> <a href= ' <* $r [' Attachment ']*> ' target= ' _blank "> Download" </a>
</li> &NBSP
<*/foreach*>&NBSP
</ul>
</div>
<* $i = $i + 1*>
<*/foreach*> |
Description
1, the template defines variables, examples are as follows: <{assign var= "I" value=0}> (note: I is integral type, so value=0, do not need to add double quotes, otherwise it will be judged as a string type).
Since foreach does not have a counter like for, it customizes a counter here, counts it in the loop, or does not define a variable in the template page, and when you include the template, a variable i comes in.
2, on the addition of Smarty, tried, Smarty does not support "+ +" (including variables before + +, variables after + +), "+ +" way, so add 1 or use "$i = $i +1" way.
Look at one more instance
The code is as follows:
| code is as follows |
&nbs P; |
|
<!–{math equation=x x=0
Assign=i}–> <!–{section Smartyvar loop=10}–>
<!–{math equation= $i +1 assign=i}–>
<br/>
<!–{$i}–>
<br/& Gt
<!–{/section}–>
Output Effect:
<br/>
1
<br/>
<br/>
2
<br/>
<br/>
3
&l T;BR/>
<br/>
4
<br/>
<br/>
5
<br/>
;
<br/>
6
<br/>
<br/>
7
<br/>
<br/>
8
<br/>
<br/>
9
<br/>
<br/>
<br/> |
Note: If $i's initial value is not 0, such as 1, the math equation=x x=1 assign=i can also be written as "math equation=1 assign=i", omitting the X variables. It's 0 o'clock, you can't save, or you'll get a warning message.
Warning:smarty error:math:missing equation parameter
Although it is not a good idea to assign a value to a variable in a template, you can try it the way you want it.