从 Twig 模板中导入 HTML 文件 [英] Import an HTML file from inside a Twig template
问题描述
我想从 Twig 模板中导入一个 HTML 文件.HTML 文件位于 /var/files/5
(没有扩展名).我像这样渲染模板:
I want to import an HTML file from inside a Twig template. The HTML file is located at /var/files/5
(with no extension). And I render the template like this:
$path = $_SERVER['DOCUMENT_ROOT'] . '/../var/files/5';
$content = $this->get('templating')->render('ProConvocationBundle:Default:definitive-view.html.twig', array('path' => $path));
在 Twig 模板中,我像这样导入 HTML 文件:
Inside the Twig template I import the HTML file like this:
{% include path %}
但它没有找到路径:无法找到模板/myDocumentRoot/../app/var/files/5"
我也尝试了几个相对路径都没有成功.知道如何实现它吗?
I've also tried several relative paths without success. Any idea of how to achieve it?
推荐答案
导致此异常的原因是什么?
稍微深入挖掘 Twig 代码后,以下似乎导致此异常:
What causes this exception?
After digging in the Twig code a little, the following seems to cause this exception:
Twig 尝试从已知路径/命名空间加载文件,即包名称(如 /var/www/myApplication/src/AcmeBundle/Resources/views
)和 app
路径为 myApplication/app/Resources/views
).无论如何它不接受绝对路径,因为它总是尝试将已知路径添加到给定文件的开头.
Twig tries to load the file from a known path/namespace, being bundle names (like /var/www/myApplication/src/AcmeBundle/Resources/views
) and the app
path being myApplication/app/Resources/views
). Anyway it doesn't accept absolute paths, since it always tries to add a known path to the beginning of the given file.
<?php
// Twig/Loader/Filesystem.php
class Twig_Loader_Filesystem {
// ...
protected function findTemplate()
{
// ...
foreach ($this->paths[$namespace] as $path) {
if (is_file($path.'/'.$shortname)) {
return $this->cache[$name] = $path.'/'.$shortname;
}
}
throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
}
所以基本上不可能通过绝对路径包含文件,就像你的例子一样.
So it basically isn't possible to include a file by an absolue path, like in your example.
您有多种可能来实现这种行为:
You've got a bunch of possibilities to achieve this behaviour:
见@Adam Elsodaney 的帖子
See post by @Adam Elsodaney
您可以简单地将文件从 app/var/files
移动到 app/Resources/views/var/files
并使用路径 var/files/5
包含文件.这可能不是一个合适的解决方案,因为您希望保留这些文件.
You could simply move your file from app/var/files
to app/Resources/views/var/files
and use the path var/files/5
to include the file. This is probably not a suitable solution, since you want to keep those files in place.
您可以编写自己的扩展程序,该扩展程序提供一个名为 include_absolute()
之类的函数,该函数只返回 file_get_contents($yourPath)
.
You could write your own extension that provides a function named something like include_absolute()
that simply returns file_get_contents($yourPath)
.
关于 Twig 扩展的更多信息:http://symfony.com/doc/current/cookbook/templating/twig_extension.html
More on Twig extensions: http://symfony.com/doc/current/cookbook/templating/twig_extension.html
请注意,您可能需要将 |raw
过滤器添加到 Twig 函数的输出中,因为很多东西都被到处转义了.
Be aware that you would might need to add the |raw
filter to the output of the Twig function since a lot of stuff gets escaped everywhere.
这篇关于从 Twig 模板中导入 HTML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!