From 0629bf68f7eee1fd88d9227124b93a984c1868b9 Mon Sep 17 00:00:00 2001 From: Savetheinternet Date: Wed, 5 Oct 2011 23:53:43 +1100 Subject: use template files instead of hardcoded HTML for generating psots --- inc/template.php | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'inc/template.php') diff --git a/inc/template.php b/inc/template.php index ed9e01f7..00cdbd36 100644 --- a/inc/template.php +++ b/inc/template.php @@ -6,8 +6,65 @@ } require 'contrib/Twig/Autoloader.php'; + Twig_Autoloader::register(); + 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 = '…') { + if (strlen($value) > $length) { + if ($preserve) { + if (false !== ($breakpoint = strpos($value, ' ', $length))) { + $length = $breakpoint; + } + } + return substr($value, 0, $length) . $separator; + } + return $value; + } + $loader = new Twig_Loader_Filesystem($config['dir']['template']); function Element($templateFile, array $options) { @@ -28,9 +85,10 @@ $twig = new Twig_Environment($loader, Array( 'autoescape' => false, - //'cache' => 'cache', + 'cache' => 'templates/cache', 'debug' => ($config['debug'] ? true : false), )); + $twig->addExtension(new Tinyboard_Twig_Extension()); // Read the template file if(@file_get_contents("{$config['dir']['template']}/${templateFile}")) { -- cgit v1.3