Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples/site-with-haml-templates/sample-template.haml.php
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions examples/site-with-haml-templates/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?__route__=/$1 [L,QSA]
37 changes: 37 additions & 0 deletions examples/site-with-haml-templates/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
chdir('..');
require_once '../src/Epi.php';
require_once 'mthaml/lib/MtHaml/Autoloader.php';
MtHaml\Autoloader::register();

Epi::setPath('base', '../src');
Epi::setPath('view', 'site-with-haml-templates');
Epi::init('route','template-mthaml');

/*
* This is a sample page whch uses EpiCode.
* There is a .htaccess file which uses mod_rewrite to redirect all requests to index.php while preserving GET parameters.
* The $_['routes'] array defines all uris which are handled by EpiCode.
* EpiCode traverses back along the path until it finds a matching page.
* i.e. If the uri is /foo/bar and only 'foo' is defined then it will execute that route's action.
* It is highly recommended to define a default route of '' for the home page or root of the site (yoursite.com/).
*/
getRoute()->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);
}
}
1 change: 1 addition & 0 deletions examples/site-with-haml-templates/mthaml
Submodule mthaml added at ccc214
4 changes: 4 additions & 0 deletions examples/site-with-haml-templates/sample-template.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%h1 = $heading
%ul
- foreach($friends as $name)
%li Hello #{$name}!
1 change: 1 addition & 0 deletions src/Epi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions src/EpiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ class EpiDatabaseException extends EpiException{}
class EpiDatabaseConnectionException extends EpiDatabaseException{}
class EpiDatabaseQueryException extends EpiDatabaseException{}
class EpiSessionException extends EpiException{}
class EpiTemplateException extends EpiException{}
Empty file modified src/EpiSession.php
100644 → 100755
Empty file.
64 changes: 55 additions & 9 deletions src/EpiTemplate.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
class EpiTemplate
class EpiTemplate implements EpiTemplateInterface
{
const PHP_EXT = 'EpiTemplate_Extendable';
const HAML = 'EpiTemplate_MtHaml';
private static $instances, $employ;

/**
* EpiRoute::display('/path/to/template.php', $array);
* @name display
Expand Down Expand Up @@ -98,16 +102,58 @@ public function jsonResponse($data)
header('Content-type: application/x-json');
echo $json;
}
}

/*
* @param type required
* @params optional
*/
public static function getInstance()
{
$params = func_get_args();
$hash = md5(json_encode($params));
if(isset(self::$instances[$hash]))
return self::$instances[$hash];

function getTemplate()
{
static $template;
if($template)
return $template;
$type = $params[0];
if(!isset($params[1]))
$params[1] = array();
self::$instances[$hash] = new $type($params[1]);
self::$instances[$hash]->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();
}
}
25 changes: 25 additions & 0 deletions src/EpiTemplate_Extendable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
class EpiTemplate_Extendable extends EpiTemplate implements EpiTemplateInterface
{
public function display($template = null, $vars = null)
{
$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'];
}

$_content = $this->get($template, $vars);

if (is_file(Epi::getPath('view').'/'.$_template) || is_file($_template)) {
$vars['_content'] = $_content;
return parent::display($_template, $vars);
} else {
echo $_content;
}
}
}
40 changes: 40 additions & 0 deletions src/EpiTemplate_MtHaml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
class EpiTemplate_MtHaml extends EpiTemplate implements EpiTemplateInterface
{
private $haml;

public function __construct() {
$this->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;
}
}