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

Advertisement

Compiler-Funktionen

Compiler-Funktionen werden während der Kompilierung des Template aufgerufen. Das ist nützlich, um PHP-Code oder zeitkritische statische Inhalte in ein Template einzufügen. Sind eine Compiler-Funktion und eine eigene Funktion unter dem selben Namen registriert, wird die Compiler-Funktion ausgeführt.

mixed smarty_compiler_name( $tag_arg,  
  &$smarty);  
string $tag_arg;
object &$smarty;
 

Die Compiler-Funktion erhält zwei Parameter: die Tag-Argument Zeichenkette - also alles ab dem Funktionsnamen bis zum schliessenden Trennzeichen - und das Smarty Objekt. Gibt den PHP-Code zurück, der in das Template eingefügt werden soll.

Sehen Sie dazu: register_compiler_function(), unregister_compiler_function().

Example 16.6. Einfache Compiler-Funktionen


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     compiler.tplheader.php
 * Type:     compiler
 * Name:     tplheader
 * Purpose:  Output header containing the source file name and
 *           the time it was compiled.
 * -------------------------------------------------------------
 */
function smarty_compiler_tplheader($tag_arg, &$smarty)
{
    return "\necho '" . $smarty->_current_file . " compiled at " . date('Y-m-d H:M'). "';";
}
?>

     

Diese Funktion kann aus dem Template wie folgt aufgerufen werden:

      
      {* diese Funktion wird nur zum Kompilier-Zeitpunkt ausgeführt *}
      {tplheader}

Der resultierende PHP-Code würde ungefähr so aussehen:


<?php
echo 'index.tpl compiled at 2002-02-20 20:02';
?>