Jump to content

[SOLVED] While Loop Through Template Engine


Altec

Recommended Posts

I'm really at a loss at to how to accurately describe what is wrong, so here is some code:

 

<?php

            $query = mysql_query("SELECT * FROM categories");
            if(!$query) {
                $page = new Page('templates/sys/info.html');
                $page->replace_tags(array(
                    'CONTENT'       => $string['CAT_DISPLAY_ERROR'] . ' - ' . mysql_error(),
                    'REFRESH'       => '',
                ));
                $page->output();
                exit;
            }
            
            // List our categories
            $page = new Page('templates/index.html');
            $page->replace_tags(array(
                'PAGE_TITLE'    => 'Category List',
                'MENU'          => 'templates/menu.html',
                'TITLE'         => 'Category List',
                'CONTENT'       => 'templates/sys/cat_list.php',
                'SIDEBAR'       => 'templates/sidebar.html',
                'FOOTER'        => 'templates/footer.html',
            ));
            $page->output();

?>

 

My problem lies in cat_list.php:

 

<?php
while($row = mysql_fetch_array($query)) {
?>

<input type="radio" name="category" value="<?php echo $row['id']; ?>" /> <?php echo $row['name']; ?><br />

<?php
}
?>

 

As you can see, I'm trying to "carry over" $query through to complete the while loop; as expected, this results in nothing. How can I "carry over" this variable? My goal is for no HTML in the PHP code and so far I haven't thought of a way to do so in this situation.

 

For reference, my template engine:

 

<?php
class Page {
    var $page;

    function Page($template) {
        if(file_exists($template)) {
            $this->page = join('', file($template));
        }
        else {
            die('Template file ' . $template . ' not found.');
        }
    }

    function parse($file) {
        ob_start();
        include($file);
        $buffer = ob_get_contents();
        ob_end_clean();
        return $buffer;
    }

    function replace_tags($tags = array()) {
        if(sizeof($tags) > 0) {
            foreach ($tags as $tag => $data) {
                $data = (file_exists($data)) ? $this->parse($data) : $data;
                $this->page = eregi_replace('{' . $tag . '}', $data, $this->page);
            }
        }
        else {
            die('No tags designated for replacement.');
        }
    }

    function output() {
        echo $this->page;
    }
}
?>

Given your examples you would need to do something like....

 

<?php

while($row = mysql_fetch_array($query)) {
  $page = new Page('templates/template_holding_your_radio_buttons.html');
  $page->replace_tags(array(
    'VALUE'    => $row['id'],
    'NAME'          => $row['name']
  ));
  $page->output();

?>

 

I don't really see the benifit of template engines to be honest. Most of the time they just cost you parsing time, and you template creators still end up needing to learn some basic logic. You may aswell use minimal amounts of php and be done with it.

Thanks for the suggestion. :)

 

As for your comments on template engines, I'm using this one to output error pages; this is the first "real" page I'm trying to output. I'm starting to see a very very VERY big issue in the future for this script if I try to keep all HTML out of the source.

Archived

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

×
×
  • 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.