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:
I template possono arrivare da varie risorse. Quando fate la display o la fetch di un template, o quando fate la include da un altro template, fornite un tipo di risorsa, seguito dal percorso appropriato e dal nome del template. Se non viene esplicitamente indicato un tipo di risorsa, viene utilizzato il valore di $default_resource_type.
I template provenienti dalla $template_dir non hanno bisogno che indichiate un tipo di risorsa, sebbene possiate indicare file: per coerenza. Basta che forniate il percorso per il template che volete usare, relativo alla directory radice $template_dir.
Example 15.6. uso dei template della $template_dir
<?php $smarty->display("index.tpl"); $smarty->display("admin/menu.tpl"); $smarty->display("file:admin/menu.tpl"); // equivale al precedente ?>
And from within Smarty template:
{include file="index.tpl"} {include file="file:index.tpl"} {* equivale al precedente *}
I template che si trovano al di fuori della $template_dir richiedono obbligatoriamente che indichiate il tipo di risorsa file: seguito dal percorso assoluto e dal nome del template.
Example 15.7. uso dei template da qualsiasi directory
<?php $smarty->display("file:/export/templates/index.tpl"); $smarty->display("file:/path/to/my/templates/menu.tpl"); ?>
And from within Smarty template:
{include file="file:/usr/local/share/templates/navigation.tpl"}
Se usate una macchina Windows, i percorsi di solito comprendono una lettera di drive (C:) all'inizio del percorso. Accertatevi di usare "file:" nel path, per evitare conflitti di namespace e ottenere i risultati voluti.
Example 15.8. uso di template da percorsi di Windows
<?php $smarty->display("file:C:/export/templates/index.tpl"); $smarty->display("file:F:/path/to/my/templates/menu.tpl"); ?> {* dall'interno di un template *} {include file="file:D:/usr/local/share/templates/navigation.tpl"}
Potete ottenere template da qualsiasi risorsa alla quale sia possibile accedere con PHP: database, socket, directory LDAP, e così via. Potete farlo scrivendo una funzione plugin per le risorse e registrandola a Smarty.
Consultate la sezione plugin risorse per maggiori informazioni sulle funzioni che dovrete creare.
Notate che non è possibile modificare il comportamento della risorsa
file
, ma potete fornire una risorsa che legge i
template dal filesystem in maniera diversa registrandola con un altro
nome di risorsa.
Example 15.9. uso di risorse personalizzate
<?php // mettete queste funzioni da qualche parte nell'applicazione function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj) { // fate qui le chiamate al database per ottenere il template // e riempire $tpl_source $sql = new SQL; $sql->query("select tpl_source from my_table where tpl_name='$tpl_name'"); if ($sql->num_rows) { $tpl_source = $sql->record['tpl_source']; return true; } else { return false; } } function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) { // qui facciamo una chiamata al db per riempire $tpl_timestamp. $sql = new SQL; $sql->query("select tpl_timestamp from my_table where tpl_name='$tpl_name'"); if ($sql->num_rows) { $tpl_timestamp = $sql->record['tpl_timestamp']; return true; } else { return false; } } function db_get_secure($tpl_name, &$smarty_obj) { // ipotizziamo che tutti i template siano sicuri return true; } function db_get_trusted($tpl_name, &$smarty_obj) { // non usata per i template } // register the resource name "db" $smarty->register_resource("db", array("db_get_template", "db_get_timestamp", "db_get_secure", "db_get_trusted")); // uso della risorsa dallo script php $smarty->display("db:index.tpl"); ?>
And from within Smarty template:
{include file="db:/extras/navigation.tpl"}
Potete specificare una funzione da usare per ottenere i contenuti del template nel caso in cui non sia possibile leggerlo dalla risorsa appropriata. Un uso possibile di questa funzione è di creare al volo template che non esistono.
Example 15.10. uso della funzione di gestione dei template di default
<?php // mettete questa funzione da qualche parte nell'applicazione function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj) { if( $resource_type == 'file' ) { if ( ! is_readable ( $resource_name )) { // create il file del template e restituite il contenuto. $template_source = "This is a new template."; $template_timestamp = time(); $smarty_obj->_write_file($resource_name,$template_source); return true; } } else { // non è un file return false; } } // impostate il gestore di default $smarty->default_template_handler_func = 'make_template'; ?>