dtsi9484 2018-10-26 22:07
浏览 17
已采纳

Twig x2.5 - 模板/ php过滤器

I'm taking over a project that used Twig v1 and I'm migrating to v2.5 with really no knowledge of Twig until now.... so be nice :)

Please have a look and let me know if I'm missing something. I can leave out any of the filters and it works, but I want to know why it's not as is.

Here's the error message I'm seeing:

<b>Fatal error</b>:  Uncaught ReflectionException: Function () does not exist in D:\xampp\htdocs\project\includes\Twig\Node\Expression\Call.php:288
Stack trace:
#0 D:\xampp\htdocs\project\includes\Twig\Node\Expression\Call.php(288): ReflectionFunction-&gt;__construct('')
#1 D:\xampp\htdocs\project\includes\Twig\Node\Expression\Call.php(23): Twig_Node_Expression_Call-&gt;reflectCallable(NULL)
#2 D:\xampp\htdocs\project\includes\Twig\Node\Expression\Filter.php(32): Twig_Node_Expression_Call-&gt;compileCallable(Object(Twig_Compiler))
#3 D:\xampp\htdocs\project\includes\Twig\Compiler.php(84): Twig_Node_Expression_Filter-&gt;compile(Object(Twig_Compiler))
#4 D:\xampp\htdocs\project\includes\Twig\Node\If.php(46): Twig_Compiler-&gt;subcompile(Object(Twig_Node_Expression_Filter))
#5 D:\xampp\htdocs\project\includes\Twig\Node.php(82): Twig_Node_If-&gt;compile(Object(Twig_Compiler))
#6 D:\xampp\htdocs\project\includes\Twig\Compiler.php(84): Twig_Node-&gt;compile(Object(Twig_Compiler))
#7 D:\xampp\htdocs\project\includes\Twig\Node\Block.php(34 in <b>D:\xampp\htdocs\project\includes\Twig\Environment.php</b> on line <b>570</b>

In the Twig environment page I see that they had filtering for PHP scripts to be included:

$twig->addFilter('is_array', new Twig_Filter_Function('is_array'));

I updated this for v2 as:

$twig->addFilter(new Twig_Filter('is_array'));

From there I see in the template where 'is_array' is applied to an attachments array:

    {% if attachments|is_array %}
<div class="knowledgebasearticleattachment">{{ LANG.ATTACHMENTS }}</div>
    {% for attachment in attachments %}
    <div><span class="knowledgebaseattachmenticon"></span> <a href="{{ attachment_url }}{{ attachment.id }}" target="_blank">{{ attachment.name }} ({{ attachment.filesize }})</a></div>

    {% endfor %}
{% endif %}

The attachment variable is defined as an array in the controller:

$q = $db->query("SELECT * FROM ".TABLE_PREFIX."attachments WHERE article_id=".$article['id']);
        while($r = $db->fetch_array($q)){
            $attachments[] = $r;
        }
        $template_vars = array();
        $template_vars['attachments'] = $attachments;
  • 写回答

1条回答 默认 最新

  • doujiu3882 2018-10-27 17:50
    关注

    Figured it out. I made a simple error that is now obvious to me. I need to define the Twig name of the filter along with the PHP function it's associated to:

    $twig->addFilter(new Twig_SimpleFilter('is_array','is_array'));
    $twig->addFilter(new Twig_SimpleFilter('is_numeric','is_numeric'));
    $twig->addFilter(new Twig_SimpleFilter('print_r','print_r'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效