addPath('.'); // current working directory global $twig; $twig = new Twig_Environment($loader, array( #'cache' => $src.'/cache', # disable cache 'debug' => true, 'auto_reload' => true, )); $twig->addGlobal('homepage_path', '/~koeppel/'); $twig->addGlobal('asset_path', '/~koeppel/src'); // these global vars shall be replaced in future by this dict: $twig->addGlobal('paths', array( 'home_fias' => 'https://fias.uni-frankfurt.de/~koeppel', 'home_itp' => 'https://itp.uni-frankfurt.de/~koeppel', # etc. )); // add global custom functions $twig->addFunction(new Twig_SimpleFunction('yamlfile', function($filename) { require_once dirname(__FILE__).'/yaml-reader.php'; // my workaround $data = read_yamlfile($filename); return $data; })); function pdfthumb($uri_or_args) { include_once dirname(__FILE__).'/pdf/lib.php'; $arg = is_string($uri_or_args) ? array('url'=>$uri_or_args) : $uri_or_args; try { $promise = new PdfPromiseThumb($arg); $promise->thumb->check_arg(); // BUG: URL still produces "page_from=1" but Idk where they come from return $promise->get_some_url(); } catch(PdfException $e) { return "ERROR when creating Thumbnail URL: $e"; } // old way: // return get_pdf_preview($uri, $size, $page, $page_from); } $twig->addFunction(new Twig_SimpleFunction('pdfthumb', 'pdfthumb')); $twig->addFunction(new Twig_SimpleFunction('active', function($pattern_or_bool) { $bool = is_string($pattern_or_bool) ? preg_match($pattern_or_bool, $_SERVER['REQUEST_URI']) : $pattern_or_bool; return $bool ? ' class="active"' : ''; }, array('is_safe' => array('html')) )); $twig->addFunction(new Twig_SimpleFunction('parse_ini_file', 'parse_ini_file')); $twig->addFilter(new Twig_SimpleFilter('markdown', function($source) { require_once dirname(__FILE__).'/lib/Markdown/Markdown.inc.php'; $html = \Michelf\Markdown::defaultTransform($source); return $html; }, array('is_safe' => array('html') ) )); // code publication treatement require_once dirname(__FILE__).'/code-viewer/PublicCode.lib.php'; PublicCode::add(__FILE__); # geht gut: $twig->addFunction(new Twig_SimpleFunction('public_code_link', array('PublicCode', 'get_secure_link'))); # geht nicht: #$twig->addFunction(new Twig_SimpleFunction('public_code', array('PublicCode', 'twig_helper')),# # array('needs_context' => true, 'needs_environment' => true) #); // search for local functions in the directory if(is_file('./template.php')) { require_once './template.php'; } $twig->addExtension(new Twig_Extension_Debug()); function render($filename, $vars=array()) { global $twig, $warnings; $twig->addGlobal('rendering_filename', $filename); // just for fun in base.html $twig->addGlobal('global_warnings', $warnings); # geht auch: $twig->addGlobal('PublicCode', PublicCode::getInstance()); try { echo $twig->render($filename, $vars); } catch(Exception $e) { echo "Failed twig rendering of $filename:
"; var_dump($e->getMessage()); } } // For non-fatal but important warnings. Appear ontop of the pages. static $warnings = array(); function warn($text) { global $warnings; $warnings[] = $text; } // "compatibility mode" usage, eg. from plain PHP files without // a twig template. // this was no more tested actually function php_page($title) { global $twig; $twig->addGlobal('title', $title); ob_start(); register_shutdown_function('php_end_page'); } function php_end_page() { $content = ob_get_contents(); ob_end_clean(); } // auxillary helper functions function slugify($text) { // helper function $text = preg_replace('~[^\\pL\d]+~u', '-', $text); $text = trim($text, '-'); $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); $text = strtolower($text); $text = preg_replace('~[^-\w]+~', '', $text); return empty($text) ? null : $text; } // helper function if(!function_exists('flatten')) { function flatten(array $array) { $return = array(); array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; }); return $return; } }