树枝延伸 - PHP相当于枝的包括
问题描述:
我有小部件(JS和其他文件)的库,而不是写树枝延伸 - PHP相当于枝的包括
{% include 'path' %}
所有的时间我写的扩展,只包括它们与函数调用像这样:
{% widget('name') %}
所以我的问题是如何重新实现枝包含功能? 这只是一个返回一个PHP包含的问题还是它是一个file_get_contents?
例子:
<?php
class Extensions
{
public function widget($name)
{
return include 'filepath/' . $name . '.html.twig';
// or
return file_get_content('filepath/' . $name . '.html.twig');
}
}
谢谢
更新:我使用的file_get_contents 和我碰到下面的返回:
alert('FROM HERE');
// AS
alert('FROM HERE');
答
其一些他们解析随叫随到模板
外观:
https://github.com/twigphp/Twig/blob/2.x/lib/Twig/Extension/Core.php#L1287
编辑:file_get_contents
做的工作。 include
使得不止输出内容
https://*.com/questions/15730037/prevent-output-escaping-in-twig-function-extension嫩枝从扩展名输出中跳出需要禁用此功能 –
您的真棒。谢谢 – TheMan68
Twig已经是一个包含函数,为什么你需要这个? – COil