From 2ca13aad316873633c3fd9ba59ee2f5579d90272 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Wed, 18 Jun 2003 04:18:09 +0000 Subject: [PATCH 1/1] - Added a new simple template system: Hooks + IT = HIT. It's a simple template system based on the experience with hooks and IT template system (and specially Sigma implementation of IT). - Moved all to HIT =) --- doc/ROADMAP | 7 +++ doc/bife.xmi | 10 ++-- examples/hit.php | 50 +++++++++++++++++++ examples/hooks/body.html | 10 ++++ examples/hooks/cell.html | 1 + examples/hooks/row.html | 1 + examples/index.php | 9 ++-- examples/templates/album/body.tpl.html | 8 +++ examples/templates/album/cell.tpl.html | 3 ++ examples/templates/album/empty.tpl.html | 1 + examples/templates/album/item.tpl.html | 1 + .../photo.tpl.html} | 0 examples/templates/album/row.tpl.html | 3 ++ examples/templates/bife_album.html | 21 -------- .../{bife_link.html => bife_link.tpl.html} | 0 .../{bife_page.html => bife_page.tpl.html} | 0 .../{bife_title.html => bife_title.tpl.html} | 0 examples/templates/photo.html | 2 - src/BIFE/Album.php | 27 +++++----- src/BIFE/AlbumPhoto.php | 5 +- src/BIFE/Container.php | 11 ++-- src/BIFE/Generic.php | 8 ++- src/BIFE/Page.php | 8 ++- src/BIFE/Parser.php | 2 +- src/BIFE/Root.php | 2 +- src/HTML/Template/HIT.php | 43 ++++++++++++++++ 26 files changed, 174 insertions(+), 59 deletions(-) create mode 100644 examples/hit.php create mode 100644 examples/hooks/body.html create mode 100644 examples/hooks/cell.html create mode 100644 examples/hooks/row.html create mode 100644 examples/templates/album/body.tpl.html create mode 100644 examples/templates/album/cell.tpl.html create mode 100644 examples/templates/album/empty.tpl.html create mode 100644 examples/templates/album/item.tpl.html rename examples/templates/{bife_albumphoto.html => album/photo.tpl.html} (100%) create mode 100644 examples/templates/album/row.tpl.html delete mode 100644 examples/templates/bife_album.html rename examples/templates/{bife_link.html => bife_link.tpl.html} (100%) rename examples/templates/{bife_page.html => bife_page.tpl.html} (100%) rename examples/templates/{bife_title.html => bife_title.tpl.html} (100%) delete mode 100644 examples/templates/photo.html create mode 100644 src/HTML/Template/HIT.php diff --git a/doc/ROADMAP b/doc/ROADMAP index 5b7317d..5e75e61 100644 --- a/doc/ROADMAP +++ b/doc/ROADMAP @@ -3,6 +3,13 @@ $Id$ Version 0.9 =========== + - Finish HTML_Template_HIT. + - Separate Album funtionality function from renderer (BIFE_Album). + - Use new hooks libs. + +Version 0.10 +============ + Makefile: - Make a way to put all classes in a package together in a single file to avoid overhead in require_once calls. diff --git a/doc/bife.xmi b/doc/bife.xmi index 7a89f99..a6ebdee 100644 --- a/doc/bife.xmi +++ b/doc/bife.xmi @@ -5,7 +5,7 @@ umbrello uml modeller http://uml.sf.net 1.1 - + @@ -24,8 +24,12 @@ - - + + + + + + diff --git a/examples/hit.php b/examples/hit.php new file mode 100644 index 0000000..9dae06d --- /dev/null +++ b/examples/hit.php @@ -0,0 +1,50 @@ + | +// +--------------------------------------------------------------------+ +// +// $Id$ +// + +// This is a simple test and example of HTML_Template_HIT + +$tmp = ini_get('include_path'); +ini_set('include_path', "../src:$tmp"); +unset($tmp); +umask('002'); + +require_once 'HTML/Template/HIT.php'; + +$hit =& new HTML_Template_HIT('hooks'); + +$rows = ''; +for ($i = 0; $i < 20; $i++) { + $cells = ''; + for ($j = 0; $j < 20; $j++) { + $cells .= $hit->parse('cell', array('CELL' => "$i,$j")); + } + $rows .= $hit->parse('row', array('ROW' => $cells, 'CACA' => 'fea')); +} +echo $hit->parse('body', array('ROWS' => $rows, 'TITLE' => 'HOLA MUNDO!!!')); + +?> diff --git a/examples/hooks/body.html b/examples/hooks/body.html new file mode 100644 index 0000000..2481bfa --- /dev/null +++ b/examples/hooks/body.html @@ -0,0 +1,10 @@ + + + {TITLE} + + + + {ROWS} +
+ + diff --git a/examples/hooks/cell.html b/examples/hooks/cell.html new file mode 100644 index 0000000..d4889a9 --- /dev/null +++ b/examples/hooks/cell.html @@ -0,0 +1 @@ +{CELL} diff --git a/examples/hooks/row.html b/examples/hooks/row.html new file mode 100644 index 0000000..2889643 --- /dev/null +++ b/examples/hooks/row.html @@ -0,0 +1 @@ +{ROW} diff --git a/examples/index.php b/examples/index.php index 33b14e1..b748728 100644 --- a/examples/index.php +++ b/examples/index.php @@ -29,8 +29,10 @@ $tmp = ini_get('include_path'); ini_set('include_path', "../src:$tmp"); unset($tmp); +umask('002'); -require_once 'HTML/Template/Sigma.php'; +require_once 'HTML/Template/HIT.php'; +#require_once 'HTML/Template/Sigma.php'; require_once 'BIFE/Parser.php'; require_once 'BIFE/Copy.php'; require_once 'BIFE/Page.php'; @@ -41,8 +43,9 @@ require_once 'BIFE/Link.php'; $file = isset($_REQUEST['BIFE']) ? $_REQUEST['BIFE'] : 'index.xbf'; -$template =& new HTML_Template_Sigma('templates', 'compiled_templates'); -$template->setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_ERROR); +$template =& new HTML_Template_HIT('templates'); +#$template =& new HTML_Template_Sigma('templates', 'compiled_templates'); +#$template->setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_ERROR); $parser =& new BIFE_Parser('BIFE_Copy'); $page =& $parser->parseFile($file); diff --git a/examples/templates/album/body.tpl.html b/examples/templates/album/body.tpl.html new file mode 100644 index 0000000..2dd5085 --- /dev/null +++ b/examples/templates/album/body.tpl.html @@ -0,0 +1,8 @@ +

{DESC}

+
+ + + {CONTENTS} + +
+
diff --git a/examples/templates/album/cell.tpl.html b/examples/templates/album/cell.tpl.html new file mode 100644 index 0000000..dcbedbf --- /dev/null +++ b/examples/templates/album/cell.tpl.html @@ -0,0 +1,3 @@ + + {CONTENTS} + diff --git a/examples/templates/album/empty.tpl.html b/examples/templates/album/empty.tpl.html new file mode 100644 index 0000000..a735e36 --- /dev/null +++ b/examples/templates/album/empty.tpl.html @@ -0,0 +1 @@ +VACÍO diff --git a/examples/templates/album/item.tpl.html b/examples/templates/album/item.tpl.html new file mode 100644 index 0000000..c41be64 --- /dev/null +++ b/examples/templates/album/item.tpl.html @@ -0,0 +1 @@ +{DESC} diff --git a/examples/templates/bife_albumphoto.html b/examples/templates/album/photo.tpl.html similarity index 100% rename from examples/templates/bife_albumphoto.html rename to examples/templates/album/photo.tpl.html diff --git a/examples/templates/album/row.tpl.html b/examples/templates/album/row.tpl.html new file mode 100644 index 0000000..6f623b2 --- /dev/null +++ b/examples/templates/album/row.tpl.html @@ -0,0 +1,3 @@ + + {CONTENTS} + diff --git a/examples/templates/bife_album.html b/examples/templates/bife_album.html deleted file mode 100644 index 749e1d0..0000000 --- a/examples/templates/bife_album.html +++ /dev/null @@ -1,21 +0,0 @@ -

{DESC}

-
- - - - - - - - - - - - - -
- {DESC} - - VACÍO -
-
diff --git a/examples/templates/bife_link.html b/examples/templates/bife_link.tpl.html similarity index 100% rename from examples/templates/bife_link.html rename to examples/templates/bife_link.tpl.html diff --git a/examples/templates/bife_page.html b/examples/templates/bife_page.tpl.html similarity index 100% rename from examples/templates/bife_page.html rename to examples/templates/bife_page.tpl.html diff --git a/examples/templates/bife_title.html b/examples/templates/bife_title.tpl.html similarity index 100% rename from examples/templates/bife_title.html rename to examples/templates/bife_title.tpl.html diff --git a/examples/templates/photo.html b/examples/templates/photo.html deleted file mode 100644 index 4ff6528..0000000 --- a/examples/templates/photo.html +++ /dev/null @@ -1,2 +0,0 @@ -{DESC} -
{DESC}
diff --git a/src/BIFE/Album.php b/src/BIFE/Album.php index b3fd409..ff08c40 100644 --- a/src/BIFE/Album.php +++ b/src/BIFE/Album.php @@ -98,12 +98,14 @@ class BIFE_Album extends BIFE_Widget { */ function render(&$template) // ~X2C { - $template->loadTemplateFile('bife_album.html'); + $template->group = 'album'; $root = $this->attrs['DIR']; $list = $this->getList(); $tot = count($list); $rows = ceil($tot / $this->attrs['COLUMNS']); + $html_rows = ''; for ($row = 0; $row < $rows; $row++) { + $html_cells = ''; for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) { $cur = $row * $this->attrs['COLUMNS'] + $col; if ($photo = @$list[$cur]) { @@ -113,22 +115,23 @@ class BIFE_Album extends BIFE_Widget { // FIXME - Si no se pudo crear el thumb, devuelve null // (ver si se agrega otro template para indicar error // o algo asi). - $photo['URL'] = $this->attrs['LINK-URL'] . '?BIFE_ALBUM_FILE=' . - urlencode($photo['FILE']); + $photo['URL'] = $this->attrs['LINK-URL'] . + '?BIFE_ALBUM_FILE=' . urlencode($photo['FILE']); if ($this->attrs['LINK-BIFE']) { - $photo['URL'] .= '&BIFE=' . urlencode($this->attrs['LINK-BIFE']); + $photo['URL'] .= '&BIFE=' . + urlencode($this->attrs['LINK-BIFE']); } - $template->setVariable($photo); - $template->parse('ITEM'); + $html_cell = $template->parse('item', $photo); } else { - $template->touchBlock('EMPTY'); - $template->parse('EMPTY'); + $html_cell = $template->parse('empty'); } + $html_cells .= $template->parse('cell', 'CONTENTS', $html_cell); } - $template->parse('ROW'); + $html_rows .= $template->parse('row', 'CONTENTS', $html_cells); } - $template->setVariable('DESC', $this->getDescription()); - return $template->get(); + return $template->parse( + 'body', + array('DESC' => $this->getDescription(), 'CONTENTS' => $html_rows)); } // -X2C @@ -158,7 +161,7 @@ Returns an array of associative arrays with this keys: if (is_readable("$root/$file") and in_array($ext, $exts)) { $thumb = $this->getThumbFilename("$root/$file"); $return[] = array( - 'FILE' => "$root/$name", + 'FILE' => "$root/$file", 'DESC' => $name, 'THUMB' => is_readable($thumb) ? $thumb : null, ); diff --git a/src/BIFE/AlbumPhoto.php b/src/BIFE/AlbumPhoto.php index ab4cc53..084088d 100644 --- a/src/BIFE/AlbumPhoto.php +++ b/src/BIFE/AlbumPhoto.php @@ -82,9 +82,8 @@ class BIFE_AlbumPhoto extends BIFE_Widget { */ function render(&$template) // ~X2C { - $template->loadTemplateFile('bife_albumphoto.html'); - $template->setVariable($this->attrs); - return $template->get(); + $template->group = 'album'; + return $template->parse('photo', $this->attrs); } // -X2C diff --git a/src/BIFE/Container.php b/src/BIFE/Container.php index 14c7105..7c138ac 100644 --- a/src/BIFE/Container.php +++ b/src/BIFE/Container.php @@ -50,12 +50,14 @@ class BIFE_Container extends BIFE_Widget { /** * Constructor. * + * @param array $attrs Attributes. + * * @return void * @access public */ - function BIFE_Container() // ~X2C + function BIFE_Container($attrs) // ~X2C { - $this->__construct(); + $this->__construct($attrs); } // -X2C @@ -63,11 +65,14 @@ class BIFE_Container extends BIFE_Widget { /** * Constructor. * + * @param array $attrs Attributes. + * * @return void * @access public */ - function __construct() // ~X2C + function __construct($attrs) // ~X2C { + parent::__construct($attrs); $this->contents = array(); } // -X2C diff --git a/src/BIFE/Generic.php b/src/BIFE/Generic.php index e31d813..1e9e06d 100644 --- a/src/BIFE/Generic.php +++ b/src/BIFE/Generic.php @@ -101,11 +101,9 @@ class BIFE_Generic extends BIFE_Container { */ function render(&$template) // ~X2C { - $contents = parent::render($template); - $template->loadTemplateFile(get_class($this).'.html'); - $template->setVariable($this->attrs); - $template->setVariable('CONTENTS', $contents); - return $template->get(); + $this->attrs['CONTENTS'] = parent::render($template); + $template->group = ''; + return $template->parse(get_class($this), $this->attrs); } // -X2C diff --git a/src/BIFE/Page.php b/src/BIFE/Page.php index d1ce348..1d68fa0 100644 --- a/src/BIFE/Page.php +++ b/src/BIFE/Page.php @@ -100,11 +100,9 @@ class BIFE_Page extends BIFE_Root { */ function render(&$template) // ~X2C { - $contents = parent::render($template); - $template->loadTemplateFile('bife_page.html'); - $template->setVariable($this->attrs); - $template->setVariable('CONTENTS', $contents); - return $template->get(); + $this->attrs['CONTENTS'] = parent::render($template); + $template->group = ''; + return $template->parse('bife_page', $this->attrs); } // -X2C diff --git a/src/BIFE/Parser.php b/src/BIFE/Parser.php index 4598ea1..bf96326 100644 --- a/src/BIFE/Parser.php +++ b/src/BIFE/Parser.php @@ -262,4 +262,4 @@ class BIFE_Parser { } // -X2C Class :Parser -?> \ No newline at end of file +?> diff --git a/src/BIFE/Root.php b/src/BIFE/Root.php index 66f9cbc..30e40ef 100644 --- a/src/BIFE/Root.php +++ b/src/BIFE/Root.php @@ -77,4 +77,4 @@ class BIFE_Root extends BIFE_Container { } // -X2C Class :Root -?> \ No newline at end of file +?> diff --git a/src/HTML/Template/HIT.php b/src/HTML/Template/HIT.php new file mode 100644 index 0000000..951cca1 --- /dev/null +++ b/src/HTML/Template/HIT.php @@ -0,0 +1,43 @@ +root = $root; + $this->group = $group; + $this->cache = array(); + } + function parse($name, $vars = null, $val = null) { + if ($this->group) { + $file = "{$this->root}/{$this->group}/$name.tpl.html"; + } else { + $file = "{$this->root}/$name.tpl.html"; + } + if (!isset($this->cache[$file])) { + $this->cache[$file] = join('', file($file)); + } + //if (!is_readable($file)) { + // trigger_error("Can't read '$file'."); + //} + if ($vars) { + if (is_string($vars)) { + $vars = array($vars => $val); + } + foreach ($vars as $key => $val) { + $keys[] = '{' . $key . '}'; + $vals[] = $val; + } + return str_replace($keys, $vals, $this->cache[$file]); + } else { + return $this->cache[$file]; + } + } +} + +?> -- 2.43.0