0

I want to pass a variable to a function:

{{#myFunc}}{{someVar}}{{/myFunction}}

This is my engine:

$options = [
    'helpers' => [
        'myFunc' => function($value) {
            // This works
            return 'got value: ' . $value;

            // This does not work
            return SomeClass::reformat($value);
            // It passes "{{someVar}}" to the method, not the value

            // var_dump shows
            var_dump($value); // '{{someVar}}'
        }
    ]
];
$engine = new Mustache_Engine($options);

$rendered = $engine->render('{{#myFunc}}{{someVar}}{{/myFunction}}', ['someVar' => '12345']);

What do I do?

Kousha
  • 32,871
  • 51
  • 172
  • 296

1 Answers1

-1

You need to use a lambda, here you can see an example:

//the data
$data = array(
    'someVar' => "12345",
    'myFunc' => function($var) {
        return "got Value:" . $var;
    }
);
$engine = new Mustache_Engine();
$rendered = $engine->render('{{#myFunc}}{{someVar}}{{/myFunc}}', $data);

the function is inside the $data.

David Lee
  • 811
  • 7
  • 19