我存储在app/resources/views
in accordance with the Symfony documentation下的模板,并在该视图目录下,我有几层目录来保持我的模板组织。我可以将模板目录分配给Symfony控制器吗?
在我的大多数控制器,所有动作渲染模板从相同的子目录,例如,所有在MyController
使用的模板的行动中app/resources/views/foo/bar
:
class MyController extends Controller
{
public function someAction()
{
// stuff
return $this->render('foo/bar/some.html.twig');
}
public function otherAction($id)
{
// more stuff
return $this->render('foo/bar/other.html.twig'); // same dir, different template
}
}
有没有“的Symfony”的方式只需将该特定的foo/bar
目录分配给控制器一次,这样我就不必再重复它了?
目前,我正在做通过指定目录下保护财产和包装父渲染方法将其追加到模板名称如下:
protected $view_path = 'foo/bar/';
protected function render($view, array $parameters = array(), Response $response = null) {
return parent::render($this->view_path . $view, $parameters, $response);
}
于是我可以只写return $this->render('some.html.twig');
代替。
但我认为可能已经有一个更清洁的内置方式,我还没有在文档中找到,就像在课堂上的注释或其他东西。有谁知道更好的方法?
有什么意义?为什么不使用这个 - [在Bundle语法中引用模板](http://symfony.com/doc/current/templating.html#referencing-templates-in-a-bundle)? –
对我而言,重点在于避免重复输入相同的东西。我已经看到了你链接的部分,但我的模板并没有捆绑在一起。他们只是在默认的'app/resources/views'位置。 –
@ Don'tPanic我认为你的最后解决方案已经很干净了。它使你的意图明确。 – Pete