How to define variables and constants in the Smarty template

Source: Internet
Author: User
Tags foreach smarty template

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.