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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog