summaryrefslogtreecommitdiff
path: root/inc/template.php
diff options
context:
space:
mode:
authorMichael Save <savetheinternet@tinyboard.org>2012-04-12 02:49:22 +1000
committerMichael Save <savetheinternet@tinyboard.org>2012-04-12 02:49:22 +1000
commitc9423a2c34a5b93bf9cf2c506acca713781e60b6 (patch)
tree16f4a569d34fc67365b453bafe561524fce87b22 /inc/template.php
parent88092e4f9e50e94f094e55c54a6150df104043b1 (diff)
large cleanup
Diffstat (limited to 'inc/template.php')
-rw-r--r--inc/template.php97
1 files changed, 56 insertions, 41 deletions
diff --git a/inc/template.php b/inc/template.php
index 2a79f918..b348f153 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -1,57 +1,72 @@
<?php
- if($_SERVER['SCRIPT_FILENAME'] == str_replace('\\', '/', __FILE__)) {
- // You cannot request this file directly.
- header('Location: ../', true, 302);
- exit;
- }
+
+/*
+ * Copyright (c) 2010-2012 Tinyboard Development Group
+ */
+
+if(realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
+ // You cannot request this file directly.
+ exit;
+}
+
+$twig = false;
+
+function load_twig() {
+ global $twig, $config;
require 'lib/Twig/Autoloader.php';
Twig_Autoloader::register();
-
+
Twig_Autoloader::autoload('Twig_Extensions_Node_Trans');
Twig_Autoloader::autoload('Twig_Extensions_TokenParser_Trans');
Twig_Autoloader::autoload('Twig_Extensions_Extension_I18n');
Twig_Autoloader::autoload('Twig_Extensions_Extension_Tinyboard');
$loader = new Twig_Loader_Filesystem($config['dir']['template']);
+ $loader->setPaths($config['dir']['template']);
+ $twig = new Twig_Environment($loader, Array(
+ 'autoescape' => false,
+ 'cache' => "{$config['dir']['template']}/cache",
+ 'debug' => ($config['debug'] ? true : false),
+ ));
+ $twig->addExtension(new Twig_Extensions_Extension_Tinyboard());
+ $twig->addExtension(new Twig_Extensions_Extension_I18n());
+}
+
+function Element($templateFile, array $options) {
+ global $config, $debug, $twig;
- function Element($templateFile, array $options) {
- global $config, $debug, $loader;
-
- if(function_exists('create_pm_header') && ((isset($options['mod']) && $options['mod']) || isset($options['__mod']))) {
- $options['pm'] = create_pm_header();
+ if(!$twig)
+ load_twig();
+
+ if(function_exists('create_pm_header') && ((isset($options['mod']) && $options['mod']) || isset($options['__mod']))) {
+ $options['pm'] = create_pm_header();
+ }
+
+ if(isset($options['body']) && $config['debug']) {
+ if(isset($debug['start'])) {
+ $debug['time'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms';
+ unset($debug['start']);
}
+ $debug['included'] = get_included_files();
+ $debug['memory'] = round(memory_get_usage(true) / (1024 * 1024), 2) . ' MiB';
+ $options['body'] .=
+ '<h3>Debug</h3><pre style="white-space: pre-wrap;font-size: 10px;">' .
+ str_replace("\n", '<br/>', utf8tohtml(print_r($debug, true))) .
+ '</pre>';
+ }
+
+ // Read the template file
+ if(@file_get_contents("{$config['dir']['template']}/${templateFile}")) {
+ $body = $twig->render($templateFile, $options);
- if(isset($options['body']) && $config['debug']) {
- if(isset($debug['start'])) {
- $debug['time'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms';
- unset($debug['start']);
-
- }
- $options['body'] .= '<h3>Debug</h3><pre style="white-space: pre-wrap;font-size: 10px;">' . str_replace("\n", '<br/>', utf8tohtml(print_r($debug, true))) . '</pre>';
+ if($config['minify_html'] && preg_match('/\.html$/', $templateFile)) {
+ $body = trim(preg_replace("/[\t\r\n]/", '', $body));
}
- $loader->setPaths($config['dir']['template']);
-
- $twig = new Twig_Environment($loader, Array(
- 'autoescape' => false,
- 'cache' => "{$config['dir']['template']}/cache",
- 'debug' => ($config['debug'] ? true : false),
- ));
- $twig->addExtension(new Twig_Extensions_Extension_Tinyboard());
- $twig->addExtension(new Twig_Extensions_Extension_I18n());
-
- // Read the template file
- if(@file_get_contents("{$config['dir']['template']}/${templateFile}")) {
- $body = $twig->render($templateFile, $options);
-
- if($config['minify_html'] && preg_match('/\.html$/', $templateFile)) {
- $body = trim(preg_replace("/[\t\r\n]/", '', $body));
- }
-
- return $body;
- } else {
- throw new Exception("Template file '${templateFile}' does not exist or is empty in '{$config['dir']['template']}'!");
- }
+ return $body;
+ } else {
+ throw new Exception("Template file '${templateFile}' does not exist or is empty in '{$config['dir']['template']}'!");
}
-?>
+}
+