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



The templates may come from a variety of sources. When you display() or fetch() a template, or when you include a template from within another template, you supply a resource type, followed by the appropriate path and template name. If a resource is not explicitly given the value of $default_resource_type is assumed.

Templates from $template_dir

Templates from the $template_dir do not require a template resource, although you can use the file: resource for consistancy. Just supply the path to the template you want to use relative to the $template_dir root directory.

Example 15.6. Using templates from the $template_dir

$smarty->display('file:admin/menu.tpl'); // same as one above

From within a Smarty template

{include file='index.tpl'}
{* below is same as above *}
{include file='file:index.tpl'}

Templates from any directory

Templates outside of the $template_dir require the file: template resource type, followed by the absolute path to the template.

Example 15.7. Using templates from any directory



And from within a Smarty template:

{include file='file:/usr/local/share/templates/navigation.tpl'}


Windows Filepaths

If you are using a Windows machine, filepaths usually include a drive letter (C:) at the beginning of the pathname. Be sure to use file: in the path to avoid namespace conflicts and get the desired results.

Example 15.8. Using templates from windows file paths



And from within Smarty template:

{include file='file:D:/usr/local/share/templates/navigation.tpl'}

Templates from other sources

You can retrieve templates using whatever possible source you can access with PHP: databases, sockets, LDAP, and so on. You do this by writing resource plugin functions and registering them with Smarty.

See resource plugins section for more information on the functions you are supposed to provide.


Note that you cannot override the built-in file: resource, but you can provide a resource that fetches templates from the file system in some other way by registering under another resource name.

Example 15.9. Using custom resources

// put these function somewhere in your application
function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
    // do database call here to fetch your template,
    // populating $tpl_source with actual template contents
    $tpl_source = "This is the template text";
    // return true on success, false to generate failure notification
    return true;

function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
    // do database call here to populate $tpl_timestamp
    // with unix epoch time value of last template modification.
    // This is used to determine if recompile is necessary.
    $tpl_timestamp = time(); // this example will always recompile!
    // return true on success, false to generate failure notification
    return true;

function db_get_secure($tpl_name, &$smarty_obj)
    // assume all templates are secure
    return true;

function db_get_trusted($tpl_name, &$smarty_obj)
    // not used for templates

// register the resource name "db"
$smarty->register_resource("db", array("db_get_template",

// using resource from php script


And from within Smarty template:

{include file='db:/extras/navigation.tpl'}


Default template handler function

You can specify a function that is used to retrieve template contents in the event the template cannot be retrieved from its resource. One use of this is to create templates that do not exist on-the-fly.

Example 15.10. Using the default template handler function

// put this function somewhere in your application

function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp,
	if( $resource_type == 'file' ) {
		if ( ! is_readable ( $resource_name )) {
			// create the template file, return contents.
			$template_source = "This is a new template.";
            require_once SMARTY_CORE_DIR . 'core.write_file.php';
            smarty_core_write_file( array( 'filename'=>$smarty_obj->template_dir . DIRECTORY_SEPARATOR . $resource_name, 'contents'=>$template_source ), $smarty_obj ); 
			return true;
    } else {
		// not a file
		return false;

// set the default handler
$smarty->default_template_handler_func = 'make_template';