我要接管一个使用Twig v1的项目,并且直到现在我还完全不了解Twig才能迁移到v2.5。...太好了:)
请看看,如果我缺少什么,请告诉我。我可以省略所有过滤器,并且可以使用,但是我想知道为什么它不是这样。
这是我看到的错误消息:
<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'));
我已将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;
答案 0 :(得分:0)
弄清楚了。我犯了一个简单的错误,现在对我来说很明显。我需要定义过滤器的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'));