aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/designers/language-builtin-functions/language-function-assign.md
blob: f5faa46d57751dc3a8c8cf6ef935790d2cc01c1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# {assign}, {$var=...}

`{assign}` or `{$var=...}` is used for assigning template variables **during the
execution of a template**.

## Attributes of the {assign} syntax
| Attribute Name | Required   | Description                                                           |
|----------------|------------|-----------------------------------------------------------------------|
| var            |            | The name of the variable being assigned                               | 
| value          |            | The value being assigned                                              |                             
| scope          | (optional) | The scope of the assigned variable: \'parent\',\'root\' or \'global\' | 

## Attributes of the {$var=...} syntax
| Attribute Name | Required   | Description                                                           |
|----------------|------------|-----------------------------------------------------------------------|
| scope          | (optional) | The scope of the assigned variable: \'parent\',\'root\' or \'global\' | 

## Option Flags
| Name    | Description                                       |
|---------|---------------------------------------------------|
| nocache | Assigns the variable with the 'nocache' attribute |


> **Note**
>
> Assignment of variables in-template is essentially placing application
> logic into the presentation that may be better handled in PHP. Use at
> your own discretion.

## Examples

```smarty
{assign var="name" value="Bob"}  {* or *}
{assign "name" "Bob"} {* short-hand, or *}
{$name='Bob'}

The value of $name is {$name}.
```
      

The above example will output:

```
The value of $name is Bob.
```


```smarty
{assign var="name" value="Bob" nocache}  {* or *}
{assign "name" "Bob" nocache} {* short-hand, or *}
{$name='Bob' nocache}

The value of $name is {$name}.
```
The above example will output:
```
The value of $name is Bob.
```
      

```smarty
{assign var=running_total value=$running_total+$some_array[$row].some_value}  {* or *}
{$running_total=$running_total+$some_array[row].some_value}
```

Variables assigned in the included template will be seen in the
including template.

```smarty
{include file="sub_template.tpl"}

{* display variable assigned in sub_template *}
{$foo}<br>
```

The template above includes the example `sub_template.tpl` below:

```smarty

{* foo will be known also in the including template *}
{assign var="foo" value="something" scope=parent}
{$foo="something" scope=parent}

{* bar is assigned only local in the including template *}
{assign var="bar" value="value"} {* or *}
{$var="value"}

```

You can assign a variable to root of the current root tree. The variable
is seen by all templates using the same root tree.

```smarty
{assign var=foo value="bar" scope="root"}
```
      
A global variable is seen by all templates.

```smarty
{assign var=foo value="bar" scope="global"} {* or *}
{assign "foo" "bar" scope="global"} {* short-hand, or *}
{$foo="bar" scope="global"}
```
 
To access `{assign}` variables from a php script use
[`getTemplateVars()`](../../programmers/api-functions/api-get-template-vars.md). 
Here's the template that creates the variable `$foo`.

```smarty
{assign var="foo" value="Smarty"} {* or *}
{$foo="Smarty"}
```

The template variables are only available after/during template
execution as in the following script.

```php
<?php

// this will output nothing as the template has not been executed
echo $smarty->getTemplateVars('foo');

// fetch the template to a variable
$whole_page = $smarty->fetch('index.tpl');

// this will output 'smarty' as the template has been executed
echo $smarty->getTemplateVars('foo');

$smarty->assign('foo','Even smarter');

// this will output 'Even smarter'
echo $smarty->getTemplateVars('foo');
```

The following functions can also *optionally* assign template variables: [`{capture}`](#language.function.capture),
[`{include}`](#language.function.include),
[`{insert}`](#language.function.insert),
[`{counter}`](#language.function.counter),
[`{cycle}`](#language.function.cycle),
[`{eval}`](#language.function.eval),
[`{fetch}`](#language.function.fetch),
[`{math}`](#language.function.math) and
[`{textformat}`](#language.function.textformat).

See also [`{append}`](./language-function-append.md),
[`assign()`](#api.assign) and
[`getTemplateVars()`](#api.get.template.vars).