Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

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

Check reviews of PHP Web Hosting, compatible with Smarty Templates

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy wedding dresses on SiteSteer.com - Best Online Wedding Dress Store

Candy Crush Cheats

Tired of playing games? Visit the Game Supply to Sell World of Warcraft Accounts.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Resources

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


<?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 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


<?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'}

     

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


<?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'}


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

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'}

     

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


<?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';
?>

     

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 20 plus 4? (Are you human?)

Sponsors [info]

UK Web Hosting @webhost.uk.net
Best Web Hosting @rshosting.com
Web Hosting UK @webhostinguk.com
Unlimited Web Hosting @infrenion.com
App Entwicklung @morphodo.com
Buy from China @dhgate.com
dissertationteam.com
Buy Web Hosting @buy-hosting.net
WordPress Hosting Hub @wphostinghub.net