Symfony 在模板中包含静态 html [英] Symfony include static html in template

查看:34
本文介绍了Symfony 在模板中包含静态 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just started to learn how to use Symfony which i believe is straight forward but i have a little problem with the templating engine. I want to include a static HTML fragment in one of my twig templates in Symfony (2.5.6). For now i created a static subfolder inside the resources directory (this might change but it definitely won't be inside the view folder). However i can't get this done, i always end up with an unable to find template error. I find the documentation on this subject a bit sparse (see http://symfony.com/doc/current/book/templating.html#including-other-templates) also the twig docs can't help me out on this one. I'm not even sure if i can use the magic @Bundle notation or if i have to reside in the view folder as ../ notation is not allowed within the include tag.

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

I tried the following (and a couple of variations):

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

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

I guess symfony cannot handle raw html in include but i could use a php template without any template tags as well, so the question is just how to specify a location outside the view folder.

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

I know of http://github.com/kgilden/KGStaticBundle which will probably solve my issue but i can't believe that is not achievable with the default configuration.

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

i just tried to include a regular template file from the very same directory specifying just the name of the template file (as done in the docs, see link above). Still i get an error also complaining it expects bundle:section:template.format.engine as its format. Is there an error in the docs?

推荐答案

看起来海报在我打字时找到了解决方案.我想我会把这个放在这里.

Looks like the poster found the solution while I was typing. I guess I'll leave this here for just a bit.

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

Creating and using twig namespaces is discussed here: http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html

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

By default, each bundles get's it's own namespace pointing to the views directory. You can add additional directories by adjusting 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'

Load the template with: '@FashionMedia/pricing.html'

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

I won't go into all the details but you can also use a compiler pass(http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html) to add additional paths from inside the bundle itself without having to adjust 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆