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

Shop high quality cheap prom dresses on Dresswe.co.uk

Advertisement

Chapter 8. Custom Functions

Smarty comes with several custom functions that you can use in the templates.

{assign}

{assign} is used for assigning template variables during the execution of a template.

Attribute Name Type Required Default Description
var string Yes n/a The name of the variable being assigned
value string Yes n/a The value being assigned

Example 8.1. {assign}


{assign var='name' value='Bob'}

The value of $name is {$name}.

  

The above example will output:


The value of $name is Bob.

  

Example 8.2. {assign} with some maths

This complex example must have the variables in `backticks`


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

  

Example 8.3. Accessing {assign} variables from a PHP script

To access {assign} variables from a php script use get_template_vars(). Here's the template that creates the variable $foo.


{assign var='foo' value='Smarty'}

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


<?php

// this will output nothing as the template has not been executed
echo $smarty->get_template_vars('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->get_template_vars('foo');

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

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

?>


The following functions can also optionally assign template variables.

{capture}, {include}, {include_php}, {insert}, {counter}, {cycle}, {eval}, {fetch}, {math}, {textformat}

See also assign() and get_template_vars().

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 18 plus 4? (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