📄 it.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. |
// +----------------------------------------------------------------------+
// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> |
// +----------------------------------------------------------------------+
//
// $Id: IT.php,v 1.5 2001/03/29 21:49:15 uw Exp $
//
require_once("HTML/IT_Error.php");
/**
* Integrated Template - IT
*
* Well there's not much to say about it. I needed a template class that
* supports a single template file with multiple (nested) blocks inside and
* a simple block API.
*
* The Isotemplate API is somewhat tricky for a beginner although it is the best
* one you can build. template::parse() [phplib template = Isotemplate] requests
* you to name a source and a target where the current block gets parsed into.
* Source and target can be block names or even handler names. This API gives you
* a maximum of fexibility but you always have to know what you do which is
* quite unusual for php skripter like me.
*
* I noticed that I do not any control on which block gets parsed into which one.
* If all blocks are within one file, the script knows how they are nested and in
* which way you have to parse them. IT knows that inner1 is a child of block2, there's
* no need to tell him about this.
*
* <table border>
* <tr>
* <td colspan=2>
* __global__
* <p>
* (hidden and automatically added)
* </td>
* </tr>
* <tr>
* <td>block1</td>
* <td>
* <table border>
* <tr>
* <td colspan=2>block2</td>
* </tr>
* <tr>
* <td>inner1</td>
* <td>inner2</td>
* </tr>
* </table>
* </td>
* </tr>
* </table>
*
* To add content to block1 you simply type:
* <code>$tpl->setCurrentBlock("block1");</code>
* and repeat this as often as needed:
* <code>
* $tpl->setVariable(...);
* $tpl->parseCurrentBlock();
* </code>
*
* To add content to block2 you would type something like:
* <code>
* $tpl->setCurrentBlock("inner1");
* $tpl->setVariable(...);
* $tpl->parseCurrentBlock();
*
* $tpl->setVariable(...);
* $tpl->parseCurrentBlock();
*
* $tpl->parse("block1");
* </code>
*
* This will result in one repition of block1 which contains two repitions
* of inner1. inner2 will be removed if $removeEmptyBlock is set to true which is the default.
*
* Usage:
* <code>
* $tpl = new IntegratedTemplate( [string filerootdir] );
*
* // load a template or set it with setTemplate()
* $tpl->loadTemplatefile( string filename [, boolean removeUnknownVariables, boolean removeEmptyBlocks] )
*
* // set "global" Variables meaning variables not beeing within a (inner) block
* $tpl->setVariable( string variablename, mixed value );
*
* // like with the Isotemplates there's a second way to use setVariable()
* $tpl->setVariable( array ( string varname => mixed value ) );
*
* // Let's use any block, even a deeply nested one
* $tpl->setCurrentBlock( string blockname );
*
* // repeat this as often as you need it.
* $tpl->setVariable( array ( string varname => mixed value ) );
* $tpl->parseCurrentBlock();
*
* // get the parsed template or print it: $tpl->show()
* $tpl->get();
* </code>
*
* @author Ulf Wendel <uw@netuse.de>
* @version $Id: IT.php,v 1.5 2001/03/29 21:49:15 uw Exp $
* @access public
* @package IX[X]
*/
class IntegratedTemplate {
/**
* Contains the error objects
* @var array
* @access public
* @see halt(), $printError, $haltOnError
*/
var $err = array();
/**
* Clear cache on get()?
* @var boolean
*/
var $clearCache = false;
/**
* First character of a variable placeholder ( _{_VARIABLE} ).
* @var string
* @access public
* @see $closingDelimiter, $blocknameRegExp, $variablenameRegExp
*/
var $openingDelimiter = "{";
/**
* Last character of a variable placeholder ( {VARIABLE_}_ ).
* @var string
* @access public
* @see $openingDelimiter, $blocknameRegExp, $variablenameRegExp
*/
var $closingDelimiter = "}";
/**
* RegExp matching a block in the template.
* Per default "sm" is used as the regexp modifier, "i" is missing.
* That means a case sensitive search is done.
* @var string
* @access public
* @see $variablenameRegExp, $openingDelimiter, $closingDelimiter
*/
var $blocknameRegExp = "[0-9A-Za-z_-]+";
/**
* RegExp matching a variable placeholder in the template.
* Per default "sm" is used as the regexp modifier, "i" is missing.
* That means a case sensitive search is done.
* @var string
* @access public
* @see $blocknameRegExp, $openingDelimiter, $closingDelimiter
*/
var $variablenameRegExp = "[0-9A-Za-z_-]+";
/**
* RegExp used to find variable placeholder, filled by the constructor.
* @var string Looks somewhat like @(delimiter varname delimiter)@
* @access public
* @see IntegratedTemplate()
*/
var $variablesRegExp = "";
/**
* RegExp used to strip unused variable placeholder.
* @brother $variablesRegExp
*/
var $removeVariablesRegExp = "";
/**
* Controls the handling of unknown variables, default is remove.
* @var boolean
* @access public
*/
var $removeUnknownVariables = true;
/**
* Controls the handling of empty blocks, default is remove.
* @var boolean
* @access public
*/
var $removeEmptyBlocks = true;
/**
* RegExp used to find blocks an their content, filled by the constructor.
* @var string
* @see IntegratedTemplate()
*/
var $blockRegExp = "";
/**
* Name of the current block.
* @var string
*/
var $currentBlock = "__global__";
/**
* Content of the template.
* @var string
*/
var $template = "";
/**
* Array of all blocks and their content.
*
* @var array
* @see findBlocks()
*/
var $blocklist = array();
/**
* Array with the parsed content of a block.
*
* @var array
*/
var $blockdata = array();
/**
* Array of variables in a block.
* @var array
*/
var $blockvariables = array();
/**
* Array of inner blocks of a block.
* @var array
*/
var $blockinner = array();
/**
* List of blocks to preverse even if they are "empty".
*
* This is something special. Sometimes you have blocks that
* should be preserved although they are empty (no placeholder replaced).
* Think of a shopping basket. If it's empty you have to drop a message to
* the user. If it's filled you have to show the contents of the shopping baseket.
* Now where do you place the message that the basket is empty? It's no good
* idea to place it in you applications as customers tend to like unecessary minor
* text changes. Having another template file for an empty basket means that it's
* very likely that one fine day the filled and empty basket templates have different
* layout. I decided to introduce blocks that to not contain any placeholder but only
* text such as the message "Your shopping basked is empty".
*
* Now if there is no replacement done in such a block the block will be recognized
* as "empty" and by default ($removeEmptyBlocks = true) be stripped off. To avoid this
* you can now call touchBlock() to avoid this.
*
* The array $touchedBlocks stores a list of touched block which must not be removed even
* if they are empty.
*
* @var array $touchedBlocks
* @see touchBlock(), $removeEmptyBlocks
*/
var $touchedBlocks = array();
/**
* Variable cache.
*
* Variables get cached before any replacement is done.
* Advantage: empty blocks can be removed automatically.
* Disadvantage: might take some more memory
*
* @var array
* @see setVariable(), $clearCacheOnParse
*/
var $variableCache = array();
/**
* Clear the variable cache on parse?
*
* If you're not an expert just leave the default false.
* True reduces memory consumption somewhat if you tend to
* add lots of values for unknown placeholder.
*
* @var boolean
*/
var $clearCacheOnParse = false;
/**
* Root directory for all file operations.
* The string gets prefixed to all filenames given.
* @var string
* @see IntegratedTemplate(), setRoot()
*/
var $fileRoot = "";
/**
* Internal flag indicating that a blockname was used multiple times.
* @var boolean
*/
var $flagBlocktrouble = false;
/**
* Flag indicating that the global block was parsed.
* @var boolean
*/
var $flagGlobalParsed = false;
/**
* EXPERIMENTAL! FIXME!
* Flag indication that a template gets cached.
*
* Complex templates require some times to be preparsed
* before the replacement can take place. Often I use
* one template file over and over again but I don't know
* before that I will use the same template file again.
* Now IT could notice this and skip the preparse.
*
* @var boolean
*/
var $flagCacheTemplatefile = true;
/**
* EXPERIMENTAL! FIXME!
*/
var $lastTemplatefile = "";
/**
* Builds some complex regular expressions and optinally sets the file root directory.
*
* Make sure that you call this constructor if you derive your template
* class from this one.
*
* @param string File root directory, prefix for all filenames given to the object.
* @see setRoot()
*/
function IntegratedTemplate($root = "") {
$this->variablesRegExp = "@" . $this->openingDelimiter . "(" . $this->variablenameRegExp . ")" . $this->closingDelimiter . "@sm";
$this->removeVariablesRegExp = "@" . $this->openingDelimiter . "\s*(" . $this->variablenameRegExp . ")\s*" . $this->closingDelimiter . "@sm";
$this->blockRegExp = '@<!--\s+BEGIN\s+(' . $this->blocknameRegExp . ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
$this->setRoot($root);
} // end constructor
/**
* Print a certain block with all replacements done.
* @brother get()
*/
function show($block = "__global__") {
print $this->get($block);
} // end func show
/**
* Returns a block with all replacements done.
*
* @param string name of the block
* @return string
* @throws IT_Error
* @access public
* @see show()
*/
function get($block = "__global__") {
if ("__global__" == $block && !$this->flagGlobalParsed)
$this->parse("__global__");
if (!isset($this->blocklist[$block])) {
new IT_Error("The block '$block' was not found in the template.", __FILE__, __LINE__);
return "";
}
if ($this->clearCache) {
$data = (isset($this->blockdata[$block])) ? $this->blockdata[$block] : "";
unset($this->blockdata[$block]);
return $data;
} else {
return (isset($this->blockdata[$block])) ? $this->blockdata[$block] : "";
}
} // end func get()
/**
* Parses the given block.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -