Reputation: 3515
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
Upvotes: 0
Views: 915
Reputation: 7764
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!
Upvotes: 0
Reputation: 27295
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.
Upvotes: 1