Source for file Standard.php
Documentation is available at Standard.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 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: Alan Knowles <alan@akbkhome.com> |
// +----------------------------------------------------------------------+
// $Id: Standard.php,v 1.14 2004/04/03 03:02:36 alan_k Exp $
// Standard 'Original Flavour' Flexy compiler
require_once 'HTML/Template/Flexy/Tokenizer.php';
$GLOBALS['_html_template_flexy_compiler_standard']['PO'] = array ();
class HTML_Template_Flexy_Compiler_Standard extends HTML_Template_Flexy_Compiler {
* @params object HTML_Template_Flexy
* @params string|false string to compile of false to use a file.
* @return string filename of template
function compile (&$flexy,$string=false )
// read the entire file into one variable
// note this should be moved to new HTML_Template_Flexy_Token
// and that can then manage all the tokens in one place..
global $_HTML_TEMPLATE_FLEXY_COMPILER;
$gettextStrings = &$_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings'];
$gettextStrings = array (); // reset it.
if (@$this->options['debug']) {
echo " compiling template $flexy->currentTemplate<BR>";
$flexy->_elements = array ();
// replace this with a singleton??
$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions'] = $this->options;
$GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'] = array ();
$GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'] = $flexy->currentTemplate;
$tokenizer = new HTML_Template_Flexy_Tokenizer ($data);
$tokenizer->fileName = $flexy->currentTemplate;
if ($this->options['nonHTML']) {
$tokenizer->ignoreHTML = true;
if ($this->options['allowPHP']) {
$tokenizer->ignorePHP = false;
$res = HTML_Template_Flexy_Token ::buildTokens ($tokenizer);
// turn tokens into Template..
$data = $res->compile ($this);
if ( @$this->options['debug']) {
if ($this->options['nonHTML']) {
// at this point we are into writing stuff...
if ($this->options['compileToString']) {
$flexy->elements = $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'];
if( ($cfp = fopen( $flexy->compiledTemplate , 'w' )) ) {
if (@$this->options['debug']) {
chmod($flexy->compiledTemplate ,0775 );
// make the timestamp of the two items match.
PEAR ::raiseError ('HTML_Template_Flexy::failed to write to '. $flexy->compiledTemplate ,null ,PEAR_ERROR_DIE );
unlink($flexy->getTextStringsFile );
if($gettextStrings && ($cfp = fopen( $flexy->getTextStringsFile , 'w') ) ) {
if( $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'] &&
($cfp = fopen( $flexy->elementsFile , 'w') ) ) {
* This is the base toString Method, it relays into toString{TokenName}
* @param object HTML_Template_Flexy_Token_*
* @return string string to build a template
function toString ($element)
static $len = 26; // strlen('HTML_Template_Flexy_Token_');
$class = get_class ($element);
if (strlen ($class) >= $len) {
$type = substr ($class,$len);
return $this->{'toString'. $type}($element);
$ret .= $element->compileChildren ($this);
$ret .= $element->close ->compile ($this);
* HTML_Template_Flexy_Token_Else toString
* @param object HTML_Template_Flexy_Token_Else
* @return string string to build a template
function toStringElse ($element)
// pushpull states to make sure we are in an area.. - should really check to see
// if the state it is pulling is a if...
if ($element->pullState () === false ) {
return $this->appendHTML (
" <font color=\"red\">Unmatched {else:} on line: {$element->line }</font> "
return $this->appendPhp ("} else {");
* HTML_Template_Flexy_Token_End toString
* @param object HTML_Template_Flexy_Token_Else
* @return string string to build a template
function toStringEnd($element)
// pushpull states to make sure we are in an area.. - should really check to see
// if the state it is pulling is a if...
if ($element->pullState() === false) {
return $this->appendHTML(
"<font color=\"red\">Unmatched {end:} on line: { $element->line }</font>"
return $this->appendPhp("}");
* HTML_Template_Flexy_Token_EndTag toString
* @param object HTML_Template_Flexy_Token_EndTag
* @return string string to build a template
function toStringEndTag($element)
return $this->toStringTag($element);
* HTML_Template_Flexy_Token_Foreach toString
* @param object HTML_Template_Flexy_Token_Foreach
* @return string string to build a template
function toStringForeach($element)
$element->toVar($element->loopOn) . ") || " .
'is_object(' . $element->toVar($element->loopOn) . ')) ' .
'foreach(' . $element->toVar($element->loopOn) . " ";
$ret .= "as \${ $element->key}" ;
$ret .= " => \${ $element->value }" ;
$element->pushVar($element->key);
$element->pushVar($element->value);
return $this->appendPhp($ret);
* HTML_Template_Flexy_Token_If toString
* @param object HTML_Template_Flexy_Token_If
* @return string string to build a template
function toStringIf($element)
$ret = "if (".$element->isNegative . $element->toVar($element->condition) .") {";
return $this->appendPhp($ret);
* converts :h, :u, :r , .....
* @param object HTML_Template_Flexy_Token_Method|Var
* @return array prefix,suffix
function getModifierWrapper($element)
$modifier = $element->modifier . ' ';
$prefix = 'echo urlencode(';
$prefix = 'echo \'<pre>\'; echo htmlspecialchars(print_r(';
$suffix = ',true)); echo \'</pre>\';';
$numberformat = @$GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'];
$prefix = 'echo number_format(';
$suffix = $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions']['numberFormat'] . ')';
case 'b': // nl2br + htmlspecialchars
$prefix = 'echo nl2br(htmlspecialchars(';
$prefix = 'echo htmlspecialchars(';
return array($prefix,$suffix);
* HTML_Template_Flexy_Token_Var toString
* @param object HTML_Template_Flexy_Token_Method
* @return string string to build a template
function toStringVar($element)
// ignore modifier at present!!
list($prefix,$suffix) = $this->getModifierWrapper($element);
return $this->appendPhp( $prefix . $element->toVar($element->value) . $suffix .';');
* HTML_Template_Flexy_Token_Method toString
* @param object HTML_Template_Flexy_Token_Method
* @return string string to build a template
function toStringMethod($element)
// set up the modifier at present!!
list($prefix,$suffix) = $this->getModifierWrapper($element);
if ($element->isConditional) {
$prefix = 'if ('.$element->isNegative;
// check that method exists..
// if (method_exists($object,'method');
$bits = explode('.',$element->method);
$prefix = 'if (isset('.$element->toVar($object).
') && method_exists('.$element->toVar($object) .",'{ $method}')) " . $prefix;
$ret .= $element->toVar($element->method) . "(";
foreach($element->args as $a) {
$ret .= $element->toVar($a);
if ($element->isConditional) {
return $this->appendPhp($ret);
* HTML_Template_Flexy_Token_Processing toString
* @param object HTML_Template_Flexy_Token_Processing
* @return string string to build a template
function toStringProcessing($element)
// if it's XML then quote it..
return $this->appendPhp("echo '" . str_replace("'","\\"."'", $element->value) . "';");
// otherwise it's PHP code - so echo it..
* HTML_Template_Flexy_Token_Text toString
* @param object HTML_Template_Flexy_Token_Text
* @return string string to build a template
function toStringText($element)
// if it's XML then quote it..
* Global variable for gettext replacement
* static object vars will be nice in PHP5 :)
global $_HTML_TEMPLATE_FLEXY_COMPILER;
$gettextStrings = &$_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings'];
static $cleanArray = array(
static $uncleanArray = false;
$uncleanArray = array_flip($cleanArray);
if (!strlen(trim($element->value) )) {
return $this->appendHtml($element->value);
// dont add comments to translation lists.
if (substr($element->value,0,4) == '<!--') {
return $this->appendHtml($element->value);
if (!count($element->argTokens) && !$element->isWord()) {
return $this->appendHtml($element->value);
for ($i=0;$i<strlen($element->value); $i++) {
if (strpos(" \n\t\r\0\x0B", $element->value{$i}) !== false) {
$front .= $element->value{$i};
for ($i=strlen($element->value)-1;$i>-1; $i--) {
if (strpos(" \n\t\r\0\x0B", $element->value{$i}) !== false) {
$rear = $element->value{$i} . $rear;
$value = trim($element->value);
// convert to escaped chars.. (limited..)
$value = strtr($value,$cleanArray);
if (!count($element->argTokens)) {
$gettextStrings[] = $value;
$value = $this->translateString($value);
$value = strtr($value,$uncleanArray);
return $this->appendHtml($front . $value . $rear);
// print_r($element->argTokens );
// these should only be text or vars..
foreach($element->argTokens as $i=>$token) {
$args[] = $token->compile($this);
$gettextStrings[] = $value;
$value = $this->translateString($value);
$value = strtr($value,$uncleanArray);
foreach($bits as $i=>$v) {
* translateString - a gettextWrapper
* tries to do gettext or falls back on File_Gettext
* This has !!!NO!!! error handling - if it fails you just get english..
* @param string string to translate
* @return string translated string..
function translateString($string)
if (@$this->options['debug']) {
echo __CLASS__.":TRANSLATING $string<BR>" ;
if (function_exists('gettext') && !$this->options['textdomain']) {
if (@$this->options['debug']) {
echo __CLASS__.":USING GETTEXT?<BR>";
if (!$this->options['textdomain'] || !$this->options['textdomainDir']) {
// text domain is not set..
if (@$this->options['debug']) {
echo __CLASS__.":MISSING textdomain settings<BR>";
$pofile = $this->options['textdomainDir'] .
'/' . $this->options['locale'] .
'/LC_MESSAGES/' . $this->options['textdomain'] . '.po';
// did we try to load it already..
if (@$GLOBALS['_'.__CLASS__]['PO'][$pofile] === false) {
if (@$this->options['debug']) {
echo __CLASS__.":LOAD failed (Cached):<BR>";
if (!@$GLOBALS['_'.__CLASS__]['PO'][$pofile]) {
// default - cant load it..
$GLOBALS['_'.__CLASS__]['PO'][$pofile] = false;
if (@$this->options['debug']) {
echo __CLASS__.":LOAD failed: { $pofile}<BR>" ;
if (!@include_once 'File/Gettext.php') {
if (@$this->options['debug']) {
echo __CLASS__.":LOAD no File_gettext:<BR>";
$GLOBALS['_'.__CLASS__]['PO'][$pofile] = File_Gettext::factory('PO',$pofile);
$GLOBALS['_'.__CLASS__]['PO'][$pofile]->load();
//echo '<PRE>'.htmlspecialchars(print_r($GLOBALS['_'.__CLASS__]['PO'][$pofile]->strings,true));
$po = &$GLOBALS['_'.__CLASS__]['PO'][$pofile];
// we should have it loaded now...
// this is odd - data is a bit messed up with CR's
if (!isset($po->strings[$string])) {
if (@$this->options['debug']) {
echo __CLASS__.":no match:<BR>";
// finally we have a match!!!
return $po->strings[$string];
* HTML_Template_Flexy_Token_Tag toString
* @param object HTML_Template_Flexy_Token_Tag
* @return string string to build a template
function toStringTag($element) {
if (strpos($element->tag,':') === false) {
$bits = explode(':',$element->tag);
if ($namespace{0} == '/') {
if (empty($this->tagHandlers[$namespace])) {
require_once 'HTML/Template/Flexy/Compiler/Standard/Tag.php';
$this->tagHandlers[$namespace] = &HTML_Template_Flexy_Compiler_Standard_Tag::factory($namespace,$this);
if (!$this->tagHandlers[$namespace] ) {
PEAR::raiseError('HTML_Template_Flexy::failed to create Namespace Handler '.$namespace .
' in file ' . $flexy->compiledTemplate,null,PEAR_ERROR_DIE);
return $this->tagHandlers[$namespace]->toString($element);
Documentation generated on Mon, 11 Mar 2019 10:14:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|