[Smarty]Define variables and arrays in Smarty (assign)

This time, i'll try to define the variable s on the template. Due to the nature of smarty, it is originally recommended to do on the PHP side, but it is not said so in the field. In fact, variables are often created and addressed on the template side. I think that there are various […]

This time, i'll try to define the variable s on the template.

Due to the nature of smarty, it is originally recommended to do on the PHP side, but it is not said so in the field.
In fact, variables are often created and addressed on the template side.

I think that there are various cases even if it is simply called a variable definition.
Here are some examples of what we used in the field.

Defining variables in Smarty

{assign var="name" value="Yamada"}
{assign "name" "Yamada"}

If you simply want to create a variable called name, that's it.The second example is a short form.
You can also use variables.It's okay if the variable is an array.

{assign var="name" value=$user.name}

If it is a number, it is possible to calculate it.

{assign var="total" value=$destroybonus+$score}

Concatenate strings and variables, and combine variables

This is surprisingly convenient.You can create variables by attaching strings and variables.
It is useful when you want to make more than one variable in a foreach statement.

{assign var="bonusImage" value="label`$bonusId`.png"}

Note the variable enclosure: the variable is enclosed in `;.
It is not a single but a back quote.
As you can imagine, the variables and variables are as follows.

{assign var="nextButton" value="`$color``$length`"}

They count

It is a common count.

{assign var="count" value=0}
{$count=$count+1}

$count++ is not calculated. If you want to add in a foreach sentence, etc.

See also: Defining arrays

Smarty2,This requires a smarty plugin.
Smarty3 doesn’t requier the plugin.
⇒Reference http://d.hatena.ne.jp/studio-m/20070512/1178978926

{assign_array var="fruits" values="apple,banana,orange"}

Leave a Reply 0

Your email address will not be published.