我正在使用带有Slim框架的Twig,我收到错误array_intersect_key(): Argument #1 is not an array
我的模板看起来像这样:
{% extends 'main.tmpl' %}
{% block content %}
<h2>Teams</h2>
<ul>
{% for team in teams %}
<li>
<img src="/images/{{ team.name }}.png"/>
<a href="/team/{{ team.name }}">{{ team.name }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}
我的php代码如下:
$app->get('/teams', function() use ($app) {
return $app->render("teams", array(
"teams" => array(array("name" => "Hello"))
));
});
我尝试将模板var团队更改为组并具有相同的效果。如何解决这个错误?为什么模板会抛出此异常?
和View类:
$twig = new Twig_Environment(new Twig_Loader_Filesystem('views'));
class View extends \Slim\View {
protected function render($template) {
global $twig;
throw new Exception("Hello");
if (substr($template, -5) !== '.tmpl') {
$template .= '.tmpl';
}
return $twig->render($template, $this->data);
}
}
更新:Stacktrace
Details
Type: Twig_Error_Runtime
Message: An exception has been thrown during the rendering of a template ("array_intersect_key(): Argument #1 is not an array") in "teams.tmpl" at line 9.
File: /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Template.php
Line: 146
Trace
#0 /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Environment.php(331) : eval()'d code(63): Twig_Template->displayBlock('content', Object(Slim\Helper\Set), Array)
#1 /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Template.php(276): __TwigTemplate_cafc9f26650a81c1b7c5fc9d1e485dc009273ca6a578c04f8bdf5826f601fb58->doDisplay(Object(Slim\Helper\Set), Array)
#2 /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Template.php(250): Twig_Template->displayWithErrorHandling(Object(Slim\Helper\Set), Array)
#3 /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Environment.php(331) : eval()'d code(24): Twig_Template->display(Object(Slim\Helper\Set), Array)
#4 /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Template.php(276): __TwigTemplate_164a4230b6490af9d793411b328170b5a5588e62f33a13c07c0f769bb7a3f06b->doDisplay(Object(Slim\Helper\Set), Array)
#5 /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Template.php(250): Twig_Template->displayWithErrorHandling(Object(Slim\Helper\Set), Array)
#6 /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Template.php(261): Twig_Template->display(Object(Slim\Helper\Set))
#7 /home/kuba/projects/euro2012/libs/Twig/lib/Twig/Environment.php(292): Twig_Template->render(Object(Slim\Helper\Set))
#8 /home/kuba/projects/euro2012/bootstrap.php(23): Twig_Environment->render('teams.tmpl', Object(Slim\Helper\Set))
#9 /home/kuba/projects/euro2012/libs/Slim/Slim/View.php(255): View->render('teams', NULL)
#10 /home/kuba/projects/euro2012/libs/Slim/Slim/View.php(243): Slim\View->fetch('teams', NULL)
#11 /home/kuba/projects/euro2012/libs/Slim/Slim/Slim.php(751): Slim\View->display('teams')
#12 /home/kuba/projects/euro2012/index.php(29): Slim\Slim->render('teams', Array)
#13 [internal function]: {closure}()
#14 /home/kuba/projects/euro2012/libs/Slim/Slim/Route.php(441): call_user_func_array(Object(Closure), Array)
#15 /home/kuba/projects/euro2012/libs/Slim/Slim/Slim.php(1314): Slim\Route->dispatch()
#16 /home/kuba/projects/euro2012/libs/Slim/Slim/Middleware/Flash.php(85): Slim\Slim->call()
#17 /home/kuba/projects/euro2012/libs/Slim/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call()
#18 /home/kuba/projects/euro2012/libs/Slim/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#19 /home/kuba/projects/euro2012/libs/Slim/Slim/Slim.php(1261): Slim\Middleware\PrettyExceptions->call()
#20 /home/kuba/projects/euro2012/index.php(39): Slim\Slim->run()
#21 {main}
答案 0 :(得分:0)
我可以通过添加此代码来修复它:
$data = get_class($this->data) == 'Slim\Helper\Set' ? $this->data->all() : $this->data;
return $template->render($data);
里面的渲染函数里面的视图。它将Slim chage数组接合到Set类的实例。