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;
|
2016-07-31 19:36:13 +00:00
|
|
|
use Twig_SimpleFilter;
|
2016-02-27 16:46:16 +00:00
|
|
|
use Twig_SimpleFunction;
|
2015-10-31 18:14:54 +00:00
|
|
|
|
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
|
|
|
/**
|
2016-08-07 14:23:02 +00:00
|
|
|
* The file extension used by template files.
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-07 14:23:02 +00:00
|
|
|
const FILE_EXT = '.twig';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-10-07 17:37:00 +00:00
|
|
|
* The path relative to the root.
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-07 14:23:02 +00:00
|
|
|
const VIEWS_DIR = 'resources/views/';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The template name.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-04-01 15:31:05 +00:00
|
|
|
public static $name;
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
2016-08-07 14:23:02 +00:00
|
|
|
* The templating engine.
|
|
|
|
* @var Twig_Environment
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-08-07 14:23:02 +00:00
|
|
|
private static $engine;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The variables passed on to the templating engine.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $vars = [];
|
2015-10-31 18:14:54 +00:00
|
|
|
|
2016-07-30 13:48:09 +00:00
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* List of utility functions to add to templating.
|
2016-07-30 13:48:09 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-07-31 19:36:13 +00:00
|
|
|
protected static $utilityFunctions = [
|
2016-07-30 13:48:09 +00:00
|
|
|
'route',
|
|
|
|
'config',
|
|
|
|
'session_id',
|
2016-07-31 19:36:13 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2016-08-05 02:35:37 +00:00
|
|
|
* List of utility filters to add to templating.
|
2016-07-31 19:36:13 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $utilityFilters = [
|
2016-07-30 13:48:09 +00:00
|
|
|
'json_decode',
|
|
|
|
'byte_symbol',
|
|
|
|
];
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Set the template name.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param string $name
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
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
|
|
|
// Set variables
|
2016-04-01 15:31:05 +00:00
|
|
|
self::$name = $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
|
|
|
{
|
2016-10-07 17:37:00 +00:00
|
|
|
$views_dir = path(self::VIEWS_DIR);
|
2016-07-30 13:48:09 +00:00
|
|
|
|
2015-10-31 18:14:54 +00:00
|
|
|
// Initialise Twig Filesystem Loader
|
2016-08-03 23:40:47 +00:00
|
|
|
$loader = new Twig_Loader_Filesystem();
|
|
|
|
|
|
|
|
foreach (glob("{$views_dir}*") as $dir) {
|
|
|
|
$key = basename($dir);
|
|
|
|
|
|
|
|
if ($key === self::$name) {
|
2016-08-06 14:09:01 +00:00
|
|
|
$loader->addPath($dir, '__main__');
|
2016-08-03 23:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$loader->addPath($dir, $key);
|
|
|
|
}
|
2015-10-31 18:14:54 +00:00
|
|
|
|
|
|
|
// Environment variable
|
2016-08-03 23:40:47 +00:00
|
|
|
$env = [
|
2016-07-30 13:48:09 +00:00
|
|
|
'cache' => config("performance.template_cache")
|
2016-10-07 17:37:00 +00:00
|
|
|
? path(config("performance.cache_dir") . 'views')
|
2016-07-30 13:48:09 +00:00
|
|
|
: false,
|
|
|
|
'auto_reload' => true,
|
|
|
|
'debug' => config("dev.twig_debug"),
|
2016-11-04 16:45:53 +00:00
|
|
|
'strict_variables' => true,
|
2016-07-30 13:48:09 +00:00
|
|
|
];
|
2015-10-31 18:14:54 +00:00
|
|
|
|
|
|
|
// And now actually initialise the templating engine
|
2016-08-03 23:40:47 +00:00
|
|
|
self::$engine = new Twig_Environment($loader, $env);
|
2015-10-31 18:14:54 +00:00
|
|
|
|
|
|
|
// Load String template loader
|
2016-04-01 15:31:05 +00:00
|
|
|
self::$engine->addExtension(new Twig_Extension_StringLoader());
|
2016-02-27 16:46:16 +00:00
|
|
|
|
2016-07-30 13:48:09 +00:00
|
|
|
// Add utility functions
|
2016-07-31 19:36:13 +00:00
|
|
|
foreach (self::$utilityFunctions as $function) {
|
|
|
|
self::$engine->addFunction(new Twig_SimpleFunction($function, $function));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add utility filters
|
|
|
|
foreach (self::$utilityFilters as $filter) {
|
|
|
|
self::$engine->addFilter(new Twig_SimpleFilter($filter, $filter));
|
2016-07-30 13:48:09 +00:00
|
|
|
}
|
2015-10-31 18:14:54 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 19:13:55 +00:00
|
|
|
/**
|
|
|
|
* Checks if twig is available.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function available()
|
|
|
|
{
|
|
|
|
return self::$engine !== null && self::$name !== null;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Merge the parse variables.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param array $vars
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
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.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param string $file
|
2016-08-07 14:23:02 +00:00
|
|
|
* @return string
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-02-04 20:56:40 +00:00
|
|
|
public static function render($file)
|
2015-10-31 18:14:54 +00:00
|
|
|
{
|
2016-07-29 19:31:36 +00:00
|
|
|
return self::$engine->render($file . self::FILE_EXT, self::$vars);
|
2015-10-31 18:14:54 +00:00
|
|
|
}
|
2016-08-07 14:23:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a template directory exists.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function exists($name)
|
|
|
|
{
|
2016-10-07 17:37:00 +00:00
|
|
|
return ctype_alnum($name) && file_exists(path(self::VIEWS_DIR . $name . "/"));
|
2016-08-07 14:23:02 +00:00
|
|
|
}
|
2015-10-30 16:15:58 +00:00
|
|
|
}
|