Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. *
  5. *
  6. * Модель работы шаблонизатора twig
  7. *
  8. *
  9. */
  10.  
  11. class M_Template {
  12. private static $instance = NULL;
  13. private $_attr = array();
  14. private $_conf = array();
  15.  
  16. public function __construct($attr, $conf) {
  17. $this->_attr = array_merge($this->_attr, $attr);
  18. $this->_conf = array_merge($this->_conf, $conf);
  19.  
  20. try {
  21. // Подгружаем и активируем авто-загрузчик Twig-а
  22. require_once ROOT_PROJECT . '/app/twig/lib/Twig/Autoloader.php';
  23. Twig_Autoloader::register();
  24.  
  25. // Указываем где хранятся шаблоны
  26. $loader = new Twig_Loader_Filesystem('themes');
  27.  
  28. // Инициализируем Twig
  29. $twig = new Twig_Environment($loader, array(
  30. 'cache' => 'app/compilation_cache',
  31. 'auto_reload' => true // отключаем кэш
  32. ));
  33.  
  34. // Подгружаем шаблон
  35. $template = $twig->loadTemplate($this->_conf['tmpl'] . '/' . $this->_conf['page'] . '.twig');
  36.  
  37. // Передаём в шаблон переменные и значения
  38. // Выводим сформированное содержание
  39. echo $template->render($this->_attr);
  40. } catch (Exception $e) {
  41. throw new Exception('Ошибка: ' . $e->getMessage());
  42. }
  43. }
  44.  
  45. public static function init($attr = array(), $conf = array()) {
  46. if(self::$instance === NULL)
  47. self::$instance = new M_Template($attr, $conf);
  48. return self::$instance;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement