How do I 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 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.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.