Commit 1d71c45a82aad00119336da2f0f4bda830830923
1 parent
518fba9c
KTTemplating is a flexible framework for finding and using templates,
allowing multiple search paths and also overriding default templates. It can support multiple templating languages (but currently hard-codes Smarty support), and also can support choosing which of multiple instances of a template to use (but currently just uses the last one found). git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3443 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
102 additions
and
0 deletions
lib/templating/templating.inc.php
0 → 100644
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * $Id$ | |
| 4 | + * | |
| 5 | + * Template factory class | |
| 6 | + * | |
| 7 | + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com | |
| 8 | + * | |
| 9 | + * This program is free software; you can redistribute it and/or modify | |
| 10 | + * it under the terms of the GNU General Public License as published by | |
| 11 | + * the Free Software Foundation; either version 2 of the License, or | |
| 12 | + * (at your option) any later version. | |
| 13 | + * | |
| 14 | + * This program is distributed in the hope that it will be useful, | |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + * GNU General Public License for more details. | |
| 18 | + * | |
| 19 | + * You should have received a copy of the GNU General Public License | |
| 20 | + * along with this program; if not, write to the Free Software | |
| 21 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 | + * | |
| 23 | + * @version $Revision$ | |
| 24 | + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa | |
| 25 | + */ | |
| 26 | + | |
| 27 | +require_once(KT_LIB_DIR . "/templating/smartytemplate.inc.php"); | |
| 28 | + | |
| 29 | +class KTTemplating { | |
| 30 | + /** Templating language registry */ | |
| 31 | + var $aTemplateRegistry; | |
| 32 | + | |
| 33 | + /** Location registry */ | |
| 34 | + var $aLocationRegistry; | |
| 35 | + | |
| 36 | + // {{{ KTTemplating | |
| 37 | + function KTTemplating() { | |
| 38 | + $this->aTemplateRegistry = array( | |
| 39 | + "smarty" => "KTSmartyTemplate", | |
| 40 | + ); | |
| 41 | + | |
| 42 | + $this->aLocationRegistry = array( | |
| 43 | + "core" => "templates", | |
| 44 | + ); | |
| 45 | + } | |
| 46 | + // }}} | |
| 47 | + | |
| 48 | + // {{{ _chooseTemplate | |
| 49 | + function _chooseTemplate($templatename, $aPossibilities) { | |
| 50 | + $aLocs = array_keys($aPossibilities); | |
| 51 | + return $aPossibilities[$aLocs[count($aLocs) - 1]]; | |
| 52 | + } | |
| 53 | + // }}} | |
| 54 | + | |
| 55 | + // {{{ _findTemplate | |
| 56 | + function _findTemplate($templatename) { | |
| 57 | + $aPossibilities = array(); | |
| 58 | + | |
| 59 | + foreach ($this->aLocationRegistry as $loc => $path) { | |
| 60 | + $fulldirectory = KT_DIR . "/" . $path . "/"; | |
| 61 | + foreach (array_keys($this->aTemplateRegistry) as $suffix) { | |
| 62 | + $fullpath = $fulldirectory . $templatename . "." . $suffix; | |
| 63 | + if (file_exists($fullpath)) { | |
| 64 | + $aPossibilities[$loc] = array($suffix, $fullpath); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + if (count($aPossibilities) === 0) { | |
| 70 | + return PEAR::raiseError("No template found"); | |
| 71 | + } | |
| 72 | + | |
| 73 | + return $this->_chooseTemplate($templatename, $aPossibilities); | |
| 74 | + } | |
| 75 | + // }}} | |
| 76 | + | |
| 77 | + // {{{ loadTemplate | |
| 78 | + /** | |
| 79 | + * Create an object that conforms to the template interface, using | |
| 80 | + * the correct template system for the given template. | |
| 81 | + * | |
| 82 | + * KTI: Theoretically, this will do path searching in multiple | |
| 83 | + * locations, allowing the user and possibly third-parties to | |
| 84 | + * replace templates. | |
| 85 | + */ | |
| 86 | + function &loadTemplate($templatename) { | |
| 87 | + $res = $this->_findTemplate($templatename); | |
| 88 | + if (PEAR::isError($res)) { | |
| 89 | + return $res; | |
| 90 | + } | |
| 91 | + list($sLanguage, $sTemplatePath) = $res; | |
| 92 | + $sClass = $this->aTemplateRegistry[$sLanguage]; | |
| 93 | + if (!class_exists($sClass)) { | |
| 94 | + return PEAR::raiseError("Could not find template language"); | |
| 95 | + } | |
| 96 | + | |
| 97 | + return new $sClass($sTemplatePath); | |
| 98 | + } | |
| 99 | + // }}} | |
| 100 | +} | |
| 101 | + | |
| 102 | +?> | ... | ... |