在我继续写代码之前,我先道歉一下。我刚开始学树枝和交响乐。
好了,我有了一个渲染简单html.twig的控制器。我卡住的地方是在for循环中传递值的语法。让我给你看看我有什么:
控制器:
namespace AppBundle'Controller;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Route;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Symfony'Component'HttpFoundation'Request;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
// replace this example code with whatever you need
return $this->render('default/mine.html.twig', array(
'user_name' => 'trolol',
'one_li' => 'Learn Symfony',
'two_li' => 'Learn Controller',
'three_li' => 'Learn Twig',
'four_li' => 'Eat',
'nav' => array(
'1':'11',
'2':'22'
)
);
};
}
枝:
<p>Welcome <h2>{{ user_name }}</h2></p>
<p> To Do:
<br />
<ul>
<li>{{ one_li }}</li>
<li>{{ two_li }}</li>
<li>{{ three_li }}</li>
<li>{{ four_li }}</li>
</ul>
<br />
<ul id="nav">
{% for link,text in nav %}
<li><a href="{{ link }}">{{ text }}</a></li>
{% endfor %}
</ul>
如果我移除小枝和控制器的环路部分,它会像预期的那样工作。所以现在,我要弄清楚我要做的事情的语法。有什么帮助我如何完成我失败了吗?
代替
'nav' => array(
'1':'11',
'2':'22'
)
你必须使用这个:
'nav' => array(
'1' => '11',
'2' => '22'
)
你的小枝代码似乎没有问题。你不能像以前那样在PHP中定义数组。
编辑:看起来你有一些错别字。试试下面的代码:<?php
namespace AppBundle'Controller;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Route;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Symfony'Component'HttpFoundation'Request;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
// replace this example code with whatever you need
return $this->render('default/mine.html.twig', array(
'user_name' => 'trolol',
'one_li' => 'Learn Symfony',
'two_li' => 'Learn Controller',
'three_li' => 'Learn Twig',
'four_li' => 'Eat',
'nav' => array('1' => '11', '2' =>'22')
));
}
}