0

product.html.twig:

<ul id="navigation">                 
    <li>
        <a href="<?php echo product.getId() ?>">
            <?php echo product.getDescription() ?>
        </a>
    </li>
</ul>

Controller Action method contains:

public function showAction($id = 5)
{
    $product = $this->getDoctrine()
        ->getRepository('AppBundle:Product')
        ->find($id);

    if (!$product) {
        throw $this->createNotFoundException(
            'No product found for id '.$id
        );
    }
    else 
    {
        return $this->render('default/productItem.html.twig', array(
            'id'=> $id,
            'name' => $name));
    }
}

I cant see the output in the list

2
  • Is it an array that is holding an object? Is $product an object? Commented Jan 16, 2017 at 17:10
  • 1
    why not with href="{{ product.id }}" Commented Jan 16, 2017 at 17:12

2 Answers 2

1

You should use the Twig syntax.

<ul id="navigation">                 
    <li>
        <a href="/page.php?id={{ product.getId() }}">
            {{ product.getDescription() }}
        </a>
    </li>
</ul>

In your case your input have to be an object. With the functions getId() and getDescription().

In your code you can remove "get" and write only {{ product.id }} for example.

Sign up to request clarification or add additional context in comments.

Comments

0

Would suggest some changes to your Controller:

In your controller you hard code your $id parameter to "5'. It's probably better to use routing annotation and have an optional parameter instead. Use the defaults to hardcode any default value.

Also, instead of $id, I suggest you call it $productID, so you know it is for a Product Entity, and to distinguish it from what you pass in the array (as a parameter) to your twig controller.

Also in your sample code you show passing in the parameter of id and name, but firstly $name is not defined anywhere, and $id is what you pass in as a parameter to the Controller, but then in your twig file you don't show using either of name or id at all! Plus you render productItem.html.twig, but above the post you call it product.html.twig. So is that a different file?

Make sure when you post question on Stackoverflow that everything is clear.

Here is sample of how you might change you controller code as per my suggestions above:

/**
 * @Route("/showproduct/{productID}",
 *      defaults={"productID" = 0},
 *      name="showproduct
 */
public function showAction($productID)
{
    $product = $this->getDoctrine()
        ->getRepository('AppBundle:Product')
        ->find($productID);

    if (!$product) {
        throw $this->createNotFoundException(
            'No product found for id '.$productID
        );
    }
    else 
    {
        return $this->render('default/productItem.html.twig', array(
            'product'=> $product,
        ));
    }
}

Then in your twig file (is it productItem.html.twig???) then like this:

<ul id="navigation">                 
    <li>
        <a href="{{ product.getId }}">
            {{ product.getDescription }}
        </a>
    </li>
</ul>

Hope it helps!

Comments

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.