我试图访问通过模板上的$this-呈现(‘url’,数组)传递的数组。我正在遵循Symfony的书,在这里我得到了这个例子,我只是无法使它工作。
我的控制器
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller{
/**
* @Route("lucky/number/{count}")
*/
public function numberActionTemplate($count){
$numbers = array();
for($i = 0; $i < $count; $i++){
$numbers[] = rand(0, 100);
}
$results = implode(',', $numbers);
$numbers = array(0=>'b', 1=>'a', 2=>'c');
return $this->render('lucky/number.html.php', array('luckyNumberList' => $numbers));
}我的模板
现在我不知道我做错了什么。基本的php经验,几天前开始使用symfony。
发布于 2015-09-16 13:16:43
Symfony使用Twig模板,而您试图使用PHP模板。
编写它的正常方式是:
<li>
{{ luckyNumberList[0] }}
</li> 您可以查看Twig引用:
发布于 2015-09-16 13:19:59
你用了什么代码?我搜索了class LuckyController并从正式文件中找到了这段代码
{# app/Resources/views/lucky/number.html.twig #}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Lucky Numbers: {{ luckyNumberList }}</h1>
{% endblock %}需要注意的是,这个Twig模板中没有PHP。
如果要显示PHP中的一个值,可以使用使用Twig访问数组中的任何项
…
<h1>Lucky Numbers: {{ luckyNumberList[0] }}</h1>
…发布于 2015-09-16 13:23:22
看起来你忘记使用echo了,没什么大不了的:
<?php echo $luckyNumberList[0] ?>我认为您在配置中包括了php模板引擎:
# app/config/config.yml
framework:
# ...
templating:
engines: ['php', 'twig']https://stackoverflow.com/questions/32609414
复制相似问题