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?

  • 写回答

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')
            ));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?