Defining constants is very simple
The method is:
The code is as follows |
Copy Code |
{$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 |
Copy Code |
{Assign var= "UserName" value= "Myz"} |
A variable defined inside the template is shown here: UserName = {$UserName}
Example
The code is as follows |
Copy Code |
<*assign var= "I" value=0*> <*foreach from= $r _article item=row*> <div class= "Scont stecont<* $i *>" > <ul> <*foreach from= $row item=r*> <li> <* $r [' title ']*><a href= ' article.php?aid=<* $r [' id ']*> ' target= ' _blank ' > ' read online ' </a> <a href= "<* $r [' Attachment ']*>" target= "_blank" > "Download" </a> </li> <*/foreach*> </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:
The code is as follows |
Copy Code |
<!–{ Math equation=x x=0 assign=i}–> <!–{section name=smartyvar loop=10}–> <!–{math equation= $i +1 Assign =i}–> <br/> <!–{$i}–> <br/> <!–{/section}–> Output effect: <br/ 1 <br/> <br/> 2 <br/> <br/> 3 <br/>< br> <br/> 4 <br/> <br/> 5 <br/> <b R/> 6 <br/> <br/> 7 <br/> <br/> 8 &l T;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.