Jump to content

Archived

This topic is now archived and is closed to further replies.

QUACK

Loop gone backwards ...

Recommended Posts

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

Share this post


Link to post
Share on other sites
ok im not very good in PHP so this may not help but i dont think u set a value it looks empty to me but like i said im not good so dont flame me for this

Share this post


Link to post
Share on other sites
I'll try and illustrate what's happening.

<!-- BEGIN example_block -->
{example_block.S_LOOPTEST}<br />
<!-- END example_block -->

you run assign_block and it does this:

<!-- BEGIN example_block -->
{example_block.S_LOOPTEST}<br />
<!-- END example_block -->
Value: 0<br />

Run it again and you get:

<!-- BEGIN example_block -->
{example_block.S_LOOPTEST}<br />
<!-- END example_block -->
Value: 1<br />
Value: 0<br />

Run it again and you get:

<!-- BEGIN example_block -->
{example_block.S_LOOPTEST}<br />
<!-- END example_block -->
Value: 2<br />
Value: 1<br />
Value: 0<br />

Run delete_block and you get:

Value: 2<br />
Value: 1<br />
Value: 0<br />

Now I don't know exactly what you're shooting for but you could fix it by printing the values above the block:

[code]   function assign_block ( $block_name, $array )
      ...
      # Replace keys with old keys + block in new_tpl.
      $this->new_tpl = str_replace ( $match[ 0 ], $block . $match[ 0 ], $this->new_tpl );
   }[/code]

Share this post


Link to post
Share on other sites
ah $block . $match[ 0 ] worked >.< all i had to do was switch it haha thanks very much

it does:

Value: 0
Value: 1
Value: 2

:) and ur statement was correct, it leave the block there and adds more too it until it deleted, simpel and works.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.