summaryrefslogtreecommitdiff
path: root/docs/designers/language-builtin-functions/language-function-capture.md
blob: 83f4d02d70d67c6805c06f237ba6122317bcedb8 (plain)
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
# {capture}

`{capture}` is used to collect the output of the template between the
tags into a variable instead of displaying it. Any content between
`{capture name='foo'}` and `{/capture}` is collected into the variable
specified in the `name` attribute.

The captured content can be used in the template from the variable
[`$smarty.capture.foo`](../language-variables/language-variables-smarty.md#smartycapture-languagevariablessmartycapture) where "foo"
is the value passed in the `name` attribute. If you do not supply the
`name` attribute, then "default" will be used as the name ie
`$smarty.capture.default`.

`{capture}'s` can be nested.

## Attributes

| Attribute Name | Required | Description                                                          |
|----------------|----------|----------------------------------------------------------------------|
| name           | Yes      | The name of the captured block                                       |
| assign         | No       | The variable name where to assign the captured output to             |
| append         | No       | The name of an array variable where to append the captured output to |

## Option Flags

| Name    | Description                             |
|---------|-----------------------------------------|
| nocache | Disables caching of this captured block |


## Examples

```smarty
{* we don't want to print a div tag unless content is displayed *}
{capture name="banner"}
{capture "banner"} {* short-hand *}
  {include file="get_banner.tpl"}
{/capture}

{if $smarty.capture.banner ne ""}
<div id="banner">{$smarty.capture.banner}</div>
{/if}
```
      
This example demonstrates the capture function.
```smarty

{capture name=some_content assign=popText}
{capture some_content assign=popText} {* short-hand *}
The server is {$my_server_name|upper} at {$my_server_addr}<br>
Your ip is {$my_ip}.
{/capture}
<a href="#">{$popText}</a>
```
         

This example also demonstrates how multiple calls of capture can be used
to create an array with captured content.

```smarty
{capture append="foo"}hello{/capture}I say just {capture append="foo"}world{/capture}
{foreach $foo as $text}{$text} {/foreach}
```

The above example will output:

```
I say just hello world
```
      

See also [`$smarty.capture`](../language-variables/language-variables-smarty.md#smartycapture-languagevariablessmartycapture),
[`{eval}`](../language-custom-functions/language-function-eval.md),
[`{fetch}`](../language-custom-functions/language-function-fetch.md), [`fetch()`](../../programmers/api-functions/api-fetch.md) and
[`{assign}`](./language-function-assign.md).