我将接手一个使用Twig v1的项目,我将迁移到v2.5,到目前为止对Twig一无所知.所以要和善:)
请看一看,如果我遗漏了什么,请告诉我。我可以省略任何一个过滤器,它可以工作,但我想知道为什么它不是这样。
下面是我看到的错误消息:
<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->__construct('')
#1 D:\xampp\htdocs\project\includes\Twig\Node\Expression\Call.php(23): Twig_Node_Expression_Call->reflectCallable(NULL)
#2 D:\xampp\htdocs\project\includes\Twig\Node\Expression\Filter.php(32): Twig_Node_Expression_Call->compileCallable(Object(Twig_Compiler))
#3 D:\xampp\htdocs\project\includes\Twig\Compiler.php(84): Twig_Node_Expression_Filter->compile(Object(Twig_Compiler))
#4 D:\xampp\htdocs\project\includes\Twig\Node\If.php(46): Twig_Compiler->subcompile(Object(Twig_Node_Expression_Filter))
#5 D:\xampp\htdocs\project\includes\Twig\Node.php(82): Twig_Node_If->compile(Object(Twig_Compiler))
#6 D:\xampp\htdocs\project\includes\Twig\Compiler.php(84): Twig_Node->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>在Twig环境页面中,我看到他们已经过滤了要包含的PHP脚本:
$twig->addFilter('is_array', new Twig_Filter_Function('is_array'));I为v2更新了如下内容:
$twig->addFilter(new Twig_Filter('is_array'));我在模板中看到了,其中“is_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 %}附件变量定义为控制器:中的数组
$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;发布于 2018-10-27 17:50:39
弄明白了。我犯了一个简单的错误,现在对我来说是显而易见的。我需要定义过滤器的Twig名称及其关联的PHP函数:
$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'));https://stackoverflow.com/questions/53016846
复制相似问题