2015-10-30 16:15:58 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the templating engine class.
|
|
|
|
*
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-10-30 16:15:58 +00:00
|
|
|
namespace Sakura;
|
|
|
|
|
2015-10-31 18:14:54 +00:00
|
|
|
use Twig_Environment;
|
|
|
|
use Twig_Extension_StringLoader;
|
|
|
|
use Twig_Loader_Filesystem;
|
|
|
|
|
2015-10-30 16:15:58 +00:00
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Sakura wrapper for Twig.
|
|
|
|
*
|
2015-10-30 16:15:58 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-10-30 16:15:58 +00:00
|
|
|
*/
|
|
|
|
class Template
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The variables passed on to the templating engine.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
private static $vars = [];
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The templating engine.
|
|
|
|
*
|
|
|
|
* @var Twig_Environment
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
private static $template;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The template name.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
private static $templateName;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The template options.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
private static $templateOptions;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The file extension used by template files
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
const FILE_EXT = '.twig';
|
2015-10-31 18:14:54 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Set the template name.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the template directory.
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
public static function set($name)
|
2015-11-01 13:26:05 +00:00
|
|
|
{
|
2015-10-31 18:14:54 +00:00
|
|
|
// Assign config path to a variable so we don't have to type it out twice
|
2015-12-03 19:40:01 +00:00
|
|
|
$confPath = ROOT . 'templates/' . $name . '/template.ini';
|
2015-10-31 18:14:54 +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
|
2016-02-04 20:56:40 +00:00
|
|
|
self::$templateOptions = parse_ini_file($confPath, true);
|
2015-10-31 18:14:54 +00:00
|
|
|
|
|
|
|
// Set variables
|
2016-02-04 20:56:40 +00:00
|
|
|
self::$templateName = $name;
|
2015-10-31 18:14:54 +00:00
|
|
|
|
|
|
|
// Reinitialise
|
2016-02-04 20:56:40 +00:00
|
|
|
self::init();
|
2015-10-31 18:14:54 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Initialise the templating engine.
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
public static function init()
|
2015-11-01 13:26:05 +00:00
|
|
|
{
|
2015-10-31 18:14:54 +00:00
|
|
|
// Initialise Twig Filesystem Loader
|
2016-02-04 20:56:40 +00:00
|
|
|
$twigLoader = new Twig_Loader_Filesystem(ROOT . 'templates/' . self::$templateName);
|
2015-10-31 18:14:54 +00:00
|
|
|
|
|
|
|
// Environment variable
|
|
|
|
$twigEnv = [];
|
|
|
|
|
|
|
|
// Enable caching
|
2015-12-04 14:19:10 +00:00
|
|
|
if (Config::get('enable_tpl_cache')) {
|
2015-12-01 13:34:16 +00:00
|
|
|
$twigEnv['cache'] = ROOT . 'cache/twig';
|
2015-10-31 18:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// And now actually initialise the templating engine
|
2016-02-04 20:56:40 +00:00
|
|
|
self::$template = new Twig_Environment($twigLoader, $twigEnv);
|
2015-10-31 18:14:54 +00:00
|
|
|
|
|
|
|
// Load String template loader
|
2016-02-04 20:56:40 +00:00
|
|
|
self::$template->addExtension(new Twig_Extension_StringLoader());
|
2015-10-31 18:14:54 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Merge the parse variables.
|
|
|
|
*
|
|
|
|
* @param array $vars The new variables.
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
public static function vars($vars)
|
2015-11-01 13:26:05 +00:00
|
|
|
{
|
2016-02-04 20:56:40 +00:00
|
|
|
self::$vars = array_merge(self::$vars, $vars);
|
2015-10-31 18:14:54 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Render a template file.
|
|
|
|
*
|
|
|
|
* @param string $file The filename/path
|
|
|
|
*
|
|
|
|
* @return bool|string An error or the HTML.
|
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
public static function render($file)
|
2015-10-31 18:14:54 +00:00
|
|
|
{
|
|
|
|
try {
|
2016-02-04 20:56:40 +00:00
|
|
|
return self::$template->render($file . self::FILE_EXT, self::$vars);
|
2015-10-31 18:14:54 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-12-10 20:55:51 +00:00
|
|
|
return trigger_error($e->getMessage(), E_USER_ERROR);
|
2015-10-31 18:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-30 16:15:58 +00:00
|
|
|
}
|