0
votes

I'm trying to render a controller inside a twig template with this:

{{ render(url('contact_form')) }}

But I'm getting this error:

An exception has been thrown during the rendering of a template ("Unable to add function "asset" as extensions have already been initialized.")

I have a twig function called asset, like this:

$app['twig'] = $app->extend('twig', function ($twig, $app) {
    // add custom globals, filters, tags, ...

    $twig->addFunction(new \Twig_SimpleFunction('asset', function ($asset) use ($app) {
        return $app['request_stack']->getMasterRequest()->getBasepath().'/'.$asset;
    }));

    return $twig;
});

It seems that silex is trying to add, again, the asset function in the render().

I have no idea how to fix this.

2

2 Answers

1
votes

I had to ignore the error by wrapping it with a try/catch block for it to work. It's a dirty solution but worked.

try {
    $twig->addFunction(new \Twig_SimpleFunction('asset', function ($asset) use ($app) {
        return $app['request_stack']->getMasterRequest()->getBasepath().'/'.$asset;
    }));
} catch (Exception $e) {
    // do nothing
}
-1
votes

There is already a function for twig called asset you need to call your asset custom function to something else as the error says, it has already been initialized.