2017-10-19 112 views
0

我有小部件(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(&#039;FROM HERE&#039;); 
+0

https://stackoverflow.com/questions/15730037/prevent-output-escaping-in-twig-function-extension嫩枝从扩展名输出中跳出需要禁用此功能 –

+0

您的真棒。谢谢 – TheMan68

+0

Twig已经是一个包含函数,为什么你需要这个? – COil

回答

0

其一些他们解析随叫随到模板

外观:

https://github.com/twigphp/Twig/blob/2.x/lib/Twig/Extension/Core.php#L1287

编辑:file_get_contents做的工作。 include使得不止输出内容

+0

非常感谢。 虽然我并不需要它。 我只是简单地需要从该文件检索HTML。 您认为file_get_contents是个好主意还是会变得太重? – TheMan68

+0

'file_get_contents'完成这项工作。 'include'使得不仅仅输出内容 –

+0

好极了。谢谢。我还有一个问题。我得到的文件内容,但它得到回应在编码的字符。你知道解决这个问题吗?谢谢 – TheMan68