Source for file yapter.php
Documentation is available at yapter.php
* Yapter - Yet Another PHP Template Engine
* @copyright Copyright 2001-2006 Vincent Driessen
* @copyright Copyright 2006-2007 Kevin L. Papendick
* @author Vincent Driessen <nvie@users.sourceforge.net>
* @author Kevin L. Papendick <kevinp@polarlava.com>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* Yapter warning level constant - Silent
* @see Yapter::setWarningLevel()
* Yapter warning level constant - Report notices
* @see Yapter::setWarningLevel()
* Yapter warning level constant - Report warnings
* @see Yapter::setWarningLevel()
define('E_YAPTER_WARNING', 2);
* Yapter warning level constant - Report errors
* @see Yapter::setWarningLevel()
* Yapter warning level constant - Die on errors
* @see Yapter::setWarningLevel()
define('E_YAPTER_DIE_ON_ERROR', 8);
* Yapter warning level constant - All: Report errors, warnings and notices
* @see Yapter::setWarningLevel()
* Yapter - Yet Another PHP Template Engine
* Ignore unknown variables?
* Automatically hide unparsed blocks?
* Template content blocks
* $blox[blockname]['content'] holds the template's content
* $blox[blockname]['numlines'] holds the number of lines in the block
* $blox[blockname]['parsed'] holds an array with the parsed data
* $blox[$_ROOT] always holds the main template
* Tracks all block definitions from which multiple block instances can be created.
* Array of variables available to all blocks
* Warning level verbosity
* Script start time used to calculate debug execution time.
* Declared variables that were not set.
* @param string $file The fully pathed template file
* @param int $level The warning level
function Yapter($file, $level =
E_YAPTER_ALL)
* @param int $line_num Line number.
* @param string $msg The error message.
function error($line_num, $msg)
trigger_error('[' .
date('Y-m-d H:i:s') .
'] Yapter (Line ' .
$line_num .
'): ' .
$msg, E_USER_ERROR);
* @param int $line_num Line number.
* @param string $msg The warning message.
trigger_error('[' .
date('Y-m-d H:i:s') .
'] Yapter (Line ' .
$line_num .
'): ' .
$msg, E_USER_WARNING);
* @param int $line_num Line number.
* @param string $msg The notice message.
function notice($line_num, $msg)
trigger_error('[' .
date('Y-m-d H:i:s') .
'] Yapter (Line ' .
$line_num .
'): ' .
$msg, E_USER_NOTICE);
* Sets the level of verbosity of error/warning/notices that Yapter displays.
* @param int The warning level
* Gets the level of verbosity of error/warning/notices that Yapter displays.
* Sets weather to ignore unknown template variables.
* Gets weather to ignore unknown template variables.
* Sets weather to hide unparsed template blocks.
* Gets weather to hide unparsed template blocks.
* Issue warning the first time an assigned variable is found, but not set.
* @param string $varname The name of the unset template variable.
* List of variables that were found to not be set during parsing.
* @param string $a The name of the unset template variable.
// ...and print a warning once.
* Adds a new block to the blox-array
* @param string $blockname The name of the block.
* @param string $content The content of the block.
$this->blox[$blockname]['content'] =
$content;
$this->blox[$blockname]['numlines'] =
count($this->blox[$blockname]['content']);
$this->blox[$blockname]['parsed'] =
'';
* Adds a new block, filling it with the specified's file contents
* @param string $blockname The name of the block.
* @param string $file The file containing the contents for the block.
$content =
@file($file) or $this->error(__LINE__
, 'Cannot open and read template file ' .
htmlspecialchars($file) .
' for addBlockFromFile() ');
* Adds a block definition to the block-definition array from which other blocks can be copied
* @param string $blockdef The name of an existing block.
* @param string $content The content of the block definition.
TODO: Should this be culled? -klp
if (isset($this->blockDefs[$blockdef])) {
$this->error(__LINE__, 'Block "' . htmlspecialchars($blockdef) . '" already exist and cannot be redefine ');
$this->blockDefs[$blockdef] = $content;
* Copies a block from the block definition array
* @param string $blockname The name of the block.
* @param string $blockdef The name of an existing defined block.
* Handles subprocessing of templates found in the main template file
* @param string $blockname The name of the block.
$currblockcontents =
array();
$block =
&$this->blox[$blockname];
for ($i =
0; $i <
$block['numlines']; $i++
) {
if (isset
($block['content'][$i])) {
$line =
$block['content'][$i];
// Try to find a tag-definition on this line
if (preg_match('/\[(INCLUDE|BLOCK|END|REUSE|SET) ([A-Za-z0-9_.\/-]+)( AS ([A-Za-z0-9_-]+))?]/', $line, $matches)) {
$name =
(!empty($matches[4])) ?
$matches[4] :
$matches[2];
if ($type ==
'END' &&
!isset
($currblockdef)) {
$this->error(__LINE__
, '"[END ' .
$name .
']" found without matching "[BLOCK ' .
$name .
']" or "[SET ' .
$name .
']" ');
if ($type ==
'END' &&
$matches[2] ==
$currblockdef) {
if (isset
($matches[4])) {
$this->error(__LINE__
, 'Given "AS" parameter not allowed in END tags ');
// End the current block definition: add the block to the blox-array
if (isset
($currblockdef) && isset
($currblockcontents) && isset
($currblockname)) {
// Now, try to remove the block from the template definition, replacing it with a var
for ($j =
$i; $j >=
$currblockstart; $j--
) {
if ($j ==
$currblockstart &&
$currblocktype ==
'BLOCK') {
$block['content'][$j] =
"{" .
$currblockname .
"}";
unset
($block['content'][$j]);
// Reset for further preparing.
$currblockcontents =
array();
} elseif (($type ==
'SET' ||
$type ==
'BLOCK') &&
!isset
($currblockname)) {
// Start BLOCK definition
$currblockdef =
$matches[2];
if (isset
($matches[4])) {
$this->error(__LINE__
, 'Given "AS" parameter not allowed in SET tags ');
$currblockname =
$matches[2];
$currblockdef =
$matches[2];
} elseif ($type ==
'INCLUDE' &&
!isset
($currblockname)) {
// Make this line a variable...
$block['content'][$i] =
'{' .
$name .
"}\n";
// ...and include the given file...
} elseif ($type ==
'REUSE' &&
!isset
($currblockname)) {
if (!isset
($matches[4])) {
$this->error(__LINE__
, 'Missing required "AS" parameter in [REUSE ' .
$name .
'] tag ');
// Make this line a variable...
$block['content'][$i] =
'{' .
$matches[4] .
"}\n";
// ...and get this REUSE value from the block definition list...
} elseif ($currblockname !=
$name) {
$currblockcontents[] =
$line;
// No tag-definition... just normal text so do nothing here
if (!empty($currblockname)) {
$currblockcontents[] =
$line;
* Parses the specified block, filling variables and nested blockdefs
* @param string $blockname The name of the block.
* @return array $this->blox[$blockname]['parsed'] Parsed block
function parse($blockname =
'')
$blockname =
$this->_ROOT;
if (!isset
($this->blox[$blockname])) {
$this->error(__LINE__
, 'Unable to parse block because block "' .
htmlspecialchars($blockname) .
'" does not exist ');
$block =
&$this->blox[$blockname];
$parsed =
$block['content'];
// Loop through all the lines of the template and parse variables one-by-one
for ($i =
0; $i <
$block['numlines']; $i++
) {
if (!isset
($parsed[$i])) {
// Look for variables in this line, processing it character-by-character
for ($j =
0; $j <
strlen($line); $j++
) {
if (!isset
($start) &&
$char ==
'{') {
} elseif (isset
($start) &&
$char ==
'}') {
// The sequence {} is not a valid variable value
// Gotcha! Now replace this variable with its contents
// First, check to see if it's a variable or a block that has to be parsed
if (isset
($this->vars[$buffer])) {
$value =
$this->vars[$buffer];
} elseif (isset
($this->blox[$buffer])) {
if ($this->blox[$buffer]['parsed']) {
// The value must be filled with the parsed data from the $buffer block
// Automaticly hide unparsed bloks
// Make the recursive call now
// No variable or block name found by the name of $buffer
// Ignore, replace with an empty string.
$part1 =
substr($line, 0, $start);
$line =
$part1 .
$value .
$part2;
} elseif (isset
($start)) {
// Check to see $char is a proper character (range: [A-Za-z0-9_./-])
// In Yapter 2.13b2, I've added the '/' char as well, to support inclusion
// from Unix paths, like '../../foo.tpl'
if (($char >=
'a' &&
$char <=
'z') ||
($char >=
'0' &&
$char <=
'9') ||
($char >=
'A' &&
$char <=
'Z') ||
($char ==
'_') ||
($char ==
'.') ||
($char ==
'-') ||
($char ==
'/')) {
$this->blox[$blockname]['parsed'] = (array)
$parsed;
return $this->blox[$blockname]['parsed'];
* Assigns a value to a variable ({varname}).
* @param string $varname Template variable name.
* @param string|int$value the value of the template variable.
function set($varname, $value)
$this->vars[$varname] =
$value;
* Assigns values to multiple variables
* @param array $variables Associative array (var_name => var_value) of template variables.
$this->error('Value passed to setVars() is not an array ');
foreach($variables as $varname =>
$value) {
$this->set($varname, $value);
* Assigns the contents of a file to a variable ({varname}).
* @param string $varname Template variable name.
* @param string $file The file containing the contents for the variable.
function setFile($varname, $filename)
$this->set($varname, $value);
* Gets the value of a variable.
* @param string $varname Template variable name.
* @return string|int|booleanThe value of the variable or FALSE if unset.
if ($this->vars[$varname]) {
return $this->vars[$varname];
* Gets the content of a block.
* @param string $blockname The name of the block.
* @return string|booleanThe contents of the block or FALSE if unset.
if ($this->$blox[$blockname]['content']) {
return @implode('', $this->$blox[$blockname]['content']);
* Replaces the contents of one block with the contents of another.
* @param string $blockname The name of the block to be replaced.
* @param string $replace_by_block The name of the block to duplicate.
function replace($block, $replace_by_block)
if (!isset
($this->blox[$block])) {
if (!isset
($this->blox[$replace_by_block])) {
$this->blox[$block]['content'] =
$this->blox[$replace_by_block]['content'];
$this->blox[$block]['numlines'] =
$this->blox[$replace_by_block]['numlines'];
* Hides all the contents of the specified block.
* @param string $block The name of the block.
if (!isset
($this->blox[$block])) {
$this->error(__LINE__
, 'Block "' .
htmlspecialchars($block) .
'" does not exist so it cannot be hidden ');
$this->blox[$block]['content'] =
array();
$this->blox[$block]['numlines'] =
0;
* Resets the parsed data to an empty string and sets the block as "unparsed".
* @param string $blockname The name of the block.
function clear($blockname)
if (!isset
($this->blox[$blockname])) {
$this->error(__LINE__
, 'Block "'.
htmlspecialchars($blockname).
'" does not exist so it cannot be cleared ');
$this->blox[$blockname]['parsed'] =
'';
// often, a variable is set whenever a block should be discarded...
// now reset such a variable to make sure the block is not overriden
unset
($this->vars[$blockname]);
* Gets the parsed content of a block or the ROOT (complete) template.
* @param string $blockname The name of the block.
* @return string|booleanThe parsed contents of the block or FALSE if unset.
$blockname =
$this->_ROOT;
$parsed =
$this->blox[$blockname]['parsed'];
* Prints the full template contents.
* Get the Unix timestamp in seconds.
* @link http://www.php.net/manual/en/function.microtime.php
return ((float)
$usec + (float)
$sec);
* Gets the time in seconds required to parse the template.
* Gets the debug string with the time required to parse the template.
echo
"<p>\n\nThe execution time is <b>".
$this->execTime().
"</b> seconds.<br>\n";
* Yapter constant - Ignore unknown variables
* @deprecated Use Yapter::setIgnoreUnknownVars()
* @see Yapter::setIgnoreUnknownVars()
define('E_YAPTER_IGN_UNKNOWN_VARS', 16);
* Yapter constant - Automaticly hide unparsed blocks
* @deprecated Use Yapter::setHideUnparsedBlocks()
* @see Yapter::setHideUnparsedBlocks()
define('E_YAPTER_AUTO_HIDE_BLOCK', 32);
* Yapter - Yet Another PHP Template Engine (Legacy Class)
* @deprecated This is for legacy support only, new developement should use Yapter class instead.
* Yapter Template Constructor
* @param string $file The fully pathed template file
* @param int|boolean$level The warning|(deprecated) debug level
* @see Yapter::setWarningLevel()
* @deprecated This is for legacy support only, new developement should use the Yapter class constructor.
function Template($file, $level =
E_YAPTER_ALL)
// Older Yapter versions had the possibility of turning
// on the so called "debug mode" with a bool parameter
// as the second argument to this constructor.
// However, since debug mode was dropped and the
// warning level support was built in, it was a logical
// step of replacing the second parameter.
// However, to prevent people from making mistakes,
// we'll check if the user passed a boolean parameter.
// If so, he or she is probably using debug mode, and
// we'll issue a notice in these cases.
$this->notice(__LINE__
, 'Debug mode has not been supported since V2.12, use setWaringLevel() instead ');
//Support deprecated constants...
* Sets whether to parse unknown variables.
* @deprecated Use Yapter::setIgnoreUnknownVars()
* @see Yapter::setIgnoreUnknownVars()
Documentation generated on Tue, 27 Feb 2007 22:01:39 -0500 by phpDocumentor 1.3.1