我有一个这样的数组:
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
我想以这样的可读方式打印它:
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
我必须将数组传递给 Twig 模板。我试过这个,但我没有得到我想要的结果。你能告诉我有什么改变吗?
在控制器中:
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
$b = json_encode($a);
$c = json_decode($b, true);
return $this->render('AcmeTestBundle:Home:data.html.twig', array('data' => $c));
在模板中:
{% extends '::base.html.twig' %}
{% block stylesheets %}
<link href="{{ asset('bundles/acmetest/css/test.css') }}" type="text/css" rel="stylesheet" />
{% endblock %}
{% block body %}
{{ data }}
{% endblock %}
(这是 base.html.twig :)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}{% endblock %}
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
</html>