Advertisement
scotepi

Twig Template Class

May 1st, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. <?php
  2. /*
  3.  
  4. $template = new template(array(
  5.     'all_head.twig', 'page.twig', 'all_foot.twig'),
  6.     'template/', '/tmp');
  7. $template->addJS('script.js');      # Also Takes arrays();
  8. $template->addCSS('style.css');     # Also Takes arrays();
  9. $template->title = 'Test Template and Twig';
  10. echo $template;
  11.  
  12. all_head.twig
  13. ...
  14.     <title>{{ title }}</title>
  15.    
  16.     <!-- Start JS -->
  17. {% for js in javascript %}
  18.     <script type="text/javascipt" src="{{ js }}"></script>
  19. {% endof %}
  20.     <!-- End JS -->
  21. ...
  22.  
  23.  
  24. */
  25.  
  26. class template {
  27.    
  28.     var $rendered = false;
  29.     var $twig = null;
  30.     var $pages = array();
  31.     var $data = array();
  32.    
  33.     function __construct($pages, $path, $cache=false) {
  34.                
  35.         // Save the pages
  36.         if (!is_array($pages))  $pages = array($pages);
  37.         $this->pages = $pages;
  38.        
  39.         // Check that the pages are real
  40.         foreach ($this->pages as $page) {
  41.             if (!is_file($path.'/'.$page)) {
  42.                 die("Template file <em>$page</em> not found");
  43.             }
  44.         }
  45.        
  46.         // Load Twig
  47.         require_once "Twig/lib/Twig/Autoloader.php";
  48.         Twig_Autoloader::register();
  49.        
  50.         $twigLoader = new Twig_Loader_Filesystem($path);
  51.         $this->twig = new Twig_Environment($twigLoader, array(
  52.             'cache' =>  $cache,
  53.         ));
  54.        
  55.         // Set up data
  56.         $this->data = array(
  57.             'javascript' => array(),
  58.             'css' => array(),
  59.         );
  60.            
  61.     }
  62.    
  63.     function render($return=false) {
  64.         if (!$this->rendered) {
  65.             if ($return) {
  66.                 $ret = '';
  67.                 foreach ($this->pages as $page) {
  68.                     $ret .= $this->twig->render($page,  $this->data);  
  69.                 }
  70.             } else {
  71.                 $ret = true;
  72.                 foreach ($this->pages as $page) {
  73.                     echo $this->twig->render($page, $this->data);  
  74.                 }
  75.             }
  76.            
  77.            
  78.             $this->rendered = true;
  79.             return $ret;
  80.         } else {
  81.             return false;  
  82.         }  
  83.     }
  84.    
  85.     function __toString() {
  86.         return $this->render(true);
  87.     }
  88.    
  89.     function addJavascript($path) {
  90.         if (is_array($path)) {
  91.             $this->data['javascript'] = array_merge($this->data['javascript'], $path); 
  92.         } else {
  93.             $this->data['javascript'][] = $path;
  94.         }
  95.     }
  96.    
  97.     function addJS($path) {
  98.         $this->addJavascript($path);   
  99.     }
  100.    
  101.     function addCSS($path) {
  102.         if (is_array($path)) {
  103.             $this->data['css'] = array_merge($this->data['css'], $path);   
  104.         } else {
  105.             $this->data['css'][] = $path;
  106.         }
  107.     }
  108.    
  109.     function __set($key, $val) {
  110.         $this->data[$key] = $val;
  111.     }
  112.    
  113.     function __get($key) {
  114.         echo "get $key";
  115.         return $this->data[$key] = $val;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement