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 Modifiers

Escaping Values

The "escape" modifier encodes or escapes values. The first argument indicates the type of escaping to perform, with "html" as the default. The options are:
Click for full image of the escape modifier result
html
Turns & " ' < > into entity equivalents.
htmlall
Turns all HTML tags into entity equivalents.
url
Makes the value safe to pass in a URL (like PHP's urlencode() function).
quotes
Escapes quotes by preceding them with the backslash.
hex
Turns characters into their hex equivalent.
hexentity
Turns characters into their hex entity equivalent. (see the image at right)
javascript
Uses JavaScript to help hide how the value looks in the HTML source code.
mail
Turns 'something@example.com' into 'something [AT] example [DOT] com'
For protecting e-mail addresses, also see Obfuscating E-mail Addresses. The "strip_tags" modifier removes all HTML tags entirely.

Adjusting Spacing

The "indent" modifier indents lines 4 spaces (you can change this default indent). The "nl2br" modifier converts newline characters to HTML break tags. The "spacify" modifier inserts a space between every character in a value. Its one parameter allows you to change what character (or sequence of characters) is inserted. The "strip" modifier removes repeated spacing characters (spaces, newlines, and tabs) with a single space. The "word_wrap" modifier wraps a string to a set length. It's equivalent to PHP's wordwrap() function.

Replacing Values using Smarty Replace Modifier

The "replace" modifier performs a simple string replacement on a value, like PHP's str_replace() function. The following replaces uses of 'foo' with 'bar':
{$var|replace:'foo':'bar'}
The "regex_replace" modifier allows you to apply Perl-compatible regular expressions to values. It's equivalent to PHP's preg_replace() function.

Formatting Strings

The "string_format" modifier is the equivalent of PHP's sprintf() function, used to format a string. The "truncate" modifier truncates a string to a length. By default, this modifier truncates a string to approximately 80 characters, ending at a word, and adds an ellipsis (...) to the value. "truncate" is an obvious tool for putting a preview of a longer bit of text on a page.

Modifying Arrays

The @ symbol applies a modifier to an entire array, rather than to each element in the array.
{$some_array|@lower}

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