twig - pass function into template
问题 Currently I place my function in a class and pass an instance of this class into template and call my required function as a class method. {{ unneededclass.blah() }} I need to do like below {{ blah() }} Is it possible? 回答1: Update 5/14/2015 Commenters point out that I'm mostly wrong. If you really need a function, and not a filter or macro, you can do it as suggested in the Twig docs: $twig = new Twig_Environment($loader); $function = new Twig_SimpleFunction('blah', function () { // ... });