Preview::render PHP Method

render() public method

Render template
public render ( $file, $mime = NULL, array $hive = NULL, $ttl ) : string
$file string
$mime string
$hive array array
$ttl int
return string
    function render($file, $mime = NULL, array $hive = NULL, $ttl = 0)
    {
        $fw = Base::instance();
        $cache = Cache::instance();
        if ($mime) {
            $this->mime = $mime;
        } elseif (!$this->mime) {
            $this->mime = 'text/html';
        }
        if (!is_dir($tmp = $fw->get('TEMP'))) {
            mkdir($tmp, Base::MODE, TRUE);
        }
        foreach ($fw->split($fw->get('UI')) as $dir) {
            if ($cache->exists($hash = $fw->hash($dir . $file), $data)) {
                return $data;
            }
            if (is_file($view = $fw->fixslashes($dir . $file))) {
                if (!is_file($this->view = $tmp . $fw->get('SEED') . '.' . $fw->hash($view) . '.php') || filemtime($this->view) < filemtime($view)) {
                    // Remove PHP code and comments
                    $text = preg_replace('/\\h*<\\?(?!xml)(?:php|\\s*=)?.+?\\?>\\h*' . '|\\{\\*.+?\\*\\}/is', '', $fw->read($view));
                    if (method_exists($this, 'parse')) {
                        $text = $this->parse($text);
                    }
                    $fw->write($this->view, $this->build($text));
                }
                if (isset($_COOKIE[session_name()]) && !headers_sent() && session_status() != PHP_SESSION_ACTIVE) {
                    session_start();
                }
                $fw->sync('SESSION');
                if (!$fw->get('CLI') && !headers_sent()) {
                    header('Content-Type: ' . $this->mime . '; ' . 'charset=' . $fw->get('ENCODING'));
                }
                $data = $this->sandbox($hive);
                if (isset($this->trigger['afterrender'])) {
                    foreach ($this->trigger['afterrender'] as $func) {
                        $data = $fw->call($func, $data);
                    }
                }
                if ($ttl) {
                    $cache->set($hash, $data, $ttl);
                }
                return $data;
            }
        }
        user_error(sprintf(Base::E_Open, $file), E_USER_ERROR);
    }