[英]Passing controller argument to template, symfony2
I have a route like this: index: 我有这样一条路线:index:
path: /{category}
defaults: { _controller: AcmeDemoBundle:Index:index, category: "default" }
requirements:
categoria: "foo|bar|this|or|that"
and the relative controller is: 而相对的控制器是:
<?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));
}
}
?>
What if I want to use the value stored in $category inside the template index.html.twig? 如果我想使用模板index.html.twig中$ category中存储的值怎么办? I tried passing the variable category with the render function but I get an error!
我尝试通过render函数传递变量类别,但出现错误! Thanks beforehand!
预先感谢!
The 2nd parameter of render() is an array of parameters that can be accessed in the rendered template. render()的第二个参数是可以在渲染的模板中访问的参数数组。 So if you pass $category in like this:
因此,如果您像这样传递$ category:
public function indexAction($category)
{
$data = array("Content1", "Content2");
return $this->render('AcmeDemoBundle:Default:index.html.twig',
array('data' => $familiari, 'category'=>$category));
}
Then you can access it in the template using: 然后,您可以使用以下模板在模板中访问它:
{{ category }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.