【问题标题】:twig extension - php equivalent of twig's includetwig 扩展 - php 相当于 twig 的包含
【发布时间】:2017-10-19 04:43:03
【问题描述】:

我有一个小部件库(js 和其他文件),而不是编写

{% include 'path' %}

一直以来,我都在编写一个扩展,以便将它们包含在一个函数调用中,如下所示:

{% widget('name') %}

所以我的问题是如何重新实现 twig 包含功能? 只是返回一个 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;);

【问题讨论】:

标签: php symfony twig extension-methods


【解决方案1】:

他们在调用时解析模板更多

看:

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

编辑:file_get_contents 完成这项工作。 include 不仅仅是输出内容

【讨论】:

  • 非常感谢。不过,我并不需要那么深入。我只需要从该文件中检索 html。你认为 file_get_contents 是个好主意还是会变得太重?
  • file_get_contents 完成这项工作。 include 不仅仅是输出内容
  • 太棒了。谢谢你。我还有一个问题。我正在获取文件内容,但它在编码字符处得到回显。你知道解决这个问题的方法吗?谢谢
  • 你能举个例子吗
  • @TheMan68 将您的函数标记为safe
猜你喜欢
  • 2016-08-11
  • 2011-10-26
  • 2012-12-16
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多