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/libraries/Template.php

151 lines
3.6 KiB
PHP
Raw Normal View History

2015-10-30 16:15:58 +00:00
<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the templating engine class.
2016-03-08 23:07:58 +00:00
*
2016-02-03 22:22:56 +00:00
* @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-03-31 20:03:25 +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.
2016-03-08 23:07:58 +00:00
*
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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @var array
*/
2016-02-04 20:56:40 +00:00
private static $vars = [];
2016-02-02 21:04:15 +00:00
/**
* The templating engine.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @var Twig_Environment
*/
2016-04-01 15:31:05 +00:00
private static $engine;
2016-02-02 21:04:15 +00:00
/**
* The template name.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @var string
*/
2016-04-01 15:31:05 +00:00
public static $name;
/**
* The path to the client side resources
*
* @var string
*/
public static $resources;
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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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
// Set variables
2016-04-01 15:31:05 +00:00
self::$name = $name;
// Set reources path
self::$resources = Config::get('content_path') . '/data/' . self::$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-04-01 15:31:05 +00:00
$twigLoader = new Twig_Loader_Filesystem(ROOT . 'templates/' . self::$name);
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-04-01 15:31:05 +00:00
self::$engine = new Twig_Environment($twigLoader, $twigEnv);
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
// Add route function
2016-04-01 15:31:05 +00:00
self::$engine->addFunction(new Twig_SimpleFunction('route', function ($name, $args = null) {
2016-02-27 16:46:16 +00:00
return Router::route($name, $args);
}));
2016-03-27 22:15:51 +00:00
2016-03-31 20:03:25 +00:00
// Add config function
2016-04-02 15:59:45 +00:00
self::$engine->addFunction(new Twig_SimpleFunction('config', function ($name, $local = false) {
if ($local) {
$name = explode('.', $name);
return Config::local($name[0], $name[1]);
}
2016-03-31 20:03:25 +00:00
return Config::get($name);
}));
2016-04-01 15:31:05 +00:00
// Add resource function
self::$engine->addFunction(new Twig_SimpleFunction('resource', function ($path = "") {
return self::$resources . "/{$path}";
}));
2016-03-31 20:03:25 +00:00
// Method of getting the currently active session id
2016-04-01 15:31:05 +00:00
self::$engine->addFunction(new Twig_SimpleFunction('session_id', 'session_id'));
2016-03-31 20:03:25 +00:00
// json_decode filter (why doesn't this exist to begin with?)
2016-04-01 15:31:05 +00:00
self::$engine->addFilter(new Twig_SimpleFilter('json_decode', 'json_decode'));
2016-04-03 21:29:46 +00:00
// byte_symbol filter
self::$engine->addFilter(new Twig_SimpleFilter('byte_symbol', 'byte_symbol'));
2015-10-31 18:14:54 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Merge the parse variables.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @param string $file The filename/path
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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-04-01 15:31:05 +00:00
return self::$engine->render($file . self::FILE_EXT, self::$vars);
2015-10-31 18:14:54 +00:00
} catch (\Exception $e) {
return trigger_error($e->getMessage(), E_USER_ERROR);
2015-10-31 18:14:54 +00:00
}
}
2015-10-30 16:15:58 +00:00
}