flatpress/fp-includes/smarty-4.4.1/docs/programmers/plugins/plugins-compiler-functions.md
Fraenkiman e544ed6d9a Smatry Release 4.4.1 on Feb-2024
Comparing changes: https://github.com/smarty-php/smarty/compare/v4.3.1...v4.4.1

It is noticeable that Smarty 4.3.1 does not officially support PHP 8.3. Is only supported with 4.4.0.

Remark:

During tests with Smarty 4.5.1, it was noticed that the following warning occurs:
Deprecated: Using the unregistered function "function_exists" in a template is deprecated and will be removed in a future version. Use Smarty::registerPlugin to explicitly register a custom modifier.

As of Smarty 5.X.X, templates must be revised again.
The Smarty release 5.0.2 is already officially available. However, integration into FlatPress is not entirely trivial.
2024-04-14 18:37:39 +02:00

1.7 KiB

Compiler Functions

Compiler functions are called only during compilation of the template. They are useful for injecting PHP code or time-sensitive static content into the template. If there is both a compiler function and a custom function registered under the same name, the compiler function has precedence.

mixed

smarty_compiler_

name

array

$params

object

$smarty

The compiler function is passed two parameters: the params array which contains precompiled strings for the attribute values and the Smarty object. It's supposed to return the code to be injected into the compiled template including the surrounding PHP tags.

<?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($params, Smarty $smarty)
{
    return "<?php\necho '" . $smarty->_current_file . " compiled at " . date('Y-m-d H:M'). "';\n?>";
}
?>

This function can be called from the template as:

{* this function gets executed at compile time only *}
{tplheader}

The resulting PHP code in the compiled template would be something like this:

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

See also registerPlugin(), unregisterPlugin().