如何在Twig中设置默认主题?

3

我使用silex和twig;

我创建了自定义的form_div_layout并将其放置在webroot中(例如)

我像这样注册TwigService提供程序

    $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']
));

但是我遇到了一个错误

Twig_Error_Loader: 在“layout.twig”的第52行中未定义模板“/home/versh/sale/web/form_div_layout.twig”。

如何正确注册主题?我知道如果将主题放在twig.path中,它将起作用,但这不是解决方案。

2个回答

6

我正在使用带命名空间的Twig,我认为这是最灵活的实践方法:

$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');

现在您可以使用命名空间呈现模板:
return $app['twig']->render('@yourApplication/sample.twig', array());

您可以定义尽可能多的命名空间。


4

您需要将基本模板 form_div_layout.html.twig 添加到 twig.form.templates 选项中。

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/../views',
    'twig.options'=>array(
        'cache'     => __DIR__.'/../cache',
    ),
    'twig.form.templates' => array(
        'form_div_layout.html.twig', 
        'theme/form_div_layout.twig'
    ),
));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接