/** * Initializes the templating system. */ private static function initialize() { // Make sure it's not already initialized if (self::$initialized) { return; } // Load up and register Twig require_once APP_VENDOR_PATH . '/Twig/Autoloader.php'; Twig_Autoloader::register(); // Greate the environment $loader = new Twig_Loader_Filesystem(APP_VIEWS_PATH); $loader->addPath(APP_VIEWS_PATH . '/shared'); $loader->addPath(APP_VIEWS_PATH_CORE); $loader->addPath(APP_VIEWS_PATH_CORE . '/shared'); $twig = new Twig_Environment($loader, array('auto_reload' => true)); // Add in the custom twig functions // The getPath function turns controller endpoints into relative // urls that can be used in the interface. $getPathFunction = new Twig_SimpleFunction("get_path", function () { $args = func_get_args(); if (count($args) == 0) { return; } $controller = $args[0]; array_shift($args); return Routes::getControllerRelativeUrl($controller, $args); }); $twig->addFunction($getPathFunction); // Used to make images square with no distortion $avatarFunction = new Twig_SimpleFunction("avatar", function () { $args = func_get_args(); $addon = ''; if (count($args) == 2 && $args[1] == true) { $addon = '-small'; } else { if (count($args) > 1) { return; } } if (empty($args[0])) { $url = Routes::getControllerRelativeUrl('image#noavatar'); } else { $url = Routes::getControllerRelativeUrl('image#viewthumb', array($args[0])); } echo '<div class="avatar-wrapper"><div class="avatar' . $addon . '" style="background-image: url(\'' . $url . '\');"></div></div>'; }); $twig->addFunction($avatarFunction); // Used to make images square with no distortion $friendlyUrlFunction = new Twig_SimpleFunction("seo_friendly", function () { $args = func_get_args(); if (count($args) != 1) { return; } return Utils::makeSeoFriendly($args[0]); }); $twig->addFunction($friendlyUrlFunction); // The public function allows the interface to get public resources $publicResourceFunction = new Twig_SimpleFunction("public_path", function ($path) { echo APP_RELATIVE_URL . '/public/' . Routes::fixPath($path); }); $twig->addFunction($publicResourceFunction); // Set the reference self::$twig = $twig; self::$initialized = true; }