shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

vq2-system_library_template_twig.php (1003B)


      1 <?php
      2 namespace Template;
      3 final class Twig {
      4 	private $twig;
      5 	private $data = array();
      6 	
      7 	public function __construct() {
      8 		// include and register Twig auto-loader
      9 		include_once(\VQMod::modCheck(DIR_SYSTEM . 'library/template/Twig/Autoloader.php'));
     10 		
     11 		\Twig_Autoloader::register();
     12 	}
     13 	
     14 	public function set($key, $value) {
     15 		$this->data[$key] = $value;
     16 	}
     17 	
     18 	public function render($template, $cache = false) {
     19 		// specify where to look for templates
     20 		$loader = new \Twig_Loader_Filesystem(DIR_TEMPLATE);
     21 
     22 		// initialize Twig environment
     23 		$config = array('autoescape' => false);
     24 
     25 		if ($cache) {
     26 			$config['cache'] = DIR_CACHE;
     27 		}
     28 
     29 		$this->twig = new \Twig_Environment($loader, $config);
     30 		
     31 		try {
     32 			// load template
     33 			$template = $this->twig->loadTemplate($template . '.twig');
     34 			
     35 			return $template->render($this->data);
     36 		} catch (Exception $e) {
     37 			trigger_error('Error: Could not load template ' . $template . '!');
     38 			exit();	
     39 		}	
     40 	}	
     41 }