如何在 Symfony2/Twig 模板中包含原始 HTML 文件? [英] How do I include raw HTML files in Symfony2/Twig templates?

查看:31
本文介绍了如何在 Symfony2/Twig 模板中包含原始 HTML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Symfony2 中处理一个项目,我有几个小的 html 需要包含在我的一个主要视图中.根据官方的 Twig 文档,我应该可以简单地使用 {% include 'filename.html' %} 但在 Symfony 中,除非文件名以.html.twig"结尾,否则它会抛出并出错找不到文件.我想避免对这些文件使用 Twig 模板,因为它们没有动态内容和大量双括号(它们是 javascript 模板),并且要求模板设计者必须将这些文件中的每一个都包装在 {%raw %} 标签似乎是一种非常笨拙的方法.

解决方案

快速回顾 twig 文件扩展名(取自文档):

<块引用>

每个模板名称还有两个扩展名,用于指定该模板的格式和引擎.

AcmeBlogBu​​ndle:Blog:index.html.twig - HTML 格式,Twig 引擎AcmeBlogBu​​ndle:Blog:index.html.php - HTML 格式,PHP 引擎AcmeBlogBu​​ndle:Blog:index.css.twig - CSS 格式,Twig 引擎

<块引用>

默认情况下,任何 Symfony2 模板都可以用 Twig 或 PHP 编写,扩展名的最后一部分(例如 .twig 或 .php)指定应该使用这两个引擎中的哪一个.扩展名的第一部分(例如 .html、.css 等)是模板将生成的最终格式.

因此,即使没有抛出错误,将文件包含为 .html 也至少是模棱两可的.

所以你有 3 个选择:

  1. 如果文件是纯 javascript,则将它们作为脚本标签包含在您的页面中.

  2. 如果它们混合了 HTML 和 JS,则使用 {% raw %} 转义 JS 并将文件包含为 foo.html.twig 模板.如果像这样包含了很多脚本,那么很可能您的设计师可以进行一些重构并将其大部分脚本移动到外部文件中(请参阅选项 1)

  3. 如果你真的坚持,你总是可以编写一个 Twig 扩展来包含原始 HTML 文件.(有关此选项的更多详细信息,请参阅下面@Haprog 的回答).

    {{ include_html('foo/bar.html') }}

  4. UPDATE 2015 twig 已经添加了源函数:

{{ source('AcmeSomeBundle:Default:somefile.html.twig') }}

在下面对选项 4 的评论中感谢@Nigel Angel.

I'm working on a project in Symfony2 and I have several small pieces of html that need to be included in one of my main views. According to the official Twig documentation I should be able to simply use {% include 'filename.html' %} but in Symfony unless the filename ends in ".html.twig" it throws and error saying it cannot find the file. I'd like to avoid using Twig templates for these files since they have no dynamic content and lots of double braces (they're javascript templates) and requiring the template designer to have to wrap every one of these files in {% raw %} tags seems like a really Kludgey way to do it.

解决方案

A quick recap on twig file extensions (taken from the documentation):

Every template name also has two extensions that specify the format and engine for that template.

AcmeBlogBundle:Blog:index.html.twig - HTML format, Twig engine
AcmeBlogBundle:Blog:index.html.php - HTML format, PHP engine
AcmeBlogBundle:Blog:index.css.twig - CSS format, Twig engine

By default, any Symfony2 template can be written in either Twig or PHP, and the last part of the extension (e.g. .twig or .php) specifies which of these two engines should be used. The first part of the extension, (e.g. .html, .css, etc) is the final format that the template will generate.

Therefore it makes sense to me that including a file as .html would be at the least ambiguous even if it didn't throw an error.

So you have 3 choices:

  1. If the files are purely javascript then include them as script tags in your page.

  2. If they are mixed HTML and JS then escape the JS with {% raw %} and include the files as foo.html.twig templates. If there are lots of scripts being included like this then most likely your designers could do with a little refactoring and move the bulk of their scripts to external files (see option 1)

  3. If you really insist you could always write a Twig extension to include raw HTML files. (EDIT: See @Haprog's answer below for more details on this option).

    {{ include_html('foo/bar.html') }}

  4. UPDATE 2015 twig has since added the source function:

{{ source('AcmeSomeBundle:Default:somefile.html.twig') }}

Kudos to @Nigel Angel in the comments below for option 4.

这篇关于如何在 Symfony2/Twig 模板中包含原始 HTML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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