无法使树枝模板沙箱工作
can't make twig templating sandbox work
我想使用 Twig templating 制作一个沙箱。
这是我的目录结构:
* cache/ - writable
* templates/
* index.html
* vendor/
* Twig - twig's content from GitHub
* index.php
我遇到的问题是 $twig->render 没有生成任何输出,我不知道为什么。我正在尝试关注 basics tutorial.
这是我的 index.php 文件:
<?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 在这里只是一个空字符串。 Apache WWW 服务器没有抛出任何错误(已在日志中检查)。 Twig 本身已正确加载,所有路径也已正确配置。
这是我的index.html(与教程中的相同):
<!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>
请告诉我我做错了什么。
tutorial page you linked to 建议您需要 composer 的自动加载器:require_once 'vendor/autoloader.php';
。
您正在加载一个不存在的文件:require_once '/vendor/Twig/lib/Twig/Autoload.php';
使用 Twig 的默认作曲家安装不存在该文件。
编辑:我在 vendor/twig/twig/lib/Twig/Autoloader.php
找到了一个自动加载器,它与您的路径略有不同。
听起来您关闭了错误报告。 You can turn it on with this:
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
那是我没看我该看的。缓存目录生成,当index.html为空时。我应该清除缓存 - 仅此而已...
我想使用 Twig templating 制作一个沙箱。
这是我的目录结构:
* cache/ - writable
* templates/
* index.html
* vendor/
* Twig - twig's content from GitHub
* index.php
我遇到的问题是 $twig->render 没有生成任何输出,我不知道为什么。我正在尝试关注 basics tutorial.
这是我的 index.php 文件:
<?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 在这里只是一个空字符串。 Apache WWW 服务器没有抛出任何错误(已在日志中检查)。 Twig 本身已正确加载,所有路径也已正确配置。
这是我的index.html(与教程中的相同):
<!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>
请告诉我我做错了什么。
tutorial page you linked to 建议您需要 composer 的自动加载器:require_once 'vendor/autoloader.php';
。
您正在加载一个不存在的文件:require_once '/vendor/Twig/lib/Twig/Autoload.php';
使用 Twig 的默认作曲家安装不存在该文件。
编辑:我在 vendor/twig/twig/lib/Twig/Autoloader.php
找到了一个自动加载器,它与您的路径略有不同。
听起来您关闭了错误报告。 You can turn it on with this:
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
那是我没看我该看的。缓存目录生成,当index.html为空时。我应该清除缓存 - 仅此而已...