2015-04-06 21:57:17 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Template Engine Wrapper
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
2015-06-04 12:41:55 +00:00
|
|
|
use Twig_Environment;
|
|
|
|
use Twig_Extension_StringLoader;
|
2015-09-14 20:51:23 +00:00
|
|
|
use Twig_Loader_Filesystem;
|
2015-06-04 12:41:55 +00:00
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
|
|
|
* Class Templates
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
class Templates
|
|
|
|
{
|
2015-04-06 21:57:17 +00:00
|
|
|
// Engine container, template folder name and options
|
2015-09-14 20:51:23 +00:00
|
|
|
public static $engine;
|
|
|
|
public static $template;
|
|
|
|
public static $configuration;
|
2015-04-06 22:19:04 +00:00
|
|
|
|
2015-04-06 22:01:32 +00:00
|
|
|
// Initialise templating engine and data
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function init($template)
|
|
|
|
{
|
2015-04-06 21:57:17 +00:00
|
|
|
|
|
|
|
// Set template folder name
|
2015-09-14 20:51:23 +00:00
|
|
|
self::$template = $template;
|
2015-04-06 21:57:17 +00:00
|
|
|
|
|
|
|
// Assign config path to a variable so we don't have to type it out twice
|
2015-09-14 20:51:23 +00:00
|
|
|
$confPath = ROOT . '_sakura/templates/' . self::$template . '/template.ini';
|
2015-04-06 21:57:17 +00:00
|
|
|
|
|
|
|
// Check if the configuration file exists
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!file_exists($confPath)) {
|
2015-04-06 21:57:17 +00:00
|
|
|
trigger_error('Template configuration does not exist', E_USER_ERROR);
|
2015-08-23 22:08:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:57:17 +00:00
|
|
|
// Parse and store the configuration
|
2015-09-14 20:51:23 +00:00
|
|
|
self::$configuration = parse_ini_file($confPath, true);
|
2015-04-06 21:57:17 +00:00
|
|
|
|
|
|
|
// Make sure we're not using a manage template for the main site or the other way around
|
2015-09-14 20:51:23 +00:00
|
|
|
if (defined('SAKURA_MANAGE') && (bool) self::$configuration['manage']['mode'] != (bool) SAKURA_MANAGE) {
|
2015-04-12 13:33:59 +00:00
|
|
|
trigger_error('Incorrect template type', E_USER_ERROR);
|
2015-08-23 22:08:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:57:17 +00:00
|
|
|
// Start Twig
|
|
|
|
self::twigLoader();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Twig Loader
|
2015-09-14 20:51:23 +00:00
|
|
|
private static function twigLoader()
|
|
|
|
{
|
2015-04-06 21:57:17 +00:00
|
|
|
|
|
|
|
// Initialise Twig Filesystem Loader
|
2015-09-14 20:51:23 +00:00
|
|
|
$twigLoader = new Twig_Loader_Filesystem(ROOT . '_sakura/templates/' . self::$template);
|
2015-04-06 21:57:17 +00:00
|
|
|
|
2015-06-04 12:41:55 +00:00
|
|
|
// Environment variable
|
|
|
|
$twigEnv = [];
|
2015-04-06 21:57:17 +00:00
|
|
|
|
2015-06-04 12:41:55 +00:00
|
|
|
// Enable caching
|
2015-09-14 20:51:23 +00:00
|
|
|
if (Configuration::getConfig('enable_tpl_cache')) {
|
|
|
|
$twigEnv['cache'] = ROOT . 'cache';
|
2015-08-29 13:25:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 12:41:55 +00:00
|
|
|
// And now actually initialise the templating engine
|
2015-09-14 20:51:23 +00:00
|
|
|
self::$engine = new Twig_Environment($twigLoader, $twigEnv);
|
2015-04-06 21:57:17 +00:00
|
|
|
|
|
|
|
// Load String template loader
|
2015-09-14 20:51:23 +00:00
|
|
|
self::$engine->addExtension(new Twig_Extension_StringLoader());
|
2015-04-06 21:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render template
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function render($file, $tags)
|
|
|
|
{
|
2015-08-29 13:25:57 +00:00
|
|
|
try {
|
2015-09-14 20:51:23 +00:00
|
|
|
return self::$engine->render($file, $tags);
|
|
|
|
} catch (\Exception $e) {
|
2015-08-29 13:25:57 +00:00
|
|
|
trigger_error($e->getMessage(), E_USER_ERROR);
|
|
|
|
}
|
2015-04-06 21:57:17 +00:00
|
|
|
}
|
|
|
|
}
|