📄 itx.php
字号:
<?php//// +----------------------------------------------------------------------+// | PHP version 4.0 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2001 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.02 of the PHP license, |// | that is bundled with this package in the file LICENSE, and is |// | available at through the world-wide-web at |// | http://www.php.net/license/2_02.txt. |// | If you did not receive a copy of the PHP license and are unable to |// | obtain it through the world-wide-web, please send a note to |// | license@php.net so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Author: Ulf Wendel <ulf.wendel@phpdoc.de> |// +----------------------------------------------------------------------+//// $Id: ITX.php,v 1.8 2003/03/12 02:25:16 pajoye Exp $//require_once('HTML/Template/IT.php');require_once 'HTML/Template/IT_Error.php';/*** Integrated Template Extension - ITX** With this class you get the full power of the phplib template class.* You may have one file with blocks in it but you have as well one main file* and multiple files one for each block. This is quite usefull when you have* user configurable websites. Using blocks not in the main template allows* you to modify some parts of your layout easily.** Note that you can replace an existing block and add new blocks at runtime.* Adding new blocks means changing a variable placeholder to a block.** @author Ulf Wendel <uw@netuse.de>* @access public* @version $Id: ITX.php,v 1.8 2003/03/12 02:25:16 pajoye Exp $* @package IT[X]*/class HTML_Template_ITX extends HTML_Template_IT { /** * Array with all warnings. * @var array * @access public * @see $printWarning, $haltOnWarning, warning() */ var $warn = array(); /** * Print warnings? * @var array * @access public * @see $haltOnWarning, $warn, warning() */ var $printWarning = false; /** * Call die() on warning? * @var boolean * @access public * @see $warn, $printWarning, warning() */ var $haltOnWarning = false; /** * RegExp used to test for a valid blockname. * @var string */ var $checkblocknameRegExp = ''; /** * Functionnameprefix used when searching function calls in the template. * @var string */ var $functionPrefix = 'func_'; /** * Functionname RegExp. * @var string */ var $functionnameRegExp = '[_a-zA-Z]+[A-Za-z_0-9]*'; /** * RegExp used to grep function calls in the template. * * The variable gets set by the constructor. * * @var string * @see HTML_Template_IT() */ var $functionRegExp = ''; /** * List of functions found in the template. * * @var array */ var $functions = array(); /** * List of callback functions specified by the user. * * @var array */ var $callback = array(); /** * Builds some complex regexps and calls the constructor * of the parent class. * * Make sure that you call this constructor if you derive your own * template class from this one. * * @see HTML_Template_IT() */ function HTML_Template_ITX($root = '') { $this->checkblocknameRegExp = '@' . $this->blocknameRegExp . '@'; $this->functionRegExp = '@' . $this->functionPrefix . '(' . $this->functionnameRegExp . ')\s*\(@sm'; $this->HTML_Template_IT($root); } // end func constructor function init() { $this->free(); $this->buildFunctionlist(); $this->findBlocks($this->template); // we don't need it any more $this->template = ''; $this->buildBlockvariablelist(); } // end func init /** * Replaces an existing block with new content. * * This function will replace a block of the template and all blocks * contained in the replaced block and add a new block insted, means * you can dynamically change your template. * * Note that changing the template structure violates one of the IT[X] * development goals. I've tried to write a simple to use template engine * supporting blocks. In contrast to other systems IT[X] analyses the way * you've nested blocks and knows which block belongs into another block. * The nesting information helps to make the API short and simple. Replacing * blocks does not only mean that IT[X] has to update the nesting * information (relatively time consumpting task) but you have to make sure * that you do not get confused due to the template change itself. * * @param string Blockname * @param string Blockcontent * @param boolean true if the new block inherits the content * of the old block * @return boolean * @throws IT_Error * @see replaceBlockfile(), addBlock(), addBlockfile() * @access public */ function replaceBlock($block, $template, $keep_content = false) { if (!isset($this->blocklist[$block])) { return new IT_Error( "The block "."'$block'". " does not exist in the template and thus it can't be replaced.", __FILE__, __LINE__ ); } if ('' == $template) { return new IT_Error('No block content given.', __FILE__, __LINE__); } if ($keep_content) { $blockdata = $this->blockdata[$block]; } // remove all kinds of links to the block / data of the block $this->removeBlockData($block); $template = "<!-- BEGIN $block -->" . $template . "<!-- END $block -->"; $parents = $this->blockparents[$block]; $this->findBlocks($template); $this->blockparents[$block] = $parents; // KLUDGE: rebuild the list for all block - could be done faster $this->buildBlockvariablelist(); if ($keep_content) { $this->blockdata[$block] = $blockdata; } // old TODO - I'm not sure if we need this // update caches return true; } // end func replaceBlock /** * Replaces an existing block with new content from a file. * * @brother replaceBlock() * @param string Blockname * @param string Name of the file that contains the blockcontent * @param boolean true if the new block inherits the content of the old block */ function replaceBlockfile($block, $filename, $keep_content = false) { return $this->replaceBlock($block, $this->getFile($filename), $keep_content); } // end func replaceBlockfile /** * Adds a block to the template changing a variable placeholder * to a block placeholder. * * Add means "replace a variable placeholder by a new block". * This is different to PHPLibs templates. The function loads a * block, creates a handle for it and assigns it to a certain * variable placeholder. To to the same with PHPLibs templates you would * call set_file() to create the handle and parse() to assign the * parsed block to a variable. By this PHPLibs templates assume * that you tend to assign a block to more than one one placeholder. * To assign a parsed block to more than only the placeholder you specify * in this function you have to use a combination of getBlock() * and setVariable(). * * As no updates to cached data is necessary addBlock() and addBlockfile() * are rather "cheap" meaning quick operations. * * The block content must not start with <!-- BEGIN blockname --> * and end with <!-- END blockname --> this would cause overhead and * produce an error. * * @param string Name of the variable placeholder, the name must be unique * within the template. * @param string Name of the block to be added * @param string Content of the block * @return boolean * @throws IT_Error * @see addBlockfile() * @access public */ function addBlock($placeholder, $blockname, $template) { // Don't trust any user even if it's a programmer or yourself... if ('' == $placeholder) { return new IT_Error('No variable placeholder given.', __FILE__, __LINE__ ); } else if ( '' == $blockname || !preg_match($this->checkblocknameRegExp, $blockname) ) { return new IT_Error("No or invalid blockname '$blockname' given.", __FILE__, __LINE__ ); } else if ('' == $template) { return new IT_Error('No block content given.', __FILE__, __LINE__); } else if (isset($this->blocklist[$blockname])) { return new IT_Error('The block already exists.', __FILE__, __LINE__ ); } // find out where to insert the new block $parents = $this->findPlaceholderBlocks($placeholder); if (0 == count($parents)) { return new IT_Error( "The variable placeholder". " '$placeholder' was not found in the template.", __FILE__, __LINE__ ); } else if ( count($parents) > 1 ) { reset($parents); while (list($k, $parent) = each($parents)) { $msg .= "$parent, "; } $msg = substr($parent, -2); return new IT_Error("The variable placeholder "."'$placeholder'". " must be unique, found in multiple blocks '$msg'.", __FILE__, __LINE__ ); } $template = "<!-- BEGIN $blockname -->" . $template . "<!-- END $blockname -->"; $this->findBlocks($template); if ($this->flagBlocktrouble) { return false; // findBlocks() already throws an exception } $this->blockinner[$parents[0]][] = $blockname; $this->blocklist[$parents[0]] = preg_replace( '@' . $this->openingDelimiter . $placeholder . $this->closingDelimiter . '@', $this->openingDelimiter . '__' . $blockname . '__' . $this->closingDelimiter, $this->blocklist[$parents[0]] ); $this->deleteFromBlockvariablelist($parents[0], $placeholder); $this->updateBlockvariablelist($blockname); /* // check if any inner blocks were found if(is_array($this->blockinner[$blockname]) and count($this->blockinner[$blockname]) > 0) { // loop through inner blocks, registering the variable placeholders in each foreach($this->blockinner[$blockname] as $childBlock) { $this->updateBlockvariablelist($childBlock); } } */ return true; } // end func addBlock /** * Adds a block taken from a file to the template changing a variable * placeholder to a block placeholder. * * @param string Name of the variable placeholder to be converted * @param string Name of the block to be added * @param string File that contains the block * @brother addBlock() */ function addBlockfile($placeholder, $blockname, $filename) { return $this->addBlock($placeholder, $blockname, $this->getFile($filename)); } // end func addBlockfile /** * Returns the name of the (first) block that contains * the specified placeholder. * * @param string Name of the placeholder you're searching * @param string Name of the block to scan. If left out (default) * all blocks are scanned. * @return string Name of the (first) block that contains * the specified placeholder. * If the placeholder was not found or an error occured * an empty string is returned. * @throws IT_Error * @access public */ function placeholderExists($placeholder, $block = '') { if ('' == $placeholder) { new IT_Error('No placeholder name given.', __FILE__, __LINE__); return ''; } if ('' != $block && !isset($this->blocklist[$block])) { new IT_Error("Unknown block '$block'.", __FILE__, __LINE__); return ''; } // name of the block where the given placeholder was found $found = ''; if ('' != $block) { if (is_array($variables = $this->blockvariables[$block])) { // search the value in the list of blockvariables reset($variables); while (list($k, $variable) = each($variables)) { if ($k == $placeholder) { $found = $block; break; } } } } else { // search all blocks and return the name of the first block that // contains the placeholder reset($this->blockvariables); while (list($blockname, $variables) = each($this->blockvariables)){ if (is_array($variables) && isset($variables[$placeholder])) { $found = $blockname; break; } } } return $found; } // end func placeholderExists /** * Checks the list of function calls in the template and * calls their callback function. * * @access public */ function performCallback() { reset($this->functions); while (list($func_id, $function) = each($this->functions)) { if (isset($this->callback[$function['name']])) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -