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

{function}

{function} is used to create functions within a template and call them just like a plugin function. Instead of writing a plugin that generates presentational content, keeping it in the template is often a more manageable choice. It also simplifies data traversal, such as deeply nested menus.

Note

Template functions are defined global. Since the Smarty compiler is a single-pass compiler, The {call} tag must be used to call a template function defined externally from the given template. Otherwise you can directly use the function as {funcname ...} in the template.

  • The {function} tag must have the name attribute which contains the the name of the template function. A tag with this name can be used to call the template function.

  • Default values for variables can be passed to the template function as attributes. Like in PHP function declarations you can only use scalar values as default. The default values can be overwritten when the template function is being called.

  • You can use all variables from the calling template inside the template function. Changes to variables or new created variables inside the template function have local scope and are not visible inside the calling template after the template function is executed.

Attributes:

Attribute Name Type Required Default Description
name string Yes n/a The name of the template function
[var ...] [var type] No n/a default variable value to pass local to the template function

Note

You can pass any number of parameter to the template function when it is called. The parameter variables must not be declared in the {funcname ...} tag unless you what to use default values. Default values must be scalar and can not be variable. Variables must be passed when the template is called.

Example 7.43. Recursive menu {function} example


{* define the function *}
{function name=menu level=0}
{function menu level=0}          {* short-hand *}
  <ul class="level{$level}">
  {foreach $data as $entry}
    {if is_array($entry)}
      <li>{$entry@key}</li>
      {menu data=$entry level=$level+1}
    {else}
      <li>{$entry}</li>
    {/if}
  {/foreach}
  </ul>
{/function}

{* create an array to demonstrate *}
{$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' =>
['item3-3-1','item3-3-2']],'item4']}

{* run the array through the function *}
{menu data=$menu}

  

Will generate the following output


* item1
* item2
* item3
      o item3-1
      o item3-2
      o item3-3
            + item3-3-1
            + item3-3-2
* item4

  

See also {call}

Comments
by theHacker on Apr 1, 2013 at 7:23
You cannot use 'name' as an attribute for your function.
by Roland on Dec 2, 2011 at 5:42
Just a slight correction: the nesting of the <li>-elements in the function doesn´t validate correctly. Instead of <li>{$entry@key}</li> {menu data=$entry level=$level+1} it should be <li>{$entry@key} {menu data=$entry level=$level+1}</li> Apart from that: Brilliant feature ;-)
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 8 plus 16? (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