包含来自 twig 的非 twig 文件
我需要将文件的内容(在我的资源文件夹内)包含在 Twig 模板中。
我尝试过,但没有成功:
{% include 'public/directory/file.ext' %}
Twig 不能做到这一点吗? (我不想使用Assetic)
I need to include the contents of a file (inside my resources folder) inside a Twig template.
I have tried this with no luck:
{% include 'public/directory/file.ext' %}
Isn't Twig capable of this? (I don't want to use Assetic)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://twig.sensiolabs.org /doc/functions/source.html
http://twig.sensiolabs.org/doc/functions/source.html
我前段时间为此制作了一个 bundle 。它几乎是
file_get_contents
的包装。可以在此处找到该设置。
I made a bundle just for this some time ago. It's pretty much a wrapper for
file_get_contents
.The setup can be found here.
如果加载文件时 Twig 不是有效的 Twig 模板,Twig 会抱怨。原因是 Twig 将包含渲染的文件而不是它的内容(发现 此处)。
您可以尝试使用
use
语句,但我认为这也不起作用。此外,您使用的语法似乎不正确。当我包含(或使用)另一个树枝模板时,我使用以下语法:
文件
include.html.twig
位于src\Acme\WebsiteBundle\Resources\views\include.html 中。树枝。因此,如果您的文件位于 src\Acme\WebsiteBundle\Resources\public\directory\include.ext
中,您可以尝试如果这不起作用,您可以将文件移动到视图文件夹中。如果这是不可能的,您可以放入
app\Resources\views\
并使用语法:如果 use 语句不起作用,我担心,您可以将您的直接文件到树枝模板中。我正在使用 twig 构建一些简单的 JSON 结构模板。因此,您可能有一种方法可以将
file.ext
的内容包含到 twig 模板中,然后渲染它。如果这一切都失败了,您将必须创建一个 Twig 扩展,添加一个新标签(例如
content
),该标签将读取文件并将其内容输出到您的 Twig 模板中。希望这会帮助您包含您的文件。
问候,
马特
Twig will complain when loading your file if it is not a valid twig template. The reason is that Twig will include the rendered file and not the content of it (found here).
You could try with a
use
statement but I don't think this will work either.Moreover, the syntax you use seems incorrect. When I include (or use) another twig template, I use this syntax:
And the file
include.html.twig
resideds insrc\Acme\WebsiteBundle\Resources\views\include.html.twig
. So, if your file is insrc\Acme\WebsiteBundle\Resources\public\directory\include.ext
, you could tryIf that doesn't work, you could possibly move the file into the views folder. If this is not possible, you can possibliy put in
app\Resources\views\
and use the syntax:If the use statement doesn't work, which I'm afraid of, you could possibly wrap your file into a twig template directly. I'm templating some simple JSON structures with twig. So, there may be a way for you to include the content of
file.ext
into a twig template and then render it.If all this fail, you will have to create a Twig extension that add a new tag (something like
content
) that would read a file and output its content in your twig template.Hope this will help you to include your file.
Regards,
Matt
您可以使用 twig 扩展函数来解决这个问题。 ( http://symfony.com/doc/current/cookbook/templated/twig_extension。 html )
在 Bundle 中为 Twig 扩展创建一个 php 文件
将此代码添加到 app/config/services.yml
现在,您可以使用自定义函数作为 twig 模板中的任何默认函数
You can solve this problem with twig extension functions. ( http://symfony.com/doc/current/cookbook/templating/twig_extension.html )
Create a php file in your Bundle for the Twig extension
Add this code to the app/config/services.yml
Now you can use your custom function as any default function in your twig template