Файл: wboard/source/system/classes/template.php
Строк: 70
<?php
/**
 * Wboard
 * Templates handler
 * @author Screamer
 * @copyright 2013
 */
class Template
{
    /**
     * @var (string)
     * Directory with templates
     */
    protected $_directory = '';
    /**
     * @var (string) Data for output
     */
    protected $_output = '';
    /**
     * @var (string) Name of Main template
     */
    public $layot = '_layot';
    /**
     * @var (Language) Languages Handler
     */
    public $lng;
    /**
     * @var (string) Title of Page
     */
    public $title = '';
    /**
     * Constructor
     * @param (string) Path to templates directory
     * @param (Language) Languages handler
     * @throws (Exception) Wrong parameter given
     * @return (void)
     */
    public function __construct($directory, $lng)
    {
        $error = array();
        // Check directory with templates
        if (is_dir($directory)) {
            $this->_directory = realpath($directory);
        } else {
            $error[] = 'Directory "' . $directory . '" is not exists';
        }
        // Check layot for exists
        if (!empty($this->_directory) && realpath($this->_directory . DIRECTORY_SEPARATOR . $this->layot . '.php') === FALSE) {
            $error[] = 'Layot is not exists';
        }
        // Check Languages handler
        if (is_object($lng) && ($lng instanceof Language)) {
            $this->lng =& $lng;
        } else {
            $error[] = 'Unable to set language object';
        }
        if (!empty($error)) {
            throw new Exception(__CLASS__ . PHP_EOL . implode(PHP_EOL, $error));
        }
    }
    /**
     * Load template
     * @param (string) $file Name of file (without extension)
     * @param (array) $data Variables for template
     * @return (string)
     */
    public function load($file, array $data = array())
    {
        $template = realpath($this->_directory . DIRECTORY_SEPARATOR . $file . '.php');
        ob_start();
        if ($template !== FALSE) {
            if (!empty($data)) {
                extract($data);
            }
            include $template;
        } else {
            echo '<pre>Template Error: Unable to load «<b>' . $file . '</b>»</pre>';
        }
        $contents = ob_get_contents();
        ob_end_clean();
        return trim($contents);
    }
    /**
     * Set data for output
     * @param (string) $data Data for output
     * @return (void)
     */
    public function set_output($data)
    {
        $this->_output = $data;
    }
    /**
     * Set output data to main template
     * @param (array) $data Data for main Template
     * @return (string)
     */
    public function output($data = array())
    {
        $data['contents'] = $this->_output;
        return $this->load($this->layot, $data, 'system');
    }
}