0

我试图在以下位置遵循 symfony 教程:

http://symfony.com/doc/current/book/page_creation.html#step-1-create-the-route

但在某个时候,我决定使用 php 代替 twig。

所以我试图在第 3 步使用模板,我的页面向我展示了这一行

extend('::base.html.php') ?> Hello escape($name) ?>!

不确定这是否是教程中的错误,因为他们通常选择使用 twig 工作,或者我做错了什么。就像我的页面在尝试扩展它时无法识别 php。

这是我要渲染的页面的完整代码:

<!-- src/Acme/HelloBundle/Resources/views/Hello/index.html.php -->
<?php $view->extend('::base.html.php') ?>

Hello <?php echo $view->escape($name) ?>!

我在这个 url http://localhost:8080/web/app_dev.php/hello/Peter 运行页面

这是我的控制器:

class HelloController extends Controller
{
    public function indexAction($name)
    {
        // return $this->render(
        //    'AcmeHelloBundle:Hello:index.html.twig',
        //    array('name' => $name)
        // );

        // render a PHP template instead
         return $this->render(
             'AcmeHelloBundle:Hello:index.html.php',
             array('name' => $name)
         );
    }
}

和我的基地:

!-- app/Resources/views/base.html.php -->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title><?php $view['slots']->output('title', 'Welcome!') ?></title>
        <?php $view['slots']->output('stylesheets') ?>
        <link rel="shortcut icon" href="<?php echo $view['assets']->getUrl('favicon.ico') ?>" />
    </head>
    <body>
        <?php $view['slots']->output('_content') ?>
        <?php $view['slots']->output('javascripts') ?>
    </body>
</html>

请注意,我使用的 /web/ 不在本教程中。这可能与问题有关吗?因为我告诉基本模型在 :: ?我怎样才能解决这个问题?

4

1 回答 1

4

如果您查看文档中的“如何使用 PHP 而不是 Twig for Templates”一文,您会读到需要将 PHP 注册为模板引擎。

假设您使用 Yaml 作为配置格式:

# app/config/config.yml
framework:
    # ...
    templating:    { engines: ['twig', 'php'] }
于 2013-03-03T19:13:43.573 回答