简体   繁体   English

如何渲染Twig模板而不将任何数组传递给render()方法?

[英]How to render a Twig template without passing any array to render() method?

I am using Twig a few days, and it's very nice, but now I am in a situation where I need to render a template without passing any array to the render() method. 我正在使用Twig几天,它非常好,但现在我处于一种情况,我需要渲染模板而不将任何数组传递给render()方法。

I am using: 我在用:

echo $template->render();

but Twig raises an exception because I NEED to pass some array to the render. 但Twig引发了异常,因为我需要将一些数组传递给渲染。 So I tried: 所以我尝试过:

echo $template->render(array(''=>''));

and it works, but it's so ugly. 它有效,但它太难看了。

Any tips about this? 关于这个的任何提示? Thanks! 谢谢!

Instead of passing an array that contains an empty string element, you should also be able to pass the template an empty array: 您还应该能够将模板传递给一个空数组,而不是传递包含空字符串元素的数组:

echo $template->render(array());

Apart from that, you are probably using Twig this way: 除此之外,你可能会这样使用Twig:

$template = $twig->loadTemplate('index.html.twig');
echo $template->render(array('the' => 'variables', 'go' => 'here'));

The Twig documentation suggests an easier way to do the same: Twig文档提供了一种更简单的方法:

echo $twig->render('index.html.twig', array('the' => 'variables', 'go' => 'here'));

The additional benefit is that the render method of the Twig_Environment class allows you to omit the second argument (it defaults to an empty array), so now you can simply do this: 另外一个好处是Twig_Environment类的render方法允许你省略第二个参数(它默认为一个空数组),所以现在你可以简单地这样做:

echo $twig->render('index.twig.html');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM