V
V
v-grabko2016-02-03 10:38:44
symfony
v-grabko, 2016-02-03 10:38:44

Why doesn't the twig extension work correctly?

function views($name, $array = []) {
    $twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__ . '/../app/resources/views'), [
            'cache' => __DIR__ . '/../storage/cache/views'
        ]);
    $twig->addFunction(new Twig_SimpleTest('Lang', function ($key) {
        return \Lang::Get($key);
    }));
    return $twig->render($name, $array);
}

Exception thrown: A function must be an instance of Twig_FunctionInterface or Twig_SimpleFunction
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2016-02-03
@v-grabko

$twig->addFunction(new Twig_SimpleTest(...));
The error says - you need to create an object that implements the Twig_FunctionInterface interface or a Twig_SimpleFunction object. And you passed an object of the Twig_SimpleTest() class.
Maybe you have a syntax error? Twig_SimpleTest -> Twig_SimpleFunction

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question