From ab23338fe9f1b3ed21455867f1c032d7b146ceb8 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 2 Mar 2015 16:51:04 +0100 Subject: Initial Commit --- inc/render.inc.php | 221 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 inc/render.inc.php (limited to 'inc/render.inc.php') diff --git a/inc/render.inc.php b/inc/render.inc.php new file mode 100644 index 0000000..0147709 --- /dev/null +++ b/inc/render.inc.php @@ -0,0 +1,221 @@ + + + + ', RENDER_DEFAULT_TITLE, self::$title, ' + + + + + + + ', + self::$header + , + ' + +
+ ', + self::$body + , + '
+ + + ', + self::$footer + , + ' + ' + ; + if ($zip) { + Header('Content-Encoding: gzip'); + ob_implicit_flush(false); + $gzip_contents = ob_get_contents(); + ob_end_clean(); + echo "\x1f\x8b\x08\x00\x00\x00\x00\x00"; + echo substr(gzcompress($gzip_contents, 5), 0, -4); + } + } + + /** + * Set the page title (title-tag) + */ + public static function setTitle($title) + { + self::$title = ' - ' . $title; + } + + /** + * Add raw html data to the header-section of the generated page + */ + public static function addHeader($html) + { + self::$header .= $html . "\n"; + } + + /** + * Add raw html data to the footer-section of the generated page (right before the closing body tag) + */ + public static function addFooter($html) + { + self::$footer .= $html . "\n"; + } + + /** + * Add given js script file from the script directory to the header + * + * @param string $file file name of script + */ + public static function addScriptTop($file) + { + self::addHeader(''); + } + + /** + * Add given js script file from the script directory to the bottom + * + * @param string $file file name of script + */ + public static function addScriptBottom($file) + { + self::addFooter(''); + } + + /** + * Add the given template to the output, using the given params for placeholders in the template + */ + public static function addTemplate($template, $params = false) + { + self::$body .= self::parse($template, $params); + } + + /** + * Add a dialog to the page output. + * + * @param string $title Title of the dialog window + * @param boolean $next URL to next dialog step, or false to hide the next button + * @param string $template template used to fill the dialog body + * @param array $params parameters for rendering the body template + */ + public static function addDialog($title, $next, $template, $params = false) + { + self::addTemplate('dialog-generic', array( + 'title' => $title, + 'next' => $next, + 'body' => self::parse($template, $params) + )); + } + + /** + * Add error message to page + */ + public static function addError($message) + { + self::addTemplate('messagebox-error', array('message' => $message)); + } + + /** + * Parse template with given params and return; do not add to body + * @param string $template name of template, relative to templates/, without .html extension + * @return string Rendered template + */ + public static function parse($template, $params = false) + { + // Load html snippet + $html = self::getTemplate($template); + // Always add token to parameter list + if (is_array($params) || $params === false || is_null($params)) + $params['token'] = Session::get('token'); + // Return rendered html + return self::$mustache->render($html, $params); + } + + /** + * Open the given html tag, optionally adding the passed assoc array of params + */ + public static function openTag($tag, $params = false) + { + array_push(self::$tags, $tag); + if (!is_array($params)) { + self::$body .= '<' . $tag . '>'; + } else { + self::$body .= '<' . $tag; + foreach ($params as $key => $val) { + self::$body .= ' ' . $key . '="' . htmlspecialchars($val) . '"'; + } + self::$body .= '>'; + } + } + + /** + * Close the given tag. Will check if it maches the tag last opened + */ + public static function closeTag($tag) + { + if (empty(self::$tags)) + Util::traceError('Tried to close tag ' . $tag . ' when no open tags exist.'); + $last = array_pop(self::$tags); + if ($last !== $tag) + Util::traceError('Tried to close tag ' . $tag . ' when last opened tag was ' . $last); + self::$body .= ''; + } + + /** + * Private helper: Load the given template and return it + */ + private static function getTemplate($template) + { + if (isset(self::$templateCache[$template])) { + return self::$templateCache[$template]; + } + // Load from disk + $data = @file_get_contents('templates/' . $template . '.html'); + if ($data === false) + $data = 'Non-existent template ' . $template . ' requested!'; + self::$templateCache[$template] = & $data; + return $data; + } + +} -- cgit v1.2.3-55-g7522