0

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

1

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')
        ));
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried that earlier, but I get CRITICAL - Fatal Parse Error: syntax error, unexpected ';' on line 25 /// if I delete the ; I get an error unexpected } etc etc.
Edited my answer. Check it
Oh wow. I was missing one closing ) there. Thank you very much. One last question since we are on the subject. If I wanted to pass a one more, so something like this <li><a href="{{ link }}">{{ text }} {{ third }}</a></li> Would the syntax be? 'nav' => array( '1' => '11' => '111', '2' => '22' => '222' ))
No, you can't define it like that. You should add a dimension to array: 'nav' => array('1' => array('11', '111'), '2' => array('2' => array('22', '222'))) and you should change Twig code as well. It would get harder. If you are gonna print them the way you described, "text" followed by "third" whith a blank space in the middle you could define the array this way 'nav' => array('1' => '11 111' , '2' => '22 222'). They could be variables of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.