lainchan/inc/template.php

101 lines
3.6 KiB
PHP
Raw Normal View History

2010-11-02 06:57:33 -04:00
<?php
2011-04-13 08:21:07 -04:00
if($_SERVER['SCRIPT_FILENAME'] == str_replace('\\', '/', __FILE__)) {
// You cannot request this file directly.
header('Location: ../', true, 302);
exit;
}
2010-11-02 06:57:33 -04:00
2011-10-05 00:22:53 -04:00
require 'contrib/Twig/Autoloader.php';
2011-10-05 00:22:53 -04:00
Twig_Autoloader::register();
2010-11-02 06:57:33 -04:00
class Tinyboard_Twig_Extension extends Twig_Extension {
public function getFilters() {
return Array(
'filesize' => new Twig_Filter_Function('format_bytes', Array('needs_environment' => false)),
'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => false)),
'truncate_body' => new Twig_Filter_Function('truncate', array('needs_environment' => false)),
'extension' => new Twig_Filter_Function('twig_extension_filter', array('needs_environment' => false)),
'sprintf' => new Twig_Filter_Function('sprintf', array('needs_environment' => false)),
'capcode' => new Twig_Filter_Function('capcode', array('needs_environment' => false)),
'hasPermission' => new Twig_Filter_Function('twig_hasPermission_filter', array('needs_environment' => false)),
'date' => new Twig_Filter_Function('twig_date_filter', array('needs_environment' => false)),
'poster_id' => new Twig_Filter_Function('poster_id', array('needs_environment' => false)),
'remove_whitespace' => new Twig_Filter_Function('twig_remove_whitespace_filter', array('needs_environment' => false))
);
}
public function getName() {
return 'tinyboard';
}
}
function twig_remove_whitespace_filter($data) {
return preg_replace('/[\t\r\n]/', '', $data);
}
function twig_date_filter($date, $format) {
return date($format, $date);
}
function twig_hasPermission_filter($mod, $permission, $board) {
return hasPermission($permission, $board, $mod);
}
function twig_extension_filter($value, $case_insensitive = true) {
return 'test';
$ext = substr($value, strrpos($value, '.') + 1);
if($case_insensitive)
$ext = strtolower($ext);
return $ext;
}
function twig_sprintf_filter( $value, $var) {
return sprintf($value, $var);
}
function twig_truncate_filter($value, $length = 30, $preserve = false, $separator = '&hellip;') {
if (strlen($value) > $length) {
if ($preserve) {
if (false !== ($breakpoint = strpos($value, ' ', $length))) {
$length = $breakpoint;
}
}
return substr($value, 0, $length) . $separator;
}
return $value;
}
2011-10-05 00:22:53 -04:00
$loader = new Twig_Loader_Filesystem($config['dir']['template']);
2010-11-02 06:57:33 -04:00
function Element($templateFile, array $options) {
2011-10-05 00:22:53 -04:00
global $config, $debug, $loader;
2011-03-17 01:52:43 -04:00
if(function_exists('create_pm_header') && ((isset($options['mod']) && $options['mod']) || isset($options['__mod']))) {
2011-03-17 01:52:43 -04:00
$options['pm'] = create_pm_header();
}
2011-05-21 01:22:10 -04:00
if(isset($options['body']) && $config['debug']) {
2011-05-25 01:22:29 -04:00
if(isset($debug['start'])) {
$debug['time'] = '~' . round((microtime(true) - $debug['start']) * 1000, 2) . 'ms';
unset($debug['start']);
}
2011-05-21 01:22:10 -04:00
$options['body'] .= '<h3>Debug</h3><pre style="white-space: pre-wrap;font-size: 10px;">' . print_r($debug, true) . '</pre><hr/>';
}
2011-10-05 00:22:53 -04:00
$twig = new Twig_Environment($loader, Array(
'autoescape' => false,
'cache' => 'templates/cache',
2011-10-05 00:22:53 -04:00
'debug' => ($config['debug'] ? true : false),
));
$twig->addExtension(new Tinyboard_Twig_Extension());
2011-10-05 00:22:53 -04:00
2010-11-02 06:57:33 -04:00
// Read the template file
2011-10-05 00:22:53 -04:00
if(@file_get_contents("{$config['dir']['template']}/${templateFile}")) {
return $twig->render($templateFile, $options);
2010-11-02 06:57:33 -04:00
} else {
2011-03-17 01:52:43 -04:00
throw new Exception("Template file '${templateFile}' does not exist or is empty in '{$config['dir']['template']}'!");
2010-11-02 06:57:33 -04:00
}
}
?>