Smarty plugin call.

Good afternoon everyone. šŸ™‚
I have the following question:
made several plugins (functions).
in the template I call from the following method:
{function_name}

Works great without any problems, but I can’t figure out how to call the function dynamically? that is, depending on some event, the corresponding function is called, and not the same one.


Answer 1, authority 100%

Got it. In this case, create a delegate plugin that will call the necessary function, depending on the passed parameter. Those. something like this:

function smarty_function_involute_performing($params, $template) {
    if (empty($params['plugin']) || !preg_match('/^[\w\d_]+$/', $params['plugin']) ) {
        trigger_error("assign: missing or incorrect 'plugin' parameter");
        return;
    }
    $function = 'smarty_function_'.$params['plugin'];
    unset($params['plugin']);
    return $function($params, $template);
}

Although, of course, I donā€™t understand why this might be needed in the template.


Answer 2

As far as I understand, you need to insert different content in one place on the page.
I usually do this PHP code in such cases:

$html = $smarty->fetch('  ');

then I pass this variable to the template engine again:

$smarty->assign("content",$html);

and only then I display this HTML block where necessary in the template.

With this approach, you can make a hierarchical structure of templates. And smart plugins can be used, for example like this:

You have the $_GET[‘PAGE’] parameter. You pass it to the template, and in the template you call the plugin, which, based on this parameter, calls PHP code, which already generates HTML code.

Where can it come in handy?
This can be useful when building the component architecture of the project. When you do not need to receive $_GET[‘page’] from the browser, but, for example, specify this parameter directly in the templates and thus connect a certain component on the page.