/**
  * Initialise twig with his correct configuration
  * @return \Twig_Environment Twig initialised
  */
 function twigInit()
 {
     $loader = new \Twig_Loader_Filesystem(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'template');
     $twig = new \Twig_Environment($loader, array('cache' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'cache', 'debug' => true));
     //Call static method
     $function = new \Twig_SimpleFunction('static_method', function ($class, $method, $arguments) {
         return call_user_func_array($class . '::' . $method, $arguments);
     });
     $twig->addFunction($function);
     //Generate an url
     $generate = new \Twig_SimpleFunction('url', function ($controller = "", $action = "", $arguments = []) {
         return Controller::generateURL($controller, $action, $arguments);
     });
     $twig->addFunction($generate);
     //Fix Riot HTML
     $close_tag = new \Twig_SimpleFunction('close_tag', function ($html) {
         //Some items have incorrect html
         $html_new = $html;
         preg_match_all("#<(?!br)([a-z]+)( .*)?(?!/)>#iU", $html, $result1);
         preg_match_all("#</([a-z]+)>#iU", $html, $result2);
         $results_start = $result1[1];
         $results_end = $result2[1];
         foreach ($results_start as $startag) {
             if (!in_array($startag, $results_end)) {
                 $html_new = str_replace('<' . $startag . '>', '', $html_new);
             }
         }
         foreach ($results_end as $endtag) {
             if (!in_array($endtag, $results_start)) {
                 $html_new = str_replace('</' . $endtag . '>', '', $html_new);
             }
         }
         return $html_new;
     });
     $twig->addFunction($close_tag);
     return $twig;
 }