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:


$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';

$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(
            'href' => '',
            'caption' => 'Basic Twig usage'
            'href' => '',
            'caption' => 'Twig main webpage'

echo $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>
        <title>My Webpage</title>
        <ul id="navigation">
        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
        {% endfor %}

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

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:

    打赏 评论

相关推荐 更多相似问题