1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-27 00:40:52 +01:00
vichan/inc/template.php

73 lines
2.3 KiB
PHP
Raw Normal View History

2010-11-02 11:57:33 +01:00
<?php
2012-04-11 18:49:22 +02:00
/*
2013-01-20 11:23:46 +01:00
* Copyright (c) 2010-2013 Tinyboard Development Group
2012-04-11 18:49:22 +02:00
*/
2012-04-12 16:18:19 +02:00
if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
2012-04-11 18:49:22 +02:00
// You cannot request this file directly.
exit;
}
$twig = false;
function load_twig() {
global $twig, $config;
2010-11-02 11:57:33 +01:00
2012-04-09 12:52:26 +02:00
require 'lib/Twig/Autoloader.php';
2011-10-05 06:22:53 +02:00
Twig_Autoloader::register();
2012-04-11 18:49:22 +02:00
2011-10-11 12:49:14 +02:00
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');
2011-10-05 06:22:53 +02:00
$loader = new Twig_Loader_Filesystem($config['dir']['template']);
2012-04-11 18:49:22 +02:00
$loader->setPaths($config['dir']['template']);
$twig = new Twig_Environment($loader, array(
2012-04-11 18:49:22 +02:00
'autoescape' => false,
'cache' => "{$config['dir']['template']}/cache",
2012-05-20 12:20:50 +02:00
'debug' => $config['debug']
2012-04-11 18:49:22 +02:00
));
$twig->addExtension(new Twig_Extensions_Extension_Tinyboard());
$twig->addExtension(new Twig_Extensions_Extension_I18n());
}
function Element($templateFile, array $options) {
global $config, $debug, $twig;
2010-11-02 11:57:33 +01:00
2012-04-12 16:18:19 +02:00
if (!$twig)
2012-04-11 18:49:22 +02:00
load_twig();
2012-04-16 09:28:57 +02:00
if (function_exists('create_pm_header') && ((isset($options['mod']) && $options['mod']) || isset($options['__mod'])) && !preg_match('!^mod/!', $templateFile)) {
2012-04-11 18:49:22 +02:00
$options['pm'] = create_pm_header();
}
2012-04-12 16:18:19 +02:00
if (isset($options['body']) && $config['debug']) {
if (isset($debug['start'])) {
2012-04-11 18:49:22 +02:00
$debug['time'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms';
unset($debug['start']);
2011-03-17 06:52:43 +01:00
}
2012-04-11 18:49:22 +02:00
$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
2012-04-12 16:18:19 +02:00
if (@file_get_contents("{$config['dir']['template']}/${templateFile}")) {
2012-04-11 18:49:22 +02:00
$body = $twig->render($templateFile, $options);
2011-03-17 06:52:43 +01:00
2012-04-12 16:18:19 +02:00
if ($config['minify_html'] && preg_match('/\.html$/', $templateFile)) {
2012-04-11 18:49:22 +02:00
$body = trim(preg_replace("/[\t\r\n]/", '', $body));
2011-05-21 07:22:10 +02:00
}
2012-04-11 18:49:22 +02:00
return $body;
} else {
throw new Exception("Template file '${templateFile}' does not exist or is empty in '{$config['dir']['template']}'!");
2010-11-02 11:57:33 +01:00
}
2012-04-11 18:49:22 +02:00
}