douba8048 2016-06-12 19:45
浏览 81

使用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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器