简体   繁体   English

Symfony2无法正确呈现php模板

[英]Symfony2 does not render php templates correctly

I have a problem with rendering PHP templates instead Twig templates in Symfony2. 我在使用Symfony2中的PHP模板而不是Twig模板时遇到问题。 The problem is Symfony for some reason does not interpret PHP code in template. 问题是Symfony由于某种原因没有解释模板中的PHP代码。 Instead of interpreting PHP blocks of code, Symfony renders them simply as plain text. Symfony不是解释PHP代码块,而是简单地将它们呈现为纯文本。 I try to follow to the documentation and render form to template like so. 我尝试按照文档并将表单呈现为模板 When I write in controller 当我在控制器中写

return $this->render('MyStudyBundle:Default:new.html.twig', array(
    'form' => $form->createView(),
));

form loads as I expect. 像我期望的那样加载形式。 But when I change "new.html.twig" to "new.html.php" and try to render form in php template - as a result I have HTML page, where php code simply showed like plain text. 但是,当我将“new.html.twig”更改为“new.html.php”并尝试在php模板中呈现表单时 - 因此我有HTML页面,其中php代码只显示为纯文本。 The question is why Symfony does not interpret blocks of php code in php templates? 问题是为什么Symfony不解释php模板中的php代码块?

You need to enable PHP templating engine. 您需要启用PHP模板引擎。

In your app/config/config.yml : 在你的app / config / config.yml中

framework:
     templating:    { engines: ['twig', 'php'] }

This might be helpful to you: How to use PHP instead of Twig for Templates . 这可能对您有所帮助: 如何使用PHP代替Twig for Templates

您需要在app / config.yml文件中启用PHP模板引擎,并将模板保存为.php而不是.twig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM