使用Twig模板的简单Slim应用程序

时间:2016-06-12 19:45:42

标签: php twig slim

我正在尝试使用Twig模板构建简单的Slim应用程序。代码是

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';
require_once '../vendor/twig/twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem('../public');
$twig = new Twig_Environment($loader, array(
    'cache' => '/cache',
));


$app = new \Slim\App;

$app->get('/', function (Request $request, Response $response) use ($twig) {
  $template = $twig->loadTemplate('index.html');
  echo $template->render(array());
  //$response->getBody()->write("Test");
});

$app->run();

运行应用程序后,我在浏览器中看不到任何内容。

但是当我评论行

  $template = $twig->loadTemplate('index.html');
  echo $template->render(array());

删除评论
$response->getBody()->write("Test");

我可以在浏览器中看到Test,这意味着路由器'/'可以工作。

文件index.html存在且不是空的

index.html文件的内容是

<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <h1>My Webpage</h1>
    </body>
</html>

如何强制显示模板?

0 个答案:

没有答案