1

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.

ducin
  • 22,544
  • 38
  • 129
  • 239
  • I can't reproduce this - it seems to be working fine. I _did_ however, have to change the `require_once` line to point to a composer generated autoloader instead of using an autoloader found in the twig folder. – HPierce Aug 07 '15 at 13:49

2 Answers2

0

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...

ducin
  • 22,544
  • 38
  • 129
  • 239
-1

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);
Community
  • 1
  • 1
HPierce
  • 6,211
  • 5
  • 32
  • 43
  • this has nothing to do with aotu-loading. I'm fetching Twig from github and all bootstrapping works fine. – ducin Aug 12 '15 at 07:34