This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/_sakura/components/Templates.php

102 lines
2.8 KiB
PHP
Raw Normal View History

2015-04-06 21:57:17 +00:00
<?php
/*
* Template Engine Wrapper
*/
namespace Sakura;
class Templates {
// Engine container, template folder name and options
public static $_ENG;
public static $_TPL;
public static $_CFG;
2015-04-06 22:19:04 +00:00
2015-04-06 22:01:32 +00:00
// Initialise templating engine and data
2015-04-06 21:57:17 +00:00
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
2015-04-18 18:26:52 +00:00
$confPath = ROOT .'_sakura/templates/'. self::$_TPL .'/template.cfg';
2015-04-06 21:57:17 +00:00
// 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);
2015-04-06 21:57:17 +00:00
// Start Twig
self::twigLoader();
}
// Twig Loader
private static function twigLoader() {
// Initialise Twig Filesystem Loader
2015-04-18 18:26:52 +00:00
$twigLoader = new \Twig_Loader_Filesystem(ROOT .'_sakura/templates/'. self::$_TPL);
2015-04-06 21:57:17 +00:00
// 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
2015-04-06 22:01:32 +00:00
public static function parseCfg($data) {
2015-04-06 21:57:17 +00:00
// Create storage variable
2015-04-06 22:01:32 +00:00
$out = array();
2015-04-06 21:57:17 +00:00
// Remove comments and empty lines
2015-04-06 22:01:32 +00:00
$data = preg_replace('/#.*?\r\n/im', null, $data);
$data = preg_replace('/^\r\n/im', null, $data);
2015-04-06 21:57:17 +00:00
// Break line breaks up into array values
2015-04-06 22:01:32 +00:00
$data = str_replace("\r\n", "\n", $data);
$data = explode("\n", $data);
2015-04-06 21:57:17 +00:00
2015-04-06 22:01:32 +00:00
foreach($data as $var) {
2015-04-06 21:57:17 +00:00
// Make sure no whitespaces escaped the check
if(empty($var))
continue;
// Remove whitespace between key, equals sign and value
2015-04-06 22:01:32 +00:00
$var = preg_replace('/[\s+]=[\s+]/i', '=', $var);
2015-04-06 21:57:17 +00:00
// Then break this up
2015-04-06 22:01:32 +00:00
$var = explode('=', $var);
2015-04-06 21:57:17 +00:00
// And assign the value with the key to the output variable
2015-04-06 22:01:32 +00:00
$out[$var[0]] = $var[1];
2015-04-06 21:57:17 +00:00
2015-04-06 22:01:32 +00:00
}
2015-04-06 21:57:17 +00:00
// Return the output variable
2015-04-06 22:01:32 +00:00
return $out;
2015-04-06 21:57:17 +00:00
2015-04-06 22:01:32 +00:00
}
2015-04-06 21:57:17 +00:00
// Render template
public static function render($file, $tags) {
return self::$_ENG->render($file, $tags);
}
}