I'm going through Symfony2 start guide and my question is:
I have a database with columns: price,description,id,name Then in my controller I fetch these columns and display them via a twig template. Within my controller I do:
public function showAction($id)
{
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product')
->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
$price = $product -> getPrice();
$description = $product -> getDescription();
return $this->render(
'AcmeStoreBundle:Store:index.html.twig',
array('id' => $id, 'price' => $price, 'description' => $description)
);
}
My question is, can I change $price,$description to call it whatever else...? or am I forced to keep mentioning these variables like they're named within the database?
basically, can I do:
$foo = $product -> getPrice();
$bar = $product -> getDescription();
And then in my render function do:
return $this->render(
'AcmeStoreBundle:Store:index.html.twig',
array('uniquecode' => $id, 'cost' => $foo, 'message' => $bar)
);
My question is two fold : 1) can I do it 2) is it a good practice to do it?
Better:
return $this->render('AcmeStoreBundle:Store:index.html.twig', array(
'product' => $product
));
You access the product attributes in twig like this
{{ product.description }}
Use meaningful names for variables. Variable name must define the exact explanation of its content
I found it here http://codebuild.blogspot.de/2012/02/15-best-practices-of-variable-method.html but you can just google for variable and method naming
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.