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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,19 @@ default: this will be the base path of your template

h2o use this path to load additional templates and extensions.

You can either explicity set the search path
You can either explicity set the search path which accepts a string with a single path or an array of path(s).

`$template = new H2o('index.html', array('searchpath' => '/sites/common_templates'));`
All paths should terminate with a backslash `/`.

Array of paths:

`$paths = array( '/your/path/1/', '/your/path/2/');`

`$template = new H2o('index.html', array('searchpath' => $paths));`

or a single string,

`$template = new H2o('index.html', array('searchpath' => '/your/unique/path/'));`

or It will try to find the searchpath for you

Expand Down
15 changes: 9 additions & 6 deletions h2o.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function getOptions($options = array()) {
'cache_ttl' => 3600, // file | apc | memcache
'searchpath' => false,
'autoescape' => true,

// Enviroment setting
'BLOCK_START' => '{%',
'BLOCK_END' => '%}',
Expand Down Expand Up @@ -63,13 +63,16 @@ function __construct($file = null, $options = array()) {
if (!class_exists($loader, false))
throw new Exception('Invalid template loader');

if (isset($options['searchpath']))
$this->searchpath = realpath($options['searchpath']).DS;
elseif ($file)
if (isset($options['searchpath'])){
$this->searchpath = is_array($options['searchpath']) ?
$options['searchpath'] :
realpath($options['searchpath']).DS ;
} else if ($file) {
$this->searchpath = dirname(realpath($file)).DS;
else
} else {
$this->searchpath = getcwd().DS;

}

$this->loader = new $loader($this->searchpath, $this->options);
}
$this->loader->runtime = $this;
Expand Down
67 changes: 47 additions & 20 deletions h2o/loaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ function cache_read($file, $object, $ttl = 3600) {}
class H2o_File_Loader extends H2o_Loader {

function __construct($searchpath, $options = array()) {
if (is_file($searchpath)) {
$searthpath = dirname($searchpath).DS;
}
if (!is_dir($searchpath))
throw new TemplateNotFound($filename);

$this->searchpath = realpath($searchpath) . DS;
$this->setOptions($options);
if (!is_array($searchpath) && is_string($search_path)){
if (is_file($searchpath)) {
$searthpath = dirname($searchpath).DS;
}

if (!is_dir($searchpath))
throw new TemplateNotFound($filename);
}

$this->searchpath = $searchpath;
$this->setOptions($options);
}

function setOptions($options = array()) {
Expand All @@ -34,10 +38,10 @@ function setOptions($options = array()) {
}
}

function read($filename) {
function read($filename) {
if (!is_file($filename))
$filename = $this->searchpath . $filename;

$filename = $this->get_template_path($this->searchpath,$filename);
if (is_file($filename)) {
$source = file_get_contents($filename);
return $this->runtime->parse($source);
Expand All @@ -46,14 +50,36 @@ function read($filename) {
}
}

function read_cache($filename) {
if (!$this->cache)
function get_template_path($search_path, $filename){
if(is_array($search_path)){
for ( $i = 0 ; $i < count($search_path); $i++ ) {
if(file_exists($search_path[$i] . $filename)) {
$filename = $search_path[$i] . $filename;
return $filename;
break;
} else {
continue;
}
}
throw new TemplateNotFound($filename);
} else {
return $search_path . $filename;
}
}

function read_cache($filename) {
if (!$this->cache){
$filename = is_array($this->searchpath) ?
$this->get_template_path($this->searchpath,$filename) :
$this->read($filename) ;
return $this->read($filename);

}

if (!is_file($filename))
$filename = $this->searchpath . $filename;

$filename = $this->get_template_path($this->searchpath,$filename);
$filename = realpath($filename);

$cache = md5($filename);
$object = $this->cache->read($cache);
$this->cached = $object && !$this->expired($object);
Expand Down Expand Up @@ -82,14 +108,15 @@ function flush_cache() {

function expired($object) {
if (!$object) return false;

$files = array_merge(array($object->filename), $object->templates);

foreach ($files as $file) {
if (!is_file($file))
$file = $this->searchpath.$file;

if (!is_file($file))
$file = $this->get_template_path($this->searchpath,$file);
if ($object->created < filemtime($file))
return true;
return true;
}
return false;
}
Expand Down
57 changes: 43 additions & 14 deletions spec/loader_spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,44 @@
class Describe_file_loader extends SimpleSpec {

function should_be_able_to_read_template() {
chdir(dirname(__FILE__));

$h2o = h2o('templates/a.html');
expects($h2o->nodelist)->should_be_a('Nodelist');

$h2o = h2o('templates/b.html');
expects($h2o->nodelist)->should_be_a('Nodelist');

$h2o = h2o('templates/emails/base.html');
expects($h2o->nodelist)->should_be_a('Nodelist');

chdir(dirname(__FILE__));

$h2o = h2o('templates/a.html');
expects($h2o->nodelist)->should_be_a('Nodelist');

$h2o = h2o('templates/b.html');
expects($h2o->nodelist)->should_be_a('Nodelist');

$h2o = h2o('templates/emails/base.html');
expects($h2o->nodelist)->should_be_a('Nodelist');
}

function should_be_able_to_read_template_from_search_path_array() {

$paths = array(dirname(__FILE__).DS.'templates'.DS);

$h2o = h2o('templates/a.html', array('searchpath' => $paths));
expects($h2o->nodelist)->should_be_a('Nodelist');

$h2o = h2o('templates/b.html', array('searchpath' => $paths) );
expects($h2o->nodelist)->should_be_a('Nodelist');

$h2o = h2o('templates/emails/base.html', array('searchpath' => $paths));
expects($h2o->nodelist)->should_be_a('Nodelist');
}


function should_be_able_to_load_template_lazily() {
chdir(dirname(__FILE__));

$paths = array(dirname(__FILE__).DS.'templates'.DS);

$h2o = h2o('a.html', array('searchpath' => $paths));
expects($h2o->render())->should_not_be_empty();

$h2o = new H2o('a.html', array('searchpath' => 'templates'));
expects($h2o->render())->should_not_be_empty();

$h2o = new H2o(null, array('searchpath' => 'templates'));
$h2o->loadTemplate('b.html');
expects($h2o->render())->should_not_be_empty();
Expand All @@ -34,11 +55,18 @@ function should_read_from_alternitive_working_path() {
}

function should_load_subtemplate_upon_extends_tag() {
$paths = array(dirname(__FILE__).DS.'templates'.DS);

$h2o = h2o('emails/campaign1.html', array(
'searchpath' => dirname(__FILE__).DS.'templates'
));
expects($h2o->render())->should_match('/Dear Customer/');

$h2o = h2o('emails/campaign1.html', array(
'searchpath' => $paths
));
expects($h2o->render())->should_match('/Dear Customer/');

$h2o->loadTemplate('emails/campaign2.html');
expects($h2o->render())->should_match('/Hello Customer/');
}
Expand All @@ -59,7 +87,8 @@ function shouble_cache_main_template() {
}

function should_invalidate_cache_if_any_subtemplates_has_updated() {
$opt = array('searchpath' => dirname(__FILE__).DS.'templates');

$opt = array('searchpath' => dirname(__FILE__).DS.'templates'.DS);

# Load template twice to make sure its cached
$h2o = h2o('emails/campaign1.html', $opt);
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function_exists('simpletest_autorun') or require 'simpletest/autorun.php';
function_exists('simpletest_autorun') or require '../../simpletest/autorun.php';
class_exists('H2o', false) or require dirname(dirname(__FILE__)).'/h2o.php';
class_exists('SimpleSpec', false) or require dirname(__FILE__).'/spec.php';

Expand Down