kenjis / codeigniter-ss-twig

A Simple and Secure Twig integration for CodeIgniter 3.x and 4.x
MIT License
168 stars 46 forks source link

Register all user defined functions #35

Closed waptik closed 6 years ago

waptik commented 7 years ago

Hi I'm trying to register all user defined functions in twig but it seems not to work. Here's my approach:

            // begins here
                $this->CI =& get_instance();
            $this->CI->load->helper('array');

             foreach ( element('user', get_defined_functions()) as $v){
                //echo $v;
                //echo 
                $this->funcx($v);
            }

        /**
        * Register a function in Twig environment
        *
        **/
             public function funcx($name)
    {
        $this->twig->addFunction(
        new Twig_SimpleFunction(
        $name,
        $name
        ));

        return $this;
    }
// ends here

I have defined functions such as script() assets() doo() inside a helper file which has been loaded. In my template I used {{ doo() }} as an example but I get this error message :

//begins here
An uncaught Exception was encountered

Type: Twig_Error_Syntax

Message: Unknown "doo" function.
// ends here
kenjis commented 6 years ago

See https://github.com/kenjis/codeigniter-ss-twig#adding-your-functions.

waptik commented 6 years ago

Sorry I forgot to say I found a way around to fix it. Anyway thanks for the answer