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 fesafe材料库问题
    • ¥35 beats蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统