我有这样的路线: 指数:
path: /{category}
defaults: { _controller: AcmeDemoBundle:Index:index, category: "default" }
requirements:
categoria: "foo|bar|this|or|that"
,相关控制器是:
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class IndexController extends Controller
{
public function indexAction($category)
{
$data = array("Content1", "Content2");
return $this->render('AcmeDemoBundle:Default:index.html.twig', array('data' => $familiari));
}
}
?>
如果我想在模板index.html.twig中使用$ category中存储的值,该怎么办?我尝试使用渲染函数传递变量类别,但是我收到错误!先谢谢!
答案 0 :(得分:2)
render()的第二个参数是可以在渲染模板中访问的参数数组。所以如果你像这样传递$ category:
public function indexAction($category)
{
$data = array("Content1", "Content2");
return $this->render('AcmeDemoBundle:Default:index.html.twig',
array('data' => $familiari, 'category'=>$category));
}
然后您可以使用以下方法在模板中访问它:
{{ category }}