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


Smarty - the compiling PHP template engine

Monte Ohrt <monte at ohrt dot com>

Andrei Zmievski <>


Table of Contents

I. Getting Started
1. What is Smarty?
2. Installation
Basic Installation
Extended Setup
II. Smarty For Template Designers
3. Basic Syntax
Embedding Vars in Double Quotes
Escaping Smarty Parsing
4. Variables
Variables assigned from PHP
Associative arrays
Array indexes
Variables loaded from config files
{$smarty} reserved variable
Request variables
{$smarty.section}, {$smarty.foreach}
{$smarty.ldelim}, {$smarty.rdelim}
5. Variable Modifiers
6. Combining Modifiers
7. Built-in Functions
8. Custom Functions
9. Config Files
10. Debugging Console
III. Smarty For Programmers
11. Constants
12. Smarty Class Variables
13. Smarty Class Methods()
append() — append an element to an assigned array
append_by_ref() — append values by reference
assign() — assign values to the templates
assign_by_ref() — assign values by reference
clear_all_assign() — clears the values of all assigned variables
clear_all_cache() — clears the entire template cache
clear_assign() — clears the value of an assigned variable
clear_cache() — clears the cache for a specific template
clear_compiled_tpl() — clears the compiled version of the specified template resource
clear_config() — clears assigned config variables
config_load() — loads config file data and assigns it to the template
display() — displays the template
fetch() — returns the template output
get_config_vars() — returns the given loaded config variable value
get_registered_object() — returns a reference to a registered object
get_template_vars() — returns assigned variable value(s)
is_cached() — returns true if there is a valid cache for this template
load_filter() — load a filter plugin
register_block() — dynamically register block functions plugins
register_compiler_function() — dynamically register a compiler function plugin
register_function() — dynamically register template function plugins
register_modifier() — dynamically register modifier plugin
register_object() — register an object for use in the templates
register_outputfilter() — dynamically register outputfilters
register_postfilter() — dynamically register postfilters
register_prefilter() — dynamically register prefilters
register_resource() — dynamically register resources
trigger_error() — output an error message
template_exists() — checks whether the specified template exists
unregister_block() — dynamically unregister block function plugins
unregister_compiler_function() — dynamically unregister a compiler function
unregister_function — dynamically unregister template function plugin
unregister_modifier() — dynamically unregister modifier plugin
unregister_object() — dynamically unregister an object
unregister_outputfilter() — dynamically unregister an output filter
unregister_postfilter() — dynamically unregister a postfilter
unregister_prefilter() — dynamically unregister a prefilter
unregister_resource() — dynamically unregister a resource plugin
14. Caching
Setting Up Caching
Multiple Caches Per Page
Cache Groups
Controlling Cacheability of Plugins' Output
15. Advanced Features
Output Filters
Cache Handler Function
Templates from $template_dir
Templates from any directory
Templates from other sources
Default template handler function
16. Extending Smarty With Plugins
How Plugins Work
Naming Conventions
Writing Plugins
Template Functions
Block Functions
Compiler Functions
Output Filters
IV. Appendixes
17. Troubleshooting
Smarty/PHP errors
18. Tips & Tricks
Blank Variable Handling
Default Variable Handling
Passing variable title to header template
Componentized Templates
Obfuscating E-mail Addresses
19. Resources
20. BUGS

List of Examples

2.1. Required Smarty library files
2.2. Set SMARTY_DIR constant manually
2.3. Supply absolute path to library file
2.4. Add the library path to the php.ini file
2.5. Appending the include path in a php script with ini_set()
2.6. What the file structure looks like
2.7. Permissions and making directories writable
2.8. /web/
2.9. Editing /web/
2.10. /php/includes/guestbook/setup.php
2.11. /web/
3.1. Comments within a template
3.2. Variables
3.3. function syntax
3.4. function attribute syntax
3.5. Syntax examples
3.6. Practical examples
3.7. math examples
3.8. changing delimiters example
4.1. Example variables
4.2. Assigned variables
4.3. Accessing associative array variables
4.4. Accessing arrays by index
4.5. Accessing object properties
4.6. config variables
4.7. Displaying request variables
5.1. Modifier examples
5.2. capitalize
5.3. cat
5.4. count_characters
5.5. count_paragraphs
5.6. count_sentences
5.7. count_words
5.8. date_format
5.9. default
5.10. escape
5.11. Other examples
5.12. indent
5.13. lower
5.14. nl2br
5.15. regex_replace
5.16. replace
5.17. spacify
5.18. string_format
5.19. strip
5.20. strip_tags
5.21. truncate
5.22. upper
5.23. wordwrap
6.1. combining modifiers
7.1. {capture} with the name attribute
7.2. {capture} into a template variable
7.3. {config_load}
7.4. function {config_load} with section
7.5. The item attribute
7.6. Demonstrates the item and key attributes
7.7. {foreach} with associative item attribute
7.8. {foreach} with nested item and key
7.9. Database example with {foreachelse}
7.10. index example
7.11. iteration and index example
7.12. first property example
7.13. last property example
7.14. total property example
7.15. {if} statements
7.16. {if} with more examples
7.17. Simple {include} example
7.18. {include} passing variables
7.19. {include} and assign to variable
7.20. Various {include} resource examples
7.21. function {include_php}
7.22. function {insert}
7.23. {ldelim}, {rdelim}
7.24. Another Javascript example
7.25. {literal} tags
7.26. Javascript function example
7.27. Some css style in a template
7.28. php code within {php} tags
7.29. {php} tags with global and assigning a variable
7.30. Looping a simple array with {section}
7.31. {section} without an assigned array
7.32. Naming a {section}
7.33. Looping an associative array with {section}
7.34. {section} demonstrating the loop variable
7.35. Nested {section}'s
7.36. Database example with a {sectionelse}
7.37. {section} index property
7.38. index, index_next and index_prev properties
7.39. A section's iteration property
7.40. {section} property first and last
7.41. {section} property loop
7.42. show property
7.43. total property example
7.44. {strip} tags
8.1. {assign}
8.2. {assign} with some maths
8.3. Accessing {assign} variables from a PHP script
8.4. {counter}
8.5. {cycle}
8.6. {eval}
8.7. Another {eval} example
8.8. {fetch} examples
8.9. {html_checkboxes}
8.10. Database example (eg PEAR or ADODB):
8.11. {html_image} example
8.12. Associative array with the options attribute
8.13. Dropdown with seperate arrays for values and ouptut
8.14. Database example (eg ADODB or PEAR)
8.15. Dropdown's with <optgroup>
8.16. {html_radios} first example
8.17. {html_radios} second example
8.18. {html_radios} - Database example (eg PEAR or ADODB):
8.19. {html_select_date}
8.20. {html_select_date} second example
8.21. {html_select_time}
8.22. {html_table}
8.23. {mailto} example lines followed by the result
8.24. {math}
8.25. {textformat}
9.1. Example of config file syntax
12.1. Appending a local plugin dir
12.2. Multiple $plugins_dir
12.3. $debugging_ctrl on localhost
12.4. Array of config #variables#
12.5. $secure_dir example
12.6. $compile_id in a virtual host enviroment
13.1. append
13.2. append_by_ref
13.3. assign()
13.4. assign_by_ref()
13.5. clear_all_assign()
13.6. clear_all_cache
13.7. clear_assign()
13.8. clear_cache()
13.9. clear_compiled_tpl()
13.10. clear_config()
13.11. config_load()
13.12. display()
13.13. Other display() template resource examples
13.14. fetch()
13.15. Using fetch() to send an email
13.16. get_config_vars()
13.17. get_registered_object()
13.18. get_template_vars
13.19. is_cached()
13.20. is_cached() with multiple-cache template
13.21. Loading filter plugins
13.22. register_block()
13.23. register_function()
13.24. register_modifier()
13.25. register_resource()
13.26. template_exists()
13.27. unregister_function
13.28. unregister_modifier()
13.29. unregister_resource()
14.1. Enabling caching
14.2. Setting $cache_lifetime per cache
14.3. Enabling $compile_check
14.4. Using is_cached()
14.5. Clearing the cache
14.6. Passing a $cache_id to display()
14.7. Passing a cache_id to is_cached()
14.8. Clearing all caches for a particular $cache_id
14.9. $cache_id groups
14.10. Preventing a plugin's output from being cached
14.11. Preventing a whole passage of a template from being cached
15.1. Using a registered or assigned object
15.2. Using a template prefilter
15.3. Using a template postfilter
15.4. Using a template outputfilter
15.5. Example using MySQL as a cache source
15.6. Using templates from the $template_dir
15.7. Using templates from any directory
15.8. Using templates from windows file paths
15.9. Using custom resources
15.10. Using the default template handler function
16.1. function plugin with output
16.2. function plugin without output
16.3. A simple modifier plugin
16.4. More complex modifier plugin
16.5. block function
16.6. A simple compiler function
16.7. prefilter plugin
16.8. postfilter plugin
16.9. An output filter plugin
16.10. resource plugin
16.11. insert plugin
17.1. Smarty errors
17.2. PHP parsing errors
17.3. Other common errors
18.1. Printing &nbsp; when a variable is empty
18.2. Assigning a template variable its default value
18.3. Passing the title variable to the header template
18.4. Using date_format
18.5. Converting form date elements back to a timestamp
18.6. Using {insert} to write a WML Content-Type header
18.7. componentized template
18.8. Example of template the Obfuscating an email address