0 レビュー
1 回答
php-Twigテンプレートには、コントローラーにあるものと同じ変数名を使用する必要がありますか?
Symfony2スタートガイドを読んでいますが、質問は次のとおりです:
次の列を持つデータベースがあります:price、description、id、name 次に、コントローラーでこれらの列をフェッチし、小枝テンプレートを介して表示します。 私のコントローラー内で私は次のことを行います:
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)
);
}
私の質問は、$ price、$ descriptionを変更して、他の名前で呼び出すことはできますか...?または、データベース内で名前が付けられているように、これらの変数について言及し続ける必要がありますか?
基本的に、私にできること:
$foo = $product -> getPrice();
$bar = $product -> getDescription();
そして、私のレンダリング関数で次のことを行います:
return $this->render(
'AcmeStoreBundle:Store:index.html.twig',
array('uniquecode' => $id, 'cost' => $foo, 'message' => $bar)
);
私の質問は2つあります:1)それを行うことはできますか2)それを行うのは良い習慣ですか?
わからない
0
レビュー
答え :
解決策:
より良い:
return $this->render('AcmeStoreBundle:Store:index.html.twig', array(
'product' => $product
));
このように小枝の商品属性にアクセスします
{{ product.description }}
変数には意味のある名前を使用します。変数名は、 その内容の正確な説明
ここで見つけましたhttp://codebuild.blogspot.de/2012/02/15-best-practices-of-variable-method.htmlですが、をグーグルで検索できます変数とメソッドの命名
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。