What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
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 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
<?php $smarty->display('index.tpl'); $smarty->display('admin/menu.tpl'); $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 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
<?php $smarty->display('file:/export/templates/index.tpl'); $smarty->display('file:/path/to/my/templates/menu.tpl'); ?>
And from within a Smarty template:
{include file='file:/usr/local/share/templates/navigation.tpl'}
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
<?php $smarty->display('file:C:/export/templates/index.tpl'); $smarty->display('file:F:/path/to/my/templates/menu.tpl'); ?>
And from within Smarty template:
{include file='file:D:/usr/local/share/templates/navigation.tpl'}
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
<?php // 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", "db_get_timestamp", "db_get_secure", "db_get_trusted")); // using resource from php script $smarty->display("db:index.tpl"); ?>
And from within Smarty template:
{include file='db:/extras/navigation.tpl'}
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
<?php // put this function somewhere in your application function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj) { 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'; ?>