gpt4 book ai didi

Symfony 在模板中包含静态 html

转载 作者:行者123 更新时间:2023-12-04 23:49:11 25 4
gpt4 key购买 nike

我刚开始学习如何使用 Symfony,我认为这很简单,但我对模板引擎有一点问题。我想在 Symfony ( 2.5.6 ) 的一个 Twig 模板中包含一个静态 HTML 片段。现在我在资源目录中创建了一个静态子文件夹(这可能会改变,但它绝对不会在 View 文件夹中)。但是我做不到,我总是得到一个 unable to find template错误。我发现关于这个主题的文档有点稀疏(见 http://symfony.com/doc/current/book/templating.html#including-other-templates),而且 Twig 文档也无法帮助我解决这个问题。我什至不确定我是否可以使用神奇的@Bundle 符号,或者我是否必须作为 ../ 驻留在 View 文件夹中include 标签中不允许使用符号。

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

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

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

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

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

最佳答案

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

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

默认情况下,每个包都有自己的命名空间,指向 View 目录。您可以通过调整 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'));
}
}

关于Symfony 在模板中包含静态 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com