From 1d71c45a82aad00119336da2f0f4bda830830923 Mon Sep 17 00:00:00 2001 From: nbm Date: Fri, 22 Jul 2005 13:31:25 +0000 Subject: [PATCH] KTTemplating is a flexible framework for finding and using templates, allowing multiple search paths and also overriding default templates. --- lib/templating/templating.inc.php | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+), 0 deletions(-) create mode 100644 lib/templating/templating.inc.php diff --git a/lib/templating/templating.inc.php b/lib/templating/templating.inc.php new file mode 100644 index 0000000..2acdab8 --- /dev/null +++ b/lib/templating/templating.inc.php @@ -0,0 +1,102 @@ +aTemplateRegistry = array( + "smarty" => "KTSmartyTemplate", + ); + + $this->aLocationRegistry = array( + "core" => "templates", + ); + } + // }}} + + // {{{ _chooseTemplate + function _chooseTemplate($templatename, $aPossibilities) { + $aLocs = array_keys($aPossibilities); + return $aPossibilities[$aLocs[count($aLocs) - 1]]; + } + // }}} + + // {{{ _findTemplate + function _findTemplate($templatename) { + $aPossibilities = array(); + + foreach ($this->aLocationRegistry as $loc => $path) { + $fulldirectory = KT_DIR . "/" . $path . "/"; + foreach (array_keys($this->aTemplateRegistry) as $suffix) { + $fullpath = $fulldirectory . $templatename . "." . $suffix; + if (file_exists($fullpath)) { + $aPossibilities[$loc] = array($suffix, $fullpath); + } + } + } + + if (count($aPossibilities) === 0) { + return PEAR::raiseError("No template found"); + } + + return $this->_chooseTemplate($templatename, $aPossibilities); + } + // }}} + + // {{{ loadTemplate + /** + * Create an object that conforms to the template interface, using + * the correct template system for the given template. + * + * KTI: Theoretically, this will do path searching in multiple + * locations, allowing the user and possibly third-parties to + * replace templates. + */ + function &loadTemplate($templatename) { + $res = $this->_findTemplate($templatename); + if (PEAR::isError($res)) { + return $res; + } + list($sLanguage, $sTemplatePath) = $res; + $sClass = $this->aTemplateRegistry[$sLanguage]; + if (!class_exists($sClass)) { + return PEAR::raiseError("Could not find template language"); + } + + return new $sClass($sTemplatePath); + } + // }}} +} + +?> -- libgit2 0.21.4