33

我发现包含带有此路径的模板可以正常工作

{% include 'AcmeDemoBundle:TemplateArchive:view.html.twig' with {'data': c.data} %}

虽然这似乎是不允许的:

{% include 'AcmeDemoBundle:TemplateArchive:6:view.html.twig' with {'data': c.data} %}

换句话说,我试图访问已分类到我的 bundle/resources/views/ 文件夹中的子文件夹结构中的模板。

如果从我的第一行开始,我不允许比常规的单级包含更深入,是否有另一种/更好的方式来构建这些模板文件?

(文件夹名称'6'代表我想包含的数据库中的模板ID,它需要是动态的并且像这样很好地在文件夹中排序......)。

我尝试将模板文件夹命名为“t6”,但没有区别,“没有前导字母的数字”不是这里的问题......

4

3 回答 3

58

关于什么

{% include 'AcmeDemoBundle:TemplateArchive:6/view.html.twig' with {'data': c.data} %}
于 2012-10-12T08:50:41.443 回答
6

两者都在工作:

AcmeDemoBundle:TemplateArchive:6/view.html.twig
AcmeDemoBundle:TemplateArchive/6:view.html.twig
于 2013-12-07T02:33:49.983 回答
5

As of Symfony 2.2 you can also use Namespaced paths:

{% include '@AcmeDemo/TemplateArchive/6/bar.html.twig' with {'data': c.data} %}

You can even define your own Namespaces:

# app/config/config.yml
twig:
    # ...
    paths:
        "%kernel.root_dir%/../src/Acme/DemoBundle/Resources/views/TemplateArchive": TemplateArchive

And then use it like:

{% include '@TemplateArchive/6/view.html.twig' with {'data': c.data} %}

This also works in controllers (with custom Namespaces too):

// TemplateArchiveController.php
return $this->render('@TemplateArchive/6/view.html.twig', ['data' => $c.getData()]);

From [Symfony Cookbook: Namespaced paths]:

As an added bonus, the namespaced syntax is faster.

于 2014-03-21T12:47:07.143 回答