我的问题是:如何允许在Silex中的Twig模板中使用debug
我在玩Silex微框架(一个利用Symfony的PHP框架)。
当使用Twig模板系统时,我想输出一个特定的对象。通常我会用var_dump($app);
来做这件事,而在Twig中用{% debug app %}
来做。
我的问题是调试(将Silex自己的调试设置为true
对Twig没有帮助)以使用Silex。开箱即用呼叫debug
将导致错误消息:
Twig_Error_Syntax: Unknown tag name "debug" in...
调试调用如下所示:
{% debug app %}
我找到了如何配置Twig的config.yml文件以正确使用debug
的参考资料,但Silex没有为Twig使用配置文件。
Silex确实表示,您可以通过将关联数组传递给twig.options
来设置选项,而Twig文档则表示您可以传递以下环境选项:
$twig = new Twig_Environment($loader, array('debug' => true));
试图在Silex中通过类似:
$app->register(new Silex'Provider'TwigServiceProvider(), array(
'twig.options' => array('debug' => true),
));
不起作用。这是错误的选择吗?只是格式不正确?我不知道,也没有什么我尝试过的作品。
我感觉自己进入了"车轮旋转"模式,所以我在so上提问,希望今天早上我能继续做更有效率的工作。:)
(呃……对于一个超特定的StackOverflow问题,情况如何?)
解决方案:(所有这些只是为了获得类似var_dump
的功能…哦,我的):老实说,这有点让人头疼,Silex的医生在发现这一点时没有任何帮助,但以下是我必须做的事情,才能使这一点发挥作用。
首先加载Twig自动加载器:
$app['autoloader']->registerPrefixes(array(
'Twig_Extensions_' => array(__DIR__.'/vendor/Twig-extensions/lib')));
为什么你必须这样注册?不知道。它实际上是如何找到自动加载器的?不知道。但它是有效的。
注册提供程序并设置调试选项:
$app->register(new Silex'Provider'TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
'twig.class_path' => __DIR__.'/vendor/Twig/lib',
'twig.options' => array('debug' => true), //<-- this is the key line to getting debug added to the options
));
最后(最好的部分):
$oldTwigConfiguration = isset($app['twig.configure']) ? $app['twig.configure']: function(){};
$app['twig.configure'] = $app->protect(function($twig) use ($oldTwigConfiguration) {
$oldTwigConfiguration($twig);
$twig->addExtension(new Twig_Extensions_Extension_Debug());
});
老实说,我认为Silex对我来说已经足够了。
这个解决方案的功劳归于Nerdpress。
*忍者编辑:一年半后,我不得不说Silex对我来说是个哑弹。我一直在使用Slim来满足所有微框架的需求,这太棒了。快速、干净、简单、轻松地完成工作。
我完全删除了旧的答案,以显示我构建的一个小示例应用程序的输出:
composer.json:
{
"require": {
"silex/silex": "1.*",
"twig/twig": "1.*",
"twig/extensions": "*"
}
}
app.php:
require_once __DIR__ . '/../vendor/.composer/autoload.php';
$app = new Silex'Application();
$app['debug'] = true;
$app->register(new Silex'Provider'TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/views',
'twig.options' => array('debug' => true)
));
$app['twig']->addExtension(new Twig_Extensions_Extension_Debug());
$app->get('/', function () use ($app) {
return $app['twig']->render('debug_test.twig', array('app' => $app));
});
$app->run();
对于silex^2.2使用丘疹3的share()已被删除,因此使用:
$app->extend('twig', function($twig, $app) {
$twig->addExtension(new Twig_Extension_Debug());
return $twig;
});
已经有一段时间了,所以我对接受的答案做了一个小的更新,你可以使用Pimple:的新扩展方法
composer.json:
"silex/silex": "~1.3",
"twig/twig": "^1.23",
"symfony/twig-bridge": "^2.7",
"twig/extensions": "^1.3"
index.php(前控制器)
/*
* config
*/
//...
$app->register(new Silex'Provider'TwigServiceProvider(), array(
'twig.path' => __DIR__.'/../templets',
'twig.options' => array('debug' => true),
)
);
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$twig->addExtension(new Twig_Extension_Debug());
return $twig;
}));
//...
/*
* some static page
*/
$app->get('/', function () use($app) {
return $app['twig']->render('index.twig');
});
$app->run();