Overview
KB
Learn PHP
PHP techniques
PHP Smarty Functions
PHP Web Email
Secure development
PHP Security Scenario
SSH and SFTP
PHP Smarty
PHP Smarty Modifiers
PHP Smarty Caching
PHP Smarty Misc
CakePHP
Zend Framework
PHP basics

PHP Smarty Functions

Smarty Array Functions

Multidimensional arrays are accessible within a template using the dot syntax:
/* PHP code */ 
$array = array (array('name' => 'John', 'age' => 35), 
array('name' => 'Jane', 'age' => 42)); 
$page->assign('people', $array);    

{* template *} 
{foreach from=$people item=person}    

{$person.name} is {$person.age} years old.    

{/foreach}
The "foreachelse" function can be used if an array has no items in it (and therefore the loop is never executed):
{foreach from=$array item=v} 
    {$v} 
{foreachelse} 
    There are no items to print. 
{/foreach}

Using PHP Functions in Smarty

The "php" function allows you to place executable PHP code within a template. This may not be advisable, though, as this breaks the distinction between application code and presentation. Any block put within {literal}{/literal} tags will not be touched by Smarty. This is particularly useful for JavaScript and CSS, whose frequent use of the curly braces would otherwise confuse the Smarty template engine:
{literal}    
 

{/literal}
The "strip" function removes newlines and whitespace from enclosed template text. Conversely, the "strip" modifier removes whitespace from variable values.

Creating Custom Smarty Functions

Smarty has many custom functions and more can be added by creating your own plugin. See the Smarty manual for the complete list of custom functions.

Article continues:  <<previous page

 Download NuSphere PHP IDE

Download a free trial of the fast PHP EDitor and robust Integrated Development Environment for PHP.

 Buy NuSphere PhpED® now

Best PHP Editor and complete PHP IDE.
NuSphere PhpED 21.0 is available from our online store front.

 Special Team4 Offer

Get 4 copies of PhpED for the price of 3!

Optimum solution for development teams.
PhpED 21.0 Team4
Need more than 4 licenses? Contact Us for more quantity discounts, please use "Ordering/Payment issue" subject on the form.
"To be honest its bloody awesome, I have looked at loads of PHP editors and this is THE only one that actual works straight out of the box!!! Brilliant, well done."
Andrew Breward,
Director of Technology
caboodal.com