Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Find Wedding Dresses Online at Canada from Ca-dresses.com

Find your local domestic cleaner

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Advertisement

{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 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 Type Required Default Description
name string Yes n/a The name of the captured block
assign string No n/a The variable name where to assign the captured output to
append string No n/a The name of an array variable where to append the captured output to

Option Flags:

Name Description
nocache Disables caching of this captured block

Caution

Be careful when capturing {insert} output. If you have $caching enabled and you have {insert} commands that you expect to run within cached content, do not capture this content.

Example 7.21. {capture} with the name attribute


{* 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}

   


Example 7.22. {capture} into a template variable

This example demonstrates the capture function.


{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>

     


Example 7.23. {capture} into a template array variable

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


{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, {eval}, {fetch}, fetch() and {assign}.

Comments
by Max-P on Jun 18, 2012 at 7:21
Just a quick note because I searched for it for a loooong time. The {capture} block is being "executed" before it is captured and put into the {$smarty.capture.xxx} variable.Therefore, if you try to use it in a loop it will fail. Instead of that, one can use {function} to keep the block inside the same template file, or {include} if it is large enought to put it in another template file ;)
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 3 plus 0? (Are you human?)

Advertisement

Sponsors [info]

UK Web Hosting @webhost.uk.net
Best Web Hosting @rshosting.com
Web Hosting UK @webhostinguk.com
Unlimited Web Hosting @infrenion.com
App Entwicklung @morphodo.com
First Click Internet Marketing @fcinternetmarketing.com
Credit Card Processing Company