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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?