Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/5387-loop-gone-backwards/
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]
Link to comment
https://forums.phpfreaks.com/topic/5387-loop-gone-backwards/#findComment-19236
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.