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/.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/src/Epi.php b/src/Epi.php index 3d79304..e45c62b 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/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 67e127d..5df18bb --- a/src/EpiTemplate.php +++ b/src/EpiTemplate.php @@ -1,6 +1,10 @@ hash = $hash; + return self::$instances[$hash]; + } - $template = new EpiTemplate(); - return $template; + /* + * @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 +{ + public function get($key = null); + public function display($template = null, $vars = null); + public function json($data); + public function jsonResponse($data); } +function getTemplate() +{ + $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 { + return new EpiTemplate(); + } +} \ No newline at end of file diff --git a/src/EpiTemplate_Extendable.php b/src/EpiTemplate_Extendable.php new file mode 100755 index 0000000..a02ea7d --- /dev/null +++ b/src/EpiTemplate_Extendable.php @@ -0,0 +1,25 @@ +get($template, $vars); + + if (is_file(Epi::getPath('view').'/'.$_template) || is_file($_template)) { + $vars['_content'] = $_content; + return parent::display($_template, $vars); + } else { + echo $_content; + } + } +} \ No newline at end of file diff --git a/src/EpiTemplate_MtHaml.php b/src/EpiTemplate_MtHaml.php new file mode 100755 index 0000000..fc6eaf5 --- /dev/null +++ b/src/EpiTemplate_MtHaml.php @@ -0,0 +1,40 @@ +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