douhan5547
2015-10-09 14:06
浏览 148
已采纳

传递循环TWIG + symfony的数组值

Before I go on with my code, I apologize in advance. I am just starting to learn twig and symfony.

Ok, so I have a controller that renders a simple html.twig. Where I am stuck is at the syntax of passing values in the for loop. Let me show you what I have:

Controller:

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'
        )
    );
};
}

Twig:

<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>

If I remove the loop part of the twig and controller, it works as expected. So now, I am stuck on figuring out the syntax for what I am trying to do. Any help on how I would accomplish what I am failing it?

图片转代码服务由CSDN问答提供 功能建议

在继续我的代码之前,我提前道歉。 我刚刚开始学习twig和symfony。

好的,所以我有一个控制器,呈现一个简单的html.twig。 我被困的地方是在for循环中传递值的语法。 让我告诉你我的内容:

控制器:

 命名空间AppBundle \ Controller; 
 
use Sensio \ Bundle \ FrameworkExtraBundle \  Configuration \ Route; 
use Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller; 
use Symfony \ Component \ HttpFoundation \ Request; 
 
class DefaultController扩展Controller 
 {
 / ** 
 * @Route(“/”  ,name =“homepage”)
 * / 
 npublic function indexAction(Request $ request)
 {
 //将此示例代码替换为您需要的任何内容
返回$ this-&gt; render('default / mine。  html.twig',array(
'user_name'=&gt;'trolol',
'one_li'=&gt;'学习Symfony',
'nwo_li'=&gt;'学习控制器',
'nwo_li'  =&gt;'学习Twig',
'four_li'=&gt;'吃',
'nav'=&gt;数组(
'1':'11',
'2':'22'\  n)
); 
}; 
} 
   
 
 

Twig:

 &lt; p&gt; 欢迎&lt; h2&gt; {{user_name}}&lt; / h2&gt;&lt; / p&gt; 
&lt; p&gt;待办事项:
&lt; br /&gt; 
&lt; ul&gt; 
&  lt; li&gt; {{one_li}}&lt; / li&gt; 
&lt; li&gt; {{two_li}}&lt; / li&gt; 
&lt; li&gt; {{three_li}}&lt; / li&gt; 
&lt;  li&gt; {{four_li}}&lt; / li&gt; 
&lt; / ul&gt; 
&lt; br /&gt; 
&lt; ul id =“nav”&gt; 
 {%{%for link,text in nav%  } 
&lt; li&gt;&lt; a href =“{{link}}”&gt; {{text}}&lt; / a&gt;&lt; / li&gt; 
 {%endfor%} 
&lt; / ul&gt;  
   
 
 

如果我删除了树枝和控制器的循环部分,它会按预期工作。 所以现在,我仍然坚持要弄清楚我想要做的事情的语法。 关于如何实现我失败的任何帮助?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongshu4755 2015-10-09 14:19
    最佳回答

    Instead of this

    'nav' => array(
                '1':'11',
                '2':'22'
            )
    

    You must use this:

    'nav' => array(
                '1' => '11',
                '2' => '22'
            )
    

    Your Twig code seems okay. You just can't define arrays in PHP the way you did.

    EDIT: Looks like you have some typos. Try with this code:

    <?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')
            ));
        }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题