Comunitatea PHP Romania
 

 
Custom Functions

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 NameTypeRequiredDefaultDescription
varstringYesn/aThe name of the variable being assigned
valuestringYesn/aThe 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().

Ultimele discutii in forum RSS Forum

Ultimele articole Ultimele articole

Top membri

Pirahna Pirahna
acasa , in pat
carco carco
Bucuresti
Birkoff Birkoff
Bucuresti
Mascka Mascka
Braila
raul_ raul_
whooper whooper
Toronto ON
mihaitha mihaitha
Sibiu
gabysolomon gabysolomon
Bacau
oriceon oriceon
Constanta
garlicinicolae garlicinicolae

Newsletter


Email:
 inscriere
 renuntare
 
 Arhiva newsletter

Parteneriat

Copyright © 2001-2008 PHP Romania Add PHPRomania to Google Add PHPRomania to Del.icio.us Add PHPRomania to Stumbleupon Add PHPRomania to Yahoo! Add PHPRomania to Digg Add PHPRomania to Blink Fast Loans | Remortgages | Mortgage | Layouts Myspace | Final Fantasy
Ads: Partener Way2Web Nework: gazduire web | inregistrare domenii | web design | imobiliare | web hosting
Powered by Simplis