Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- *
- *
- * Модель работы шаблонизатора twig
- *
- *
- */
- class M_Template {
- private static $instance = NULL;
- private $_attr = array();
- private $_conf = array();
- public function __construct($attr, $conf) {
- $this->_attr = array_merge($this->_attr, $attr);
- $this->_conf = array_merge($this->_conf, $conf);
- try {
- // Подгружаем и активируем авто-загрузчик Twig-а
- require_once ROOT_PROJECT . '/app/twig/lib/Twig/Autoloader.php';
- Twig_Autoloader::register();
- // Указываем где хранятся шаблоны
- $loader = new Twig_Loader_Filesystem('themes');
- // Инициализируем Twig
- $twig = new Twig_Environment($loader, array(
- 'cache' => 'app/compilation_cache',
- 'auto_reload' => true // отключаем кэш
- ));
- // Подгружаем шаблон
- $template = $twig->loadTemplate($this->_conf['tmpl'] . '/' . $this->_conf['page'] . '.twig');
- // Передаём в шаблон переменные и значения
- // Выводим сформированное содержание
- echo $template->render($this->_attr);
- } catch (Exception $e) {
- throw new Exception('Ошибка: ' . $e->getMessage());
- }
- }
- public static function init($attr = array(), $conf = array()) {
- if(self::$instance === NULL)
- self::$instance = new M_Template($attr, $conf);
- return self::$instance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement