how to set default theme in twig?

silex, twig

Solution

I'm using Twig with namespaces and I think it is the most flexible practice:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.options' => array(
    'cache' => true,
    'strict_variables' => true,
    'debug' => false,
    'autoescape' => true
   )
));

// set namespace for your application
$app['twig.loader.filesystem']->addPath(WHERE_EVER_YOU_WANT_PATH, 'yourApplication');

Now you can render templates using the namespace:

return $app['twig']->render('@yourApplication/sample.twig', array());

you can define as many namespaces as you need.

Problem

I use a silex with twig; I create custom form_div_layout and put it in the webroot( for example) I register TwigService provider like this ``` $app->register(new Silex\Provider\TwigServiceProvider(), array( 'twig.path' => __DIR__ . '/../views', 'twig.options' => array( 'cache' => __DIR__ . '/../cache/twig', 'strict_variables' => false ), 'twig.form.templates'=> [WEBROOT . '/form_div_layout.twig'] )); ``` but i have an error `Twig_Error_Loader: Template "/home/versh/sale/web/form_div_layout.twig" is not defined () in "layout.twig" at line 52.` how to register theme correctly ? I know that if i put theme in the twig.path it will work, but this is not solution

Original source