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

79 lines
2.7 KiB
PHP
Raw Normal View History

2013-07-31 10:28:26 +02:00
<?php
/*
* Copyright (c) 2010-2013 Tinyboard Development Group
*/
defined('TINYBOARD') or exit;
2013-07-31 10:28:26 +02:00
$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' => is_writable('templates') && (!is_dir('templates/cache') || is_writable('templates/cache')) ?
"{$config['dir']['template']}/cache" : false,
2013-07-31 10:28:26 +02:00
'debug' => $config['debug']
));
$twig->addExtension(new Twig_Extensions_Extension_Tinyboard());
$twig->addExtension(new Twig_Extensions_Extension_I18n());
}
function Element($templateFile, array $options) {
2013-08-29 10:55:25 +02:00
global $config, $debug, $twig, $build_pages;
2013-07-31 10:28:26 +02:00
if (!$twig)
load_twig();
if (function_exists('create_pm_header') && ((isset($options['mod']) && $options['mod']) || isset($options['__mod'])) && !preg_match('!^mod/!', $templateFile)) {
$options['pm'] = create_pm_header();
}
if (isset($options['body']) && $config['debug']) {
$_debug = $debug;
2013-07-31 10:28:26 +02:00
if (isset($debug['start'])) {
$_debug['time']['total'] = '~' . round((microtime(true) - $_debug['start']) * 1000, 2) . 'ms';
$_debug['time']['init'] = '~' . round(($_debug['start_debug'] - $_debug['start']) * 1000, 2) . 'ms';
unset($_debug['start']);
unset($_debug['start_debug']);
2013-07-31 10:28:26 +02:00
}
2013-08-29 10:55:25 +02:00
if ($config['try_smarter'] && isset($build_pages) && !empty($build_pages))
$_debug['build_pages'] = $build_pages;
$_debug['included'] = get_included_files();
$_debug['memory'] = round(memory_get_usage(true) / (1024 * 1024), 2) . ' MiB';
$_debug['time']['db_queries'] = '~' . round($_debug['time']['db_queries'] * 1000, 2) . 'ms';
$_debug['time']['exec'] = '~' . round($_debug['time']['exec'] * 1000, 2) . 'ms';
2013-07-31 10:28:26 +02:00
$options['body'] .=
'<h3>Debug</h3><pre style="white-space: pre-wrap;font-size: 10px;">' .
str_replace("\n", '<br/>', utf8tohtml(print_r($_debug, true))) .
2013-07-31 10:28:26 +02:00
'</pre>';
}
// 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']}'!");
}
}