【问题标题】:Symfony include static html in templateSymfony 在模板中包含静态 html
【发布时间】:2014-11-14 12:43:53
【问题描述】:

我刚开始学习如何使用 Symfony,我认为这很简单,但我对模板引擎有一点问题。我想在 Symfony (2.5.6) 中的一个树枝模板中包含一个静态 HTML 片段。现在我在资源目录中创建了一个静态子文件夹(这可能会改变,但它肯定不会在视图文件夹中)。但是我无法完成这项工作,我总是以unable to find template 错误告终。我发现有关此主题的文档有点稀疏(请参阅http://symfony.com/doc/current/book/templating.html#including-other-templates),树枝文档也无法帮助我解决这个问题。我什至不确定我是否可以使用魔术 @Bundle 表示法,或者我是否必须驻留在视图文件夹中,因为包含标签中不允许使用 ../ 表示法。

我尝试了以下方法(以及一些变体):

{{ include('@FashionMediaBundle/Resources/static/pricing.html') }}

我猜 symfony 无法处理包含的原始 html,但我也可以使用没有任何模板标签的 php 模板,所以问题是如何指定视图文件夹之外的位置。

我知道http://github.com/kgilden/KGStaticBundle 可能会解决我的问题,但我不敢相信默认配置无法实现。

编辑:我只是试图从同一个目录中包含一个常规模板文件,仅指定模板文件的名称(如文档中所做的那样,请参见上面的链接)。我仍然收到一个错误,还抱怨它期望 bundle:section:template.format.engine 作为它的格式。文档中有错误吗?

【问题讨论】:

  • 如果你的 bundle 被称为 FashionMediaBundle 然后删除 Bundle 部分,在没有它的情况下调用 twig 命名空间路径,如下所示:@FashionMedia
  • @xurshid29 不,不是,但你必须省略@。我有另一个模板的工作测试用例,其中包含类似FashionMediaBundle:Static:include.html.php 但不适用于FashionMedia:Static:include.html.php
  • 不,不是read this :)
  • 啊,我明白了。也适用于视图目录中的资源。

标签: symfony templates twig


【解决方案1】:

看起来发帖人在我打字时找到了解决方案。我想我会把它留在这里一会儿。

此处讨论了创建和使用 twig 命名空间:http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html

默认情况下,每个包都有自己的命名空间,指向视图目录。您可以通过调整 app/config.yml 来添加额外的目录

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    paths:
        "%kernel.root_dir%/../../cerad2/src/Cerad/Bundle/FashionMediaBundle/Resources/static": FashionMedia

加载模板:'@FashionMedia/pricing.html'

我不会详细介绍所有细节,但您也可以使用编译器通道 (http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html) 从包本身添加其他路径,而无需调整 config.yml:

class Pass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
    $bundleDirAction = $container->getParameter('cerad_api01__bundle_dir') . '/Action';

    $twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');

    $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($bundleDirAction, 'CeradApi01'));        
}
}

【讨论】:

    【解决方案2】:

    试试这个:

    {{ include('FashionMediaBundle:Resources:static:pricing.html') }}
    

    如果您想将此文件放在另一个目录中,我建议您使用symbolic link(我不知道Twig 是否可以包含不在Resources 目录中的文件)。

    【讨论】:

    • 还是一样的结果。
    • 您的文件是否位于FashionMediaBundle/Resources/views/static/pricing.html 路径?
    • 不,如上所述,它不在视图目录中,这是实际的复杂情况。我有一个测试用例{{ include('FashionMediaBundle:Static:include.html.php') }},它在views目录中工作一个文件,但是1)呈现为twig而不是php,2)在他们包含的文档中没有包和部分,但我不能这样做......
    • 对不起,我不知道你是否可以使用不在views``directory. I still suggest you to use a symbolic link if you want to put the file in another directory than views`中的文件。
    • 再次感谢我找到了解决方案,请看我的回答
    【解决方案3】:

    我找到了解决方案!

    在 twig 配置中,您可以设置路径并为它们分配名称。

    twig:
        [... twig options here ...]
        paths:
            "%kernel.root_dir%/../[path to static]": static_pages
    

    然后你可以将它们包含在

     {% include "@static_pages/pricing.html.twig" %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      相关资源
      最近更新 更多