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

I'm trying to build simple Slim application with Twig template. The code is

<?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();

After running application I can see nothing in my browser.

But when I comment lines

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

And remove comment from line

$response->getBody()->write("Test");

I can see Test in my browser which means that router '/' works.

File index.html exists and it's not emply

The content of index.html file is

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

How can I force template to be shown?

dongzu3511
dongzu3511 感谢您的github项目。它对我很有用,我可以将twig模板引擎连接到我的应用程序。我是php的新手,并且一开始并没有得到我不能简单地得到并且运行苗条引导的twig引擎。它必须是一些称为引擎的包装器。
4 年多之前 回复
dsw7547
dsw7547 你检查过github.com/slimphp/Twig-View吗?
4 年多之前 回复
dsoy71058
dsoy71058 放入error_report并将twig的debug设置为true
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐