Template wrapper

This commit is contained in:
flash 2015-04-06 21:57:17 +00:00
parent 710cd981aa
commit 107a41711c
9 changed files with 119 additions and 86 deletions

View file

@ -86,35 +86,4 @@ class Configuration {
} }
// Parse .cfg files, mainly/only used for templates
public static function parseCfg($data) {
// Create storage variable
$out = array();
// Remove comments and empty lines
$data = preg_replace('/#.*?\r\n/im', null, $data);
$data = preg_replace('/^\r\n/im', null, $data);
// Break line breaks up into array values
$data = explode("\r\n", $data);
foreach($data as $var) {
// Remove whitespace between key, equals sign and value
$var = preg_replace('/[\s+]=[\s+]/i', '=', $var);
// Then break this up
$var = explode('=', $var);
// And assign the value with the key to the output variable
$out[$var[0]] = $var[1];
}
// Return the output variable
return $out;
}
} }

View file

@ -9,6 +9,7 @@ class Main {
public static $_TPL; public static $_TPL;
public static $_MD; public static $_MD;
public static $_IN_MANAGE = false;
// Constructor // Constructor
public static function init($config) { public static function init($config) {
@ -30,38 +31,13 @@ class Main {
Session::init(); Session::init();
// Templating engine // Templating engine
self::initTpl(); Templates::init(Configuration::getLocalConfig('etc', 'design'));
// Markdown Parser // Markdown Parser
self::initMD(); self::initMD();
} }
// Initialise Twig
private static function initTpl() {
// Initialise Twig Filesystem Loader
$twigLoader = new \Twig_Loader_Filesystem(Configuration::getLocalConfig('etc', 'templatesPath') .'/'. Configuration::getLocalConfig('etc', 'design'));
// And now actually initialise the templating engine
self::$_TPL = new \Twig_Environment($twigLoader, array(
// 'cache' => SATOKO_ROOT_DIRECTORY. self::getConfig('path', 'cache') // Set cache directory
));
// Load String template loader
self::$_TPL->addExtension(new \Twig_Extension_StringLoader());
}
// Render template
public static function tplRender($file, $tags) {
return self::$_TPL->render($file, $tags);
}
// Initialise Parsedown // Initialise Parsedown
private static function initMD() { private static function initMD() {

View file

@ -1,14 +1,102 @@
<?php <?php
/* /*
* Template Engine Wrapper * Template Engine Wrapper
*/ */
namespace Sakura; namespace Sakura;
class Templates { class Templates {
// Engine container and template options // Engine container, template folder name and options
public static $_ENG; public static $_ENG;
public static $_CFG; public static $_TPL;
public static $_CFG;
}
// Initialise templating engine and data
public static function init($template) {
// Set template folder name
self::$_TPL = $template;
// Assign config path to a variable so we don't have to type it out twice
$confPath = Configuration::getLocalConfig('etc', 'templatesPath') .'/'. self::$_TPL .'/template.cfg';
// Check if the configuration file exists
if(!file_exists($confPath))
trigger_error('Template configuration does not exist', E_USER_ERROR);
// Parse and store the configuration
self::$_CFG = self::parseCfg(file_get_contents($confPath));
// Make sure we're not using a manage template for the main site or the other way around
//if((self::$_CFG['MANAGE'] && !Main::$_IN_MANAGE) || (!self::$_CFG['MANAGE'] && Main::$_IN_MANAGE))
// trigger_error('Incorrect template type', E_USER_ERROR);
// Need to unfuck this later
// Start Twig
self::twigLoader();
}
// Twig Loader
private static function twigLoader() {
// Initialise Twig Filesystem Loader
$twigLoader = new \Twig_Loader_Filesystem(Configuration::getLocalConfig('etc', 'templatesPath') .'/'. Configuration::getLocalConfig('etc', 'design'));
// And now actually initialise the templating engine
self::$_ENG = new \Twig_Environment($twigLoader, array(
// 'cache' => SATOKO_ROOT_DIRECTORY. self::getConfig('path', 'cache') // Set cache directory
));
// Load String template loader
self::$_ENG->addExtension(new \Twig_Extension_StringLoader());
}
// Parse .cfg files
public static function parseCfg($data) {
// Create storage variable
$out = array();
// Remove comments and empty lines
$data = preg_replace('/#.*?\r\n/im', null, $data);
$data = preg_replace('/^\r\n/im', null, $data);
// Break line breaks up into array values
$data = str_replace("\r\n", "\n", $data);
$data = explode("\n", $data);
foreach($data as $var) {
// Make sure no whitespaces escaped the check
if(empty($var))
continue;
// Remove whitespace between key, equals sign and value
$var = preg_replace('/[\s+]=[\s+]/i', '=', $var);
// Then break this up
$var = explode('=', $var);
// And assign the value with the key to the output variable
$out[$var[0]] = $var[1];
}
// Return the output variable
return $out;
}
// Render template
public static function render($file, $tags) {
return self::$_ENG->render($file, $tags);
}
}

View file

@ -1,10 +1,10 @@
# #
# Yuuno # Yuuno
# 2015 Flashii Design # 2015 Flashii Design
# #
# Sets name of this template # Sets name of this template
NAME = Yuuno NAME = Yuuno
# Is this style intended for manage? # Is this style intended for manage?
MANAGE = false MANAGE = false

View file

@ -30,4 +30,4 @@ $renderData['auth'] = [
]; ];
// Print page contents // Print page contents
print Main::tplRender('main/authenticate.tpl', $renderData); print Templates::render('main/authenticate.tpl', $renderData);

View file

@ -26,4 +26,4 @@ $renderData['thirdParty'] = [
]; ];
// Print page contents // Print page contents
print Main::tplRender('main/credits.tpl', $renderData); print Templates::render('main/credits.tpl', $renderData);

View file

@ -23,4 +23,4 @@ $renderData['stats'] = [
]; ];
// Print page contents // Print page contents
print Main::tplRender('main/index.tpl', $renderData); print Templates::render('main/index.tpl', $renderData);

View file

@ -27,4 +27,4 @@ if($ipData = Main::loadInfoPage(isset($_GET['r']) ? strtolower($_GET['r']) : '')
} }
// Print page contents // Print page contents
print Main::$_TPL->render('main/infopage.tpl', $renderData); print Templates::render('main/infopage.tpl', $renderData);

View file

@ -55,4 +55,4 @@ if(isset($_GET['xml'])) {
} }
// Print page contents // Print page contents
print Main::tplRender('main/news.tpl', $renderData); print Templates::render('main/news.tpl', $renderData);