From 00c74a2fcdcb616ed2ff786b9e615de7d465cdd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 11 Mar 2017 13:37:16 -0300 Subject: [PATCH] Add Twig support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add basic support for Twig template engine Signed-off-by: MaurĂ­cio Meneghini Fauth --- .gitignore | 2 ++ composer.json | 3 ++- libraries/Template.php | 24 +++++++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 41bd940970..a07f964431 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ /tmp/ # For setup script /config/ +# Template cache +/templates/cache/ # ctags tags # Editor files diff --git a/composer.json b/composer.json index a3e6fb8ab5..4e61914b2f 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ "phpmyadmin/motranslator": "^3.0", "phpmyadmin/shapefile": "^2.0", "phpseclib/phpseclib": "^2.0", - "google/recaptcha": "^1.1" + "google/recaptcha": "^1.1", + "twig/twig": "^2.2" }, "conflict": { "tecnickcom/tcpdf": "<6.2" diff --git a/libraries/Template.php b/libraries/Template.php index 548ec9e1a1..102c859bcd 100644 --- a/libraries/Template.php +++ b/libraries/Template.php @@ -31,6 +31,16 @@ class Template */ protected $helperFunctions; + /** + * Twig loader + */ + protected $loader; + + /** + * Twig environment + */ + protected $environment; + const BASE_PATH = 'templates/'; /** @@ -45,6 +55,10 @@ class Template $this->name = $name; $this->data = $data; $this->helperFunctions = $helperFunctions; + $this->loader = new \Twig_Loader_Filesystem(static::BASE_PATH); + $this->environment = new \Twig_Environment($this->loader, array( + 'cache' => static::BASE_PATH . 'cache', + )); } /** @@ -139,7 +153,15 @@ class Template */ public function render($data = array(), $helperFunctions = array()) { - $template = static::BASE_PATH . $this->name . '.phtml'; + $template = static::BASE_PATH . $this->name; + + if (file_exists($template . '.twig')) { + $this->set($data); + return $this->environment->load($this->name . '.twig') + ->render($this->data); + } + + $template = $template . '.phtml'; try { $this->set($data); $this->helperFunctions = array_merge(