dongqiao3214
2015-08-07 13:07
浏览 22
已采纳

不能使树枝模板沙箱工作

I want to make a sandbox using Twig templating.

This is my dir structure:

* cache/ - writable
* templates/
  * index.html
* vendor/
  * Twig - twig's content from GitHub
* index.php

The problem I'm having is that $twig->render isn't generating any output and I don't know why. I'm trying to follow the basics tutorial.

this is my index.php file:

<?php

$twig_lib = __DIR__ . '/vendor/Twig/lib/Twig';
$twig_templates = __DIR__ . '/templates';
$twig_cache = __DIR__ . '/cache'; // remember to `chmod 777 cache` (make this directory writable)

require_once $twig_lib . '/Autoloader.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem($twig_templates);
$twig = new Twig_Environment($loader, array(
    'cache' => $twig_cache,
));

$output = $twig->render('index.html', array(
    'a_variable' => 'Hello World',
    'navigation' => array(
        array(
            'href' => 'http://twig.sensiolabs.org/doc/intro.html#basic-api-usage',
            'caption' => 'Basic Twig usage'
        ),
        array(
            'href' => 'http://twig.sensiolabs.org/',
            'caption' => 'Twig main webpage'
        )
    )
));

echo $output;
var_dump($output);

$output is just an empty string here. Apache WWW server is not throwing any errors (checked in logs). Twig itself is loaded correctly, all paths are configured correctly as well.

This is my index.html (same as in the tutorial):

<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <ul id="navigation">
        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
        {% endfor %}
        </ul>

        <h1>My Webpage</h1>
        {{ a_variable }}
    </body>
</html>

Please tell me what am I doing wrong.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongqin1861 2015-08-12 07:34
    已采纳

    That was just me not looking at what I should look at. The cache directory was generated, when index.html was empty. I should have cleared the cache - that's all...

    已采纳该答案
    打赏 评论
  • douguomou5094 2015-08-07 14:02

    The tutorial page you linked to suggests you should require composer's autoloader: require_once 'vendor/autoloader.php';.

    You are loading a nonexistant file: require_once '/vendor/Twig/lib/Twig/Autoload.php'; This file does not exist using the default composer installation of Twig.

    EDIT: I've found an autoloader at vendor/twig/twig/lib/Twig/Autoloader.php which is slightly different than your path.

    It sounds like you have error reporting off. You can turn it on with this:

    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);
    
    打赏 评论

相关推荐 更多相似问题