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:
register_function
void register_function(string name,
mixed impl,
bool cacheable,
mixed cache_attrs);
Si può usare questa funzione per registrare dinamicamente funzioni plugin per i template. Dovete fornire il nome della funzione di template, seguito dalla funzione PHP da richiamare che implementa tale funzione.
Il parametro impl
, contenente la funzione
callback, può avere uno dei seguenti valori: (a) una stringa
contenente il nome della funzione (b) un array nella forma
array(&$oggetto, $metodo)
, dove
&$oggetto
è il riferimento ad un
oggetto e $metodo
è una stringa contenente
il nome di un metodo (c) un array nella forma
array(&$classe, $metodo)
dove
$classe
è un nome di classe e
$metodo
è un metodo statico della
classe.
cacheable
e cache_attrs
possono essere omessi nella maggioranza dei casi. Consultate
Controllo della Cache per l'output dei Plugins
per capire come usarli.
Example 13.22. register_function
<?php $smarty->register_function("date_now", "print_current_date"); function print_current_date($params) { if(empty($params['format'])) { $format = "%b %e, %Y"; } else { $format = $params['format']; return strftime($format,time()); } } // ora potete usare questa funzione in Smarty per stampare la data attuale: {date_now} // oppure {date_now format="%Y/%m/%d"} per formattarla. ?>