我刚刚开始学习如何使用 Symfony,我相信它很简单,但我在模板引擎方面遇到了一些问题。我想在 Symfony 中的一个树枝模板中包含一个静态 HTML 片段(2.5.6
)。现在我在资源目录中创建了一个静态子文件夹(这可能会改变,但它肯定不会在视图文件夹中)。然而我无法完成这件事,我总是以一个unable to find template
错误。我发现关于这个主题的文档有点稀疏(请参阅http://symfony.com/doc/current/book/templated.html#include-other-templates http://symfony.com/doc/current/book/templating.html#including-other-templates)twig 文档也无法帮助我解决这个问题。我什至不确定我是否可以使用神奇的 @Bundle 符号,或者我是否必须驻留在视图文件夹中../
include 标记内不允许使用符号。
我尝试了以下方法(以及一些变体):
{{ include('@FashionMediaBundle/Resources/static/pricing.html') }}
我猜 symfony 无法处理包含中的原始 html,但我也可以使用没有任何模板标签的 php 模板,所以问题只是如何指定视图文件夹之外的位置。
我知道http://github.com/kgilden/KGStaticBundle http://github.com/kgilden/KGStaticBundle这可能会解决我的问题,但我不敢相信默认配置无法实现这一点。
编辑:我只是尝试包含来自同一目录的常规模板文件,仅指定模板文件的名称(如文档中所做的那样,请参阅上面的链接)。我仍然收到一个错误,并抱怨它期望bundle:section:template.format.engine
作为其格式。文档中有错误吗?
看来发帖者在我打字时找到了解决方案。我想我会把它留在这里一会儿。
这里讨论创建和使用 Twig 命名空间:http://symfony.com/doc/current/cookbook/templated/namespaced_paths.html 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 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'));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)