From 33881cacefc86981fb5c24a1c61a4b77d1f31dbe Mon Sep 17 00:00:00 2001 From: Akulov Andrey Date: Tue, 24 Sep 2013 18:46:00 +0400 Subject: [PATCH 1/6] Added template-mthaml feature. --- src/Epi.php | 1 + src/EpiException.php | 1 + src/EpiTemplate.php | 18 +++++++++++++---- src/EpiTemplate_MtHaml.php | 40 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100755 src/EpiTemplate_MtHaml.php diff --git a/src/Epi.php b/src/Epi.php index f0cc91f..2010781 100644 --- a/src/Epi.php +++ b/src/Epi.php @@ -26,6 +26,7 @@ class Epi 'session-apc' => array('base', 'EpiSession.php', 'EpiSession_Apc.php'), 'session-memcached' => array('base', 'EpiSession.php', 'EpiSession_Memcached.php'), 'template' => array('base', 'EpiTemplate.php'), + 'template-mthaml' => array('base', 'EpiTemplate.php', 'EpiTemplate_MtHaml.php') ); private static $included = array(); diff --git a/src/EpiException.php b/src/EpiException.php index bf7095f..58c2f02 100644 --- a/src/EpiException.php +++ b/src/EpiException.php @@ -29,3 +29,4 @@ class EpiDatabaseException extends EpiException{} class EpiDatabaseConnectionException extends EpiDatabaseException{} class EpiDatabaseQueryException extends EpiDatabaseException{} class EpiSessionException extends EpiException{} +class EpiTemplateException extends EpiException{} diff --git a/src/EpiTemplate.php b/src/EpiTemplate.php index 67e127d..4b3d78e 100644 --- a/src/EpiTemplate.php +++ b/src/EpiTemplate.php @@ -1,5 +1,5 @@ haml = new MtHaml\Environment('php'); + } + + public function display($template = null, $vars = null) + { + $template = $this->getCompiledFile($template); + return parent::display($template, $vars); + } + + public function get($template = null, $vars = null) + { + $template = $this->getCompiledFile($template); + return parent::display($template, $vars); + } + + private function getCompiledFile($template) { + $template = Epi::getPath('view').'/'.$template; + $complied = $template.'.php'; + $hamlCode = file_get_contents($template); + + // no need to compile if already compiled and up to date + if (!file_exists($complied) || filemtime($complied) != filemtime($template)) { + + $phpCode = $this->haml->compileString($hamlCode, $template); + + $tempnam = tempnam(dirname($template), basename($template)); + file_put_contents($tempnam, $phpCode); + rename($tempnam, $complied); + touch($complied, filemtime($template)); + } + + return $complied; + } +} \ No newline at end of file From a6697cc599e1cb6e703c24d50bede73fb1fde18b Mon Sep 17 00:00:00 2001 From: Akulov Andrey Date: Tue, 24 Sep 2013 19:05:14 +0400 Subject: [PATCH 2/6] Add template-mthaml example. --- .gitmodules | 3 ++ examples/site-with-haml-templates/.htaccess | 4 ++ examples/site-with-haml-templates/index.php | 37 +++++++++++++++++++ examples/site-with-haml-templates/mthaml | 1 + .../sample-template.haml | 4 ++ .../sample-template.haml.php | 6 +++ 6 files changed, 55 insertions(+) create mode 100644 .gitmodules create mode 100755 examples/site-with-haml-templates/.htaccess create mode 100755 examples/site-with-haml-templates/index.php create mode 160000 examples/site-with-haml-templates/mthaml create mode 100755 examples/site-with-haml-templates/sample-template.haml create mode 100755 examples/site-with-haml-templates/sample-template.haml.php diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b137995 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "examples/site-with-haml-templates/mthaml"] + path = examples/site-with-haml-templates/mthaml + url = https://github.com/arnaud-lb/MtHaml diff --git a/examples/site-with-haml-templates/.htaccess b/examples/site-with-haml-templates/.htaccess new file mode 100755 index 0000000..bab0219 --- /dev/null +++ b/examples/site-with-haml-templates/.htaccess @@ -0,0 +1,4 @@ +RewriteEngine on +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^(.*)\?*$ index.php?__route__=/$1 [L,QSA] diff --git a/examples/site-with-haml-templates/index.php b/examples/site-with-haml-templates/index.php new file mode 100755 index 0000000..c0de50c --- /dev/null +++ b/examples/site-with-haml-templates/index.php @@ -0,0 +1,37 @@ +get('/', array('MyClass', 'MyMethod')); +getRoute()->run(); + +/* + * ****************************************************************************************** + * Define functions and classes which are executed by EpiCode based on the $_['routes'] array + * ****************************************************************************************** + */ +class MyClass +{ + static public function MyMethod() + { + $params = array(); + $params['heading'] = 'Hello friends!'; + $params['friends'] = array('John', 'Mike', 'Luke', 'Jack', 'Spike'); + + getTemplate()->display('sample-template.haml', $params); + } +} diff --git a/examples/site-with-haml-templates/mthaml b/examples/site-with-haml-templates/mthaml new file mode 160000 index 0000000..ccc214b --- /dev/null +++ b/examples/site-with-haml-templates/mthaml @@ -0,0 +1 @@ +Subproject commit ccc214b053c1f74aa6250f7cdb5b2609003c59b0 diff --git a/examples/site-with-haml-templates/sample-template.haml b/examples/site-with-haml-templates/sample-template.haml new file mode 100755 index 0000000..881ba72 --- /dev/null +++ b/examples/site-with-haml-templates/sample-template.haml @@ -0,0 +1,4 @@ +%h1 = $heading +%ul + - foreach($friends as $name) + %li Hello #{$name}! \ No newline at end of file diff --git a/examples/site-with-haml-templates/sample-template.haml.php b/examples/site-with-haml-templates/sample-template.haml.php new file mode 100755 index 0000000..57b4567 --- /dev/null +++ b/examples/site-with-haml-templates/sample-template.haml.php @@ -0,0 +1,6 @@ +

+ From 77accc83ea62b37d3980e8366e1aa30e203b66df Mon Sep 17 00:00:00 2001 From: Akulov Andrey Date: Tue, 24 Sep 2013 19:13:02 +0400 Subject: [PATCH 3/6] Remove compiled template from template-tmhaml example. --- .gitignore | 1 + examples/site-with-haml-templates/sample-template.haml.php | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) create mode 100644 .gitignore delete mode 100755 examples/site-with-haml-templates/sample-template.haml.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf088f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +examples/site-with-haml-templates/sample-template.haml.php diff --git a/examples/site-with-haml-templates/sample-template.haml.php b/examples/site-with-haml-templates/sample-template.haml.php deleted file mode 100755 index 57b4567..0000000 --- a/examples/site-with-haml-templates/sample-template.haml.php +++ /dev/null @@ -1,6 +0,0 @@ -

-
    - -
  • Hello !
  • - -
From a33a57d8ae72b3a1ee4f15c13d6b0fbe9f979f1c Mon Sep 17 00:00:00 2001 From: Andrey Akulov Date: Wed, 10 Dec 2014 11:02:09 +0000 Subject: [PATCH 4/6] Make EpiTemplate work with employ. Add EpiTemplate_Extendable class. --- src/EpiSession.php | 0 src/EpiTemplate.php | 52 ++++++++++++++++++++++++++++------ src/EpiTemplate_Extendable.php | 21 ++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) mode change 100644 => 100755 src/EpiSession.php mode change 100644 => 100755 src/EpiTemplate.php create mode 100755 src/EpiTemplate_Extendable.php diff --git a/src/EpiSession.php b/src/EpiSession.php old mode 100644 new mode 100755 diff --git a/src/EpiTemplate.php b/src/EpiTemplate.php old mode 100644 new mode 100755 index 4b3d78e..5df18bb --- a/src/EpiTemplate.php +++ b/src/EpiTemplate.php @@ -1,6 +1,10 @@ hash = $hash; + return self::$instances[$hash]; + } + + /* + * @param $const + * @params optional + */ + public static function employ(/*$const*/) + { + if(func_num_args() === 1) + self::$employ = func_get_arg(0); + elseif(func_num_args() > 1) + self::$employ = func_get_args(); + + return self::$employ; + } } interface EpiTemplateInterface @@ -110,14 +147,13 @@ public function jsonResponse($data); function getTemplate() { - static $template; - if($template) - return $template; - - if (class_exists('EpiTemplate_MtHaml') && class_exists('MtHaml\Environment')) { - $template = new EpiTemplate_MtHaml(); + $employ = EpiTemplate::employ(); + $class = array_shift($employ); + if(class_exists($class)) { + return EpiTemplate::getInstance($class, $employ); + } else if(class_exists(EpiTemplate::PHP_EXT)) { + return EpiTemplate::getInstance(EpiTemplate::PHP_EXT); } else { - $template = new EpiTemplate(); + return new EpiTemplate(); } - return $template; } \ No newline at end of file diff --git a/src/EpiTemplate_Extendable.php b/src/EpiTemplate_Extendable.php new file mode 100755 index 0000000..c6ffacc --- /dev/null +++ b/src/EpiTemplate_Extendable.php @@ -0,0 +1,21 @@ +get($template, $vars); + + if (file_exists(Epi::getPath('view').'/'.$_template) || file_exists($_template)) { + $vars['_content'] = $_content; + return parent::display($_template, $vars); + } else { + echo $_content; + } + } +} \ No newline at end of file From b90d174a1963741d3ea7287586305cbc18c12e44 Mon Sep 17 00:00:00 2001 From: Andrey Akulov Date: Wed, 10 Dec 2014 13:43:07 +0000 Subject: [PATCH 5/6] Bugfix. Fails on false _template value. --- src/EpiTemplate_Extendable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EpiTemplate_Extendable.php b/src/EpiTemplate_Extendable.php index c6ffacc..a67e9b8 100755 --- a/src/EpiTemplate_Extendable.php +++ b/src/EpiTemplate_Extendable.php @@ -11,7 +11,7 @@ public function display($template = null, $vars = null) $_content = $this->get($template, $vars); - if (file_exists(Epi::getPath('view').'/'.$_template) || file_exists($_template)) { + if (is_file(Epi::getPath('view').'/'.$_template) || is_file($_template)) { $vars['_content'] = $_content; return parent::display($_template, $vars); } else { From 2f56f741638519637d1064100cafb8e9e06940ec Mon Sep 17 00:00:00 2001 From: Andrey Akulov Date: Fri, 12 Dec 2014 10:19:18 +0000 Subject: [PATCH 6/6] Add template_default_vars feature. --- src/EpiTemplate_Extendable.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/EpiTemplate_Extendable.php b/src/EpiTemplate_Extendable.php index a67e9b8..a02ea7d 100755 --- a/src/EpiTemplate_Extendable.php +++ b/src/EpiTemplate_Extendable.php @@ -3,7 +3,11 @@ class EpiTemplate_Extendable extends EpiTemplate implements EpiTemplateInterface { public function display($template = null, $vars = null) { - $_template = Epi::getSetting('default_layout'); + $def_vars = Epi::getSetting('template_default_vars'); + + if (is_array($def_vars)) { + $vars = array_merge($def_vars, $vars); + } if (array_key_exists('_template', $vars)) { $_template = $vars['_template'];