I created a template class in Python worked perfect. I decided to convert it to PHP. It works as expected execpt one thing. When I do a loop and assign the block it counts backwards. Ill explaine ... [code]<?php ######################################################### # FILENAME: template.php # COPYRIGHT: (C) 2006, Kyle King (aibotca@yahoo.ca ) ######################################################### ######################################## # Class: Template # 1: set_file. # 2: assign_vars. # 3: assign_block. # 4: delete_block. # 5: parse. ######################################## class Template { var $new_tpl; ######################################## # Function: set_file # 1: template, string. ######################################## function set_file ( $template ) { # Set new_tpl to the contents of $template $this->new_tpl = file_get_contents ( $template ); } ######################################## # Function: assign_vars # 1: array, array. ######################################## function assign_vars ( $array ) { # Loop though the array. foreach ( $array AS $key => $value ) { # Replace the key with the value in new_tpl. $this->new_tpl = str_replace ( '{' . $key . '}', $value, $this->new_tpl ); } } ######################################## # Function: assign_block # 1: block_name, string. # 2: array, array. ######################################## function assign_block ( $block_name, $array ) { # Get all the block, put them in an array. preg_match ( '#<!-- BEGIN ' . $block_name . ' -->\n(.+)\n<!-- END ' . $block_name . ' -->#', $this->new_tpl, $match ); $block = $match [ 1 ]; # Loop though the array. foreach ( $array AS $key => $value ) { # Replace key with value in block. $block = str_replace ( '{' . $block_name . '.' . $key . '}', $value, $block ); } # Replace keys with old keys + block in new_tpl. $this->new_tpl = str_replace ( $match[ 0 ], $match[ 0 ] . $block, $this->new_tpl ); } ######################################## # Function: delete_block # 1: block_name, string. ######################################## function delete_block ( $block_name ) { # Get rid of the old keys and blocks. $this->new_tpl = preg_replace ( '#<!-- BEGIN ' . $block_name . ' -->\n(.+)\n<!-- END ' . $block_name . ' -->#', '', $this->new_tpl ); } ######################################## # Function: parse ######################################## function parse ( ) { # Print it out onto the page ^^ print $this->new_tpl; } } $template = new Template ( ); $template->set_file ( 'example.tpl' ); $template->assign_vars ( array ( 'L_EXAMPLE' => 'Example One ...', 'L_EXAMPLE2' => 'Example Two ...' ) ); for ( $i = 0; $i < 3; $i++ ) { $template->assign_block ( 'example_block', array ( 'S_LOOPTEST' => 'Value: ' . $i ) ); } $template->delete_block ( 'example_block' ); for ( $x = 0; $x < 3; $x++ ) { $template->assign_block ( 'example_block2', array ( 'S_LOOPTEST2' => 'Value: ' . $x ) ); } $template->delete_block ( 'example_block2' ); $template->parse ( ); ?>[/code] [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--] for ( $i = 0; $i < 3; $i++ ) { $template->assign_block ( 'example_block', array ( 'S_LOOPTEST' => 'Value: ' . $i ) ); } $template->delete_block ( 'example_block' );[/quote] Thats what does the looping :/ Now this is whats in example.tpl: [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]{L_EXAMPLE}<br />{L_EXAMPLE2} <br /> <br /> <!-- BEGIN example_block --> {example_block.S_LOOPTEST}<br /> <!-- END example_block --> <br /> <br /> <!-- BEGIN example_block2 --> <b>{example_block2.S_LOOPTEST2}</b><br /> <!-- END example_block2 -->[/quote] It outputs [code]Example One ... Example Two ... Value: 2 Value: 1 Value: 0 Value 2: 2 Value 2: 1 Value 2: 0[/code] Why is it going backwards :(. See assign_block in the PHP code thats what does the block :( Thanks