2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // | offers you the ability to separate your PHP code and your HTML |
6 // +----------------------------------------------------------------------+
8 // | Copyright (C) 2001,2002 R.P.J. Velzeboer, The Netherlands |
10 // | This program is free software; you can redistribute it and/or |
11 // | modify it under the terms of the GNU General Public License |
12 // | as published by the Free Software Foundation; either version 2 |
13 // | of the License, or (at your option) any later version. |
15 // | This program is distributed in the hope that it will be useful, |
16 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 // | GNU General Public License for more details. |
20 // | You should have received a copy of the GNU General Public License |
21 // | along with this program; if not, write to the Free Software |
22 // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
23 // | 02111-1307, USA. |
25 // | Author: R.P.J. Velzeboer, rovel@codocad.nl The Netherlands |
27 // +----------------------------------------------------------------------+
28 // | http://templatepower.codocad.com |
29 // +----------------------------------------------------------------------+
31 // $Id: class.TemplatePower.inc.php 459 2004-12-13 19:01:46Z sacerdoti $
33 define("T_BYFILE", 0);
36 define("TP_ROOTBLOCK", '_ROOT');
38 class TemplatePowerParser
40 var $tpl_base; //Array( [filename/varcontent], [T_BYFILE/T_BYVAR] )
41 var $tpl_include; //Array( [filename/varcontent], [T_BYFILE/T_BYVAR] )
44 var $parent = Array(); // $parent[{blockname}] = {parentblockname}
45 var $defBlock = Array();
53 * TemplatePowerParser::TemplatePowerParser()
61 function TemplatePowerParser( $tpl_file, $type )
63 $this->version
= '3.0.1';
65 $this->tpl_base
= Array( $tpl_file, $type );
67 $this->ignore_stack
= Array( false );
71 * TemplatePowerParser::__errorAlert()
78 function __errorAlert( $message )
80 print( '<br>'. $message .'<br>\r\n');
84 * TemplatePowerParser::__prepare()
92 $this->defBlock
[ TP_ROOTBLOCK
] = Array();
93 $tplvar = $this->__prepareTemplate( $this->tpl_base
[0], $this->tpl_base
[1] );
95 $initdev["varrow"] = 0;
96 $initdev["coderow"] = 0;
97 $initdev["index"] = 0;
98 $initdev["ignore"] = false;
100 $this->__parseTemplate( $tplvar, TP_ROOTBLOCK
, $initdev );
105 * TemplatePowerParser::__cleanUp()
113 for( $i=0; $i <= $this->tpl_count
; $i++
)
115 $tplvar = 'tpl_rawContent'. $i;
116 unset( $this->{$tplvar} );
121 * TemplatePowerParser::__prepareTemplate()
129 function __prepareTemplate( $tpl_file, $type )
131 $tplvar = 'tpl_rawContent'. $this->tpl_count
;
133 if( $type == T_BYVAR
)
135 $this->{$tplvar}["content"] = preg_split('/\n/', $tpl_file, -1, PREG_SPLIT_DELIM_CAPTURE
);
139 $this->{$tplvar}["content"] = @file
( $tpl_file ) or
140 die( $this->__errorAlert('TemplatePower Error: Couldn\'t open [ '. $tpl_file .' ]!'));
143 $this->{$tplvar}["size"] = sizeof( $this->{$tplvar}["content"] );
151 * TemplatePowerParser::__parseTemplate()
160 function __parseTemplate( $tplvar, $blockname, $initdev )
162 $coderow = $initdev["coderow"];
163 $varrow = $initdev["varrow"];
164 $index = $initdev["index"];
165 $ignore = $initdev["ignore"];
167 while( $index < $this->{$tplvar}["size"] )
169 if ( preg_match('/<!--[ ]?(START|END) IGNORE -->/', $this->{$tplvar}["content"][$index], $ignreg) )
171 if( $ignreg[1] == 'START')
174 array_push( $this->ignore_stack
, true );
179 array_pop( $this->ignore_stack
);
184 if( !end( $this->ignore_stack
) )
186 if( preg_match('/<!--[ ]?(START|END|INCLUDE|INCLUDESCRIPT|REUSE) BLOCK : (.+)-->/', $this->{$tplvar}["content"][$index], $regs))
188 //remove trailing and leading spaces
189 $regs[2] = trim( $regs[2] );
191 if( $regs[1] == 'INCLUDE')
193 $include_defined = true;
195 //check if the include file is assigned
196 if( isset( $this->tpl_include
[ $regs[2] ]) )
198 $tpl_file = $this->tpl_include
[ $regs[2] ][0];
199 $type = $this->tpl_include
[ $regs[2] ][1];
202 if (file_exists( $regs[2] )) //check if defined as constant in template
204 $tpl_file = $regs[2];
209 $include_defined = false;
212 if( $include_defined )
214 //initialize startvalues for recursive call
215 $initdev["varrow"] = $varrow;
216 $initdev["coderow"] = $coderow;
217 $initdev["index"] = 0;
218 $initdev["ignore"] = false;
220 $tplvar2 = $this->__prepareTemplate( $tpl_file, $type );
221 $initdev = $this->__parseTemplate( $tplvar2, $blockname, $initdev );
223 $coderow = $initdev["coderow"];
224 $varrow = $initdev["varrow"];
228 if( $regs[1] == 'INCLUDESCRIPT' )
230 $include_defined = true;
232 //check if the includescript file is assigned by the assignInclude function
233 if( isset( $this->tpl_include
[ $regs[2] ]) )
235 $include_file = $this->tpl_include
[ $regs[2] ][0];
236 $type = $this->tpl_include
[ $regs[2] ][1];
239 if (file_exists( $regs[2] )) //check if defined as constant in template
241 $include_file = $regs[2];
246 $include_defined = false;
249 if( $include_defined )
253 if( $type == T_BYFILE
)
255 if( !@include_once
( $include_file ) )
257 $this->__errorAlert( 'TemplatePower Error: Couldn\'t include script [ '. $include_file .' ]!' );
263 eval( "?>" . $include_file );
266 $this->defBlock
[$blockname]["_C:$coderow"] = ob_get_contents();
273 if( $regs[1] == 'REUSE' )
276 if (preg_match('/(.+) AS (.+)/', $regs[2], $reuse_regs))
278 $originalbname = trim( $reuse_regs[1] );
279 $copybname = trim( $reuse_regs[2] );
281 //test if original block exist
282 if (isset( $this->defBlock
[ $originalbname ] ))
285 $this->defBlock
[ $copybname ] = $this->defBlock
[ $originalbname ];
287 //tell the parent that he has a child block
288 $this->defBlock
[ $blockname ]["_B:". $copybname ] = '';
290 //create index and parent info
291 $this->index
[ $copybname ] = 0;
292 $this->parent
[ $copybname ] = $blockname;
296 $this->__errorAlert('TemplatePower Error: Can\'t find block \''. $originalbname .'\' to REUSE as \''. $copybname .'\'');
301 //so it isn't a correct REUSE tag, save as code
302 $this->defBlock
[$blockname]["_C:$coderow"] = $this->{$tplvar}["content"][$index];
308 if( $regs[2] == $blockname ) //is it the end of a block
312 else //its the start of a block
314 //make a child block and tell the parent that he has a child
315 $this->defBlock
[ $regs[2] ] = Array();
316 $this->defBlock
[ $blockname ]["_B:". $regs[2]] = '';
318 //set some vars that we need for the assign functions etc.
319 $this->index
[ $regs[2] ] = 0;
320 $this->parent
[ $regs[2] ] = $blockname;
322 //prepare for the recursive call
324 $initdev["varrow"] = 0;
325 $initdev["coderow"] = 0;
326 $initdev["index"] = $index;
327 $initdev["ignore"] = false;
329 $initdev = $this->__parseTemplate( $tplvar, $regs[2], $initdev );
331 $index = $initdev["index"];
335 else //is it code and/or var(s)
337 //explode current template line on the curly bracket '{'
338 $sstr = explode( '{', $this->{$tplvar}["content"][$index] );
342 if (current($sstr) != '')
344 //the template didn't start with a '{',
345 //so the first element of the array $sstr is just code
346 $this->defBlock
[$blockname]["_C:$coderow"] = current( $sstr );
352 //find the position of the end curly bracket '}'
353 $pos = strpos( current($sstr), "}" );
355 if ( ($pos !== false) && ($pos > 0) )
357 //a curly bracket '}' is found
358 //and at least on position 1, to eliminate '{}'
360 //note: position 1 taken without '{', because we did explode on '{'
362 $strlength = strlen( current($sstr) );
363 $varname = substr( current($sstr), 0, $pos );
365 if (strstr( $varname, ' ' ))
367 //the varname contains one or more spaces
368 //so, it isn't a variable, save as code
369 $this->defBlock
[$blockname]["_C:$coderow"] = '{'. current( $sstr );
375 $this->defBlock
[$blockname]["_V:$varrow" ] = $varname;
378 //is there some code after the varname left?
379 if( ($pos +
1) != $strlength )
381 //yes, save that code
382 $this->defBlock
[$blockname]["_C:$coderow"] = substr( current( $sstr ), ($pos +
1), ($strlength - ($pos +
1)) );
389 //no end curly bracket '}' found
390 //so, the curly bracket is part of the text. Save as code, with the '{'
391 $this->defBlock
[$blockname]["_C:$coderow"] = '{'. current( $sstr );
399 $this->defBlock
[$blockname]["_C:$coderow"] = $this->{$tplvar}["content"][$index];
407 $initdev["varrow"] = $varrow;
408 $initdev["coderow"] = $coderow;
409 $initdev["index"] = $index;
416 * TemplatePowerParser::version()
423 return $this->version
;
427 * TemplatePowerParser::assignInclude()
437 function assignInclude( $iblockname, $value, $type=T_BYFILE
)
439 $this->tpl_include
["$iblockname"] = Array( $value, $type );
443 class TemplatePower
extends TemplatePowerParser
445 var $index = Array(); // $index[{blockname}] = {indexnumber}
446 var $content = Array();
451 var $globalvars = Array();
455 * TemplatePower::TemplatePower()
463 function TemplatePower( $tpl_file='', $type= T_BYFILE
)
465 TemplatePowerParser
::TemplatePowerParser( $tpl_file, $type );
467 $this->prepared
= false;
468 $this->showUnAssigned
= false;
469 $this->serialized
= false; //added: 26 April 2002
473 * TemplatePower::__deSerializeTPL()
481 function __deSerializeTPL( $stpl_file, $type )
483 if( $type == T_BYFILE
)
485 $serializedTPL = @file
( $stpl_file ) or
486 die( $this->__errorAlert('TemplatePower Error: Can\'t open [ '. $stpl_file .' ]!'));
490 $serializedTPL = $stpl_file;
493 $serializedStuff = unserialize( join ('', $serializedTPL) );
495 $this->defBlock
= $serializedStuff["defBlock"];
496 $this->index
= $serializedStuff["index"];
497 $this->parent
= $serializedStuff["parent"];
501 * TemplatePower::__makeContentRoot()
507 function __makeContentRoot()
509 $this->content
[ TP_ROOTBLOCK
."_0" ][0] = Array( TP_ROOTBLOCK
);
510 $this->currentBlock
= &$this->content
[ TP_ROOTBLOCK
."_0" ][0];
514 * TemplatePower::__assign()
522 function __assign( $varname, $value)
524 if( sizeof( $regs = explode('.', $varname ) ) == 2 ) //this is faster then preg_match
526 $ind_blockname = $regs[0] .'_'. $this->index
[ $regs[0] ];
528 $lastitem = sizeof( $this->content
[ $ind_blockname ] );
530 $lastitem > 1 ?
$lastitem-- : $lastitem = 0;
532 $block = &$this->content
[ $ind_blockname ][ $lastitem ];
537 $block = &$this->currentBlock
;
540 $block["_V:$varname"] = $value;
545 * TemplatePower::__assignGlobal()
553 function __assignGlobal( $varname, $value )
555 $this->globalvars
[ $varname ] = $value;
560 * TemplatePower::__outputContent()
567 function __outputContent( $blockname )
569 $numrows = sizeof( $this->content
[ $blockname ] );
571 for( $i=0; $i < $numrows; $i++
)
573 $defblockname = $this->content
[ $blockname ][$i][0];
575 for( reset( $this->defBlock
[ $defblockname ]); $k = key( $this->defBlock
[ $defblockname ]); next( $this->defBlock
[ $defblockname ] ) )
579 print( $this->defBlock
[ $defblockname ][$k] );
584 $defValue = $this->defBlock
[ $defblockname ][$k];
586 if( !isset( $this->content
[ $blockname ][$i][ "_V:". $defValue ] ) )
588 if( isset( $this->globalvars
[ $defValue ] ) )
590 $value = $this->globalvars
[ $defValue ];
594 if( $this->showUnAssigned
)
596 //$value = '{'. $this->defBlock[ $defblockname ][$k] .'}';
597 $value = '{'. $defValue .'}';
607 $value = $this->content
[ $blockname ][$i][ "_V:". $defValue ];
616 if( isset( $this->content
[ $blockname ][$i][$k] ) )
618 $this->__outputContent( $this->content
[ $blockname ][$i][$k] );
625 function __printVars()
627 var_dump($this->defBlock
);
628 print("<br>--------------------<br>");
629 var_dump($this->content
);
638 * TemplatePower::serializedBase()
644 function serializedBase()
646 $this->serialized
= true;
647 $this->__deSerializeTPL( $this->tpl_base
[0], $this->tpl_base
[1] );
651 * TemplatePower::showUnAssigned()
658 function showUnAssigned( $state = true )
660 $this->showUnAssigned
= $state;
664 * TemplatePower::prepare()
672 if (!$this->serialized
)
674 TemplatePowerParser
::__prepare();
677 $this->prepared
= true;
679 $this->index
[ TP_ROOTBLOCK
] = 0;
680 $this->__makeContentRoot();
684 * TemplatePower::newBlock()
691 function newBlock( $blockname )
693 $parent = &$this->content
[ $this->parent
[$blockname] .'_'. $this->index
[$this->parent
[$blockname]] ];
695 $lastitem = sizeof( $parent );
696 $lastitem > 1 ?
$lastitem-- : $lastitem = 0;
698 $ind_blockname = $blockname .'_'. $this->index
[ $blockname ];
700 if ( !isset( $parent[ $lastitem ]["_B:$blockname"] ))
702 //ok, there is no block found in the parentblock with the name of {$blockname}
704 //so, increase the index counter and create a new {$blockname} block
705 $this->index
[ $blockname ] +
= 1;
707 $ind_blockname = $blockname .'_'. $this->index
[ $blockname ];
709 if (!isset( $this->content
[ $ind_blockname ] ) )
711 $this->content
[ $ind_blockname ] = Array();
714 //tell the parent where his (possible) children are located
715 $parent[ $lastitem ]["_B:$blockname"] = $ind_blockname;
718 //now, make a copy of the block defenition
719 $blocksize = sizeof( $this->content
[ $ind_blockname ] );
721 $this->content
[ $ind_blockname ][ $blocksize ] = Array( $blockname );
723 //link the current block to the block we just created
724 $this->currentBlock
= &$this->content
[ $ind_blockname ][ $blocksize ];
728 * TemplatePower::assignGlobal()
736 function assignGlobal( $varname, $value )
738 if (is_array( $varname ))
740 foreach($varname as $var => $value)
742 $this->__assignGlobal( $var, $value );
747 $this->__assignGlobal( $varname, $value );
753 * TemplatePower::assign()
761 function assign( $varname, $value='' )
763 if (is_array( $varname ))
765 foreach($varname as $var => $value)
767 $this->__assign( $var, $value );
772 $this->__assign( $varname, $value );
777 * TemplatePower::gotoBlock()
784 function gotoBlock( $blockname )
786 if ( isset( $this->defBlock
[ $blockname ] ) )
788 $ind_blockname = $blockname .'_'. $this->index
[ $blockname ];
790 //get lastitem indexnumber
791 $lastitem = sizeof( $this->content
[ $ind_blockname ] );
793 $lastitem > 1 ?
$lastitem-- : $lastitem = 0;
795 //link the current block
796 $this->currentBlock
= &$this->content
[ $ind_blockname ][ $lastitem ];
801 * TemplatePower::getVarValue()
808 function getVarValue( $varname )
810 if( sizeof( $regs = explode('.', $varname ) ) == 2 ) //this is faster then preg_match
812 $ind_blockname = $regs[0] .'_'. $this->index
[ $regs[0] ];
814 $lastitem = sizeof( $this->content
[ $ind_blockname ] );
816 $lastitem > 1 ?
$lastitem-- : $lastitem = 0;
818 $block = &$this->content
[ $ind_blockname ][ $lastitem ];
823 $block = &$this->currentBlock
;
826 return $block["_V:$varname"];
830 * TemplatePower::printToScreen()
836 function printToScreen()
840 $this->__outputContent( TP_ROOTBLOCK
.'_0' );
844 $this->__errorAlert('TemplatePower Error: Template isn\'t prepared!');
849 * TemplatePower::getOutputContent()
855 function getOutputContent()
859 $this->printToScreen();
861 $content = ob_get_contents();