Files
smarty/docs/designers/language-builtin-functions/language-function-capture.md

82 lines
2.8 KiB
Markdown
Raw Permalink Normal View History

2023-02-05 23:14:10 +01:00
# {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
2023-02-05 23:14:10 +01:00
[`$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.
2023-02-05 23:14:10 +01:00
## Attributes
2023-02-05 23:14:10 +01:00
| 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 |
2023-02-05 23:14:10 +01:00
## Option Flags
2023-02-05 23:14:10 +01:00
| Name | Description |
|---------|-----------------------------------------|
| nocache | Disables caching of this captured block |
> **Note**
>
> Be careful when capturing [`{insert}`](#language.function.insert)
> output. If you have [`$caching`](#caching) enabled and you have
> [`{insert}`](#language.function.insert) commands that you expect to
> run within cached content, do not capture this content.
2023-02-05 23:14:10 +01:00
## Examples
2023-02-05 23:14:10 +01:00
```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}
2023-02-05 23:14:10 +01:00
{if $smarty.capture.banner ne ""}
<div id="banner">{$smarty.capture.banner}</div>
{/if}
```
This example demonstrates the capture function.
2023-02-05 23:14:10 +01:00
```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.
2023-02-05 23:14:10 +01:00
```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:
2023-02-05 23:14:10 +01:00
```
I say just hello world
```
2023-02-05 23:14:10 +01:00
See also [`$smarty.capture`](../language-variables/language-variables-smarty.md#smartycapture-languagevariablessmartycapture),
2023-02-06 14:42:31 +01:00
[`{eval}`](../language-custom-functions/language-function-eval.md),
[`{fetch}`](../language-custom-functions/language-function-fetch.md), [`fetch()`](../../programmers/api-functions/api-fetch.md) and
2023-02-05 23:14:10 +01:00
[`{assign}`](./language-function-assign.md).