php - Mark all methods as "safe" -


i want create class full of html helper methods:

class formhelper {     public function text() {         return 'hello <b>world</b>';     } }    

which register global:

$twig->addglobal('fh',new formhelper); 

then can call methods twig:

{{ fh.text }} 

but they're escaped (e.g. hello &lt;b&gt;world&lt;/b&gt;).

i know can prevent escaping |raw, want bypass this, same way can designate function safe.

is possible class methods?

twig globals storage can put variable available in context. no logic applied, if variables stored in local context.

but can iterate class's methods , register them safe functions:

$object = new formhelper(); foreach (get_class_methods($object) $method) {     $function = new twig_simplefunction("fh_{$method}", array($object, $method), array('is_safe' => array('html')));     $twig->addfunction($function); } 

note can't use dot (.) in function name, you'll need call:

{{ fh_test() }} 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -