Defining Your Own Template Function

Applies to: Jamroom 7.0+ Last verified: 2026-10-24
  • A function in your module

    A module can add its own template functions. Define a PHP function named smarty_function_ followed by the name you'll use in templates, in your module's include.php:

    /**
     * Show a greeting
     * @param array $params Template parameters
     * @param object $smarty Smarty object
     * @return string
     */
    function smarty_function_xxHello_greeting($params, $smarty)
    {
        if (empty($params['name'])) {
            return jrCore_smarty_missing_error('name');
        }
        $out = 'Hello, ' . jrCore_entity_string($params['name']) . '!';
        if (!empty($params['assign'])) {
            $smarty->assign($params['assign'], $out);
            return '';
        }
        return $out;
    }
  • Templates can then use:

    {xxHello_greeting name=$profile_name}
  • Conventions

    • Name it after your module: (module)_(name), so it can't clash with another module's.
    • Parameters arrive in $params. Check required ones, and return jrCore_smarty_missing_error('name') when one is missing, like the core functions.
    • Support assign, so templates can keep the result for later.
    • Escape values you output, with jrCore_entity_string().
    • Return the output, don't echo it.

    For a function other skins may use, tell them to call it through {jrCore_module_function}, so their templates still work without your module.

    See the Developer Guide chapter for building modules.

Tags