3

Can a modifier plugin for PHP Smarty access the Smarty object?

The block and function plugin types both take the current Smarty object as a parameter. i.e.

function smarty_function_NAME($params, Smarty_Internal_Template $Smarty) {...}
function smarty_block_NAME($params, $content, Smarty_Internal_Template $Smarty, &$repeat) {...}

But the modifier plugin does not. i.e.

function smarty_modifier_NAME($arg1, $arg2, ...) {...}

Is there any way to access the Smarty object from within a modifier?

I'd like to make a modifier that get the value of a variable name string, like so:

{$colorblue = '#0317e9'}
{$index = 'blue'}
{$_color = "color`$index`"|smarty_variable_value}
Martijn
  • 3,696
  • 2
  • 38
  • 64
  • 1
    Why do you want the smarty object for? If you need to get the $index part, you can do a `{$_variable = "variable\`$index\`"|smarty_variable_value:$index}` – periklis Feb 12 '15 at 07:47
  • I think my example wasn't clear enough. The idea was that there would be a variable named `variable1`. The index needn't be numeric either. I'll edit the question accordingly. – Martijn Feb 12 '15 at 11:37
  • I'm still not sure why you need the smarty object. You can pass any number of variables to your modifier, using the `:` operator: `{$_color = "color\`$index\`"|smarty_variable_value:$colorblue:$index:$var1:$var2}` – periklis Feb 12 '15 at 13:10

1 Answers1

0

I ended up creating a small get extension function to handle this:

function smarty_function_get($params, Smarty_Internal_Template $Smarty) {
    if (!isset($params['name'])) {
        return;
    }

    $value = $Smarty->getTemplateVars($params['name']);

    if (!isset($params['assign'])) {
        return $value;
    }

    $Smarty->assign($params['assign'], $value);
}

Call using

{get name="color`$index`" assign="_color"}
Martijn
  • 3,696
  • 2
  • 38
  • 64