[英]path to twig template in Symfony 2
I am working on a Symfony2 project, currently trying to render a view for my controller but annoyingly I am constantly seeing this error Message: 我正在开发一个Symfony2项目,当前正在尝试为我的控制器渲染一个视图,但很烦人的是,我不断看到此错误消息:
Unable to find template
So to my understanding there is an issue with the path part of the render function. 因此,据我了解,render函数的路径部分存在问题。 I tried tackling this but no matter how I change my path I allways get this message.
我尝试解决此问题,但是无论我如何改变自己的道路,我始终会收到此消息。
Full path to the test.html.twig file: test.html.twig文件的完整路径:
src/Test/NewsInfrastructure/Sitemap/test.html.twig
This is how I implemente this path: 这是我实现此路径的方式:
return $this->templating->renderResponse('src:Test:NewsInfrastructure:Sitemap:test.html.twig', array('sitemap' => $this->siteMap->getSiteMap()));
What is it that I am doing wrong...? 我做错了什么...?
TwigBundle ( TwigExtension ) searches the templates through Bundle's folder (Resources/views) or upper levels ; TwigBundle( TwigExtension )通过Bundle的文件夹(资源/视图)或更高级别搜索模板;
I think the easiest way to render a template by calling it by namespace, like this: 我认为通过命名空间调用模板的最简单方法是这样的:
$this->render('@Vendor/Product/view.html.twig'); // src/Vendor/MyBundle/Resources/views/Product/view.html.twig
@Vendor
- without Bundle suffix, also it works bit faster.. @Vendor
没有Bundle后缀,它的工作速度也更快。
Or 要么
This can help you if you really want to load the template wich locates outside of Resources folder: 如果您确实要加载位于Resources文件夹之外的模板,这可以为您提供帮助:
$loader = new \Twig_Loader_Filesystem(PATH_TO_DIRECTORY);
$twig = new \Twig_Environment($loader, array('cache' => false));
$twig_code = $twig->render('myFile.html.twig', []);
return new Response($twig_code);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.