Q:

Making editable loop to template (PHP)

I'm working on my template class and I don't have any good idea to make editable LOOP in my tpl file. (I'm not using Smarty or some other framework). I have my .tpl file like this:
  <ul>
    {TABLE_ROWS}
  </ul>

{TABLE_ROWS} is parsed in PHP loop:
                while($row = mysql_fetch_array($query))
                {
                    $content = $this->tools->cutString(strip_tags($row['content']), 100);
                    $time = date("m/Y", $row['time']);
                    $table_rows .= "<li>
                    <strong>" . $time . "</strong> » " . $content . "
                    <div class='riadokZmien'><a href='" . ADMIN_URL . "shortnews/edit/" . $row['id'] . "' class='edit'><strong>Upraviť</strong></a><a href='" . ADMIN_URL . "shortnews/delete/" . $row['id'] . "' onclick=\"return confirm('Naozaj vymazať? Tento krok už nepôjde vrátiť späť.');\" class='del'><strong>Odstrániť</strong></a>
                    </div></li>";
                }
                $replace = Array(
                'TABLE_ROWS' => $table_rows,
                );
                $this->loadTemplate('shortnews');
                // .......
        if(isSet($replace) && $replace)
        $this->parseTags($replace);

But it is not effective if page template is completely changed. Then I have to edit code in my module.
I am trying to solve it as follows:
  <ul>
    {TABLE_ROWS_START}
      <li><strong>{row.TIME}</strong> {row.CONTENT}
      <div class='riadokZmien'><a href="{ADMIN_URL}shortnews/edit/{row.ID}' class='edit'><strong>Upraviť</strong></a><a href="{ADMIN_URL}shortnews/delete/{row.ID}" onclick=\"return confirm('Naozaj vymazať? Tento krok už nepôjde vrátiť späť.');\" class='del'><strong>Odstrániť</strong></a>
      </div></li>
    {TABLE_ROWS_END}
  </ul>

or something like this and this parse in PHP as loop, but I don't have any good idea
This is loadTemplate method:
public function loadTemplate($tpl_name)
{
    $path = ($this->admin === false ? TEMPLATES_PATH : ADMIN_TPL_PATH);
    if(file_exists($path . $this->template_folder . DS . 'tpl' . DS . $tpl_name . '.tpl'))
    {
        $this->content = file_get_contents($path . $this->template_folder . DS . 'tpl' . DS . $tpl_name . '.tpl');
    }
    else
    {
        die ('Cannot load main template: ' . $path . $this->template_folder . DS . 'tpl' . DS . $tpl_name . '.tpl');
    }
}

And there's parseTags method:
public function parseTags($replace = Array())
{
    $replaced = Array();
    foreach ($replace as $key => $value)
    {
        $replaced['{' . $key . '}'] = $value;
    }
    $this->content = str_replace(array_keys($replaced), array_values($replaced), $this->content);
}

Thanks for any help.

A:

I have solution if somebody has same problem. My new Template class:
class Template
{
    public $template_name,
    $template_author,
    $template_version,
    $template_folder,
    $content,
    $info;

    private $l_delim = '{',
            $r_delim = '}';

    public function __construct()
    {
        global $info;
        $this->info = $info;
                // sql query for $template_* variables
    }

    public function loadTemplate($template)
    {
        $path = ($this->info->admin ? MAIN_TPL_PATH : ADMIN_TPL_PATH) . $this->template_folder . DS . 'tpl' . DS . $template . '.tpl';
        if(file_exists($path))
        {
            $this->content = @file_get_contents($path);
        }
        else
            die('Error with loading template: ' . $path);
    }

    public function parseTags($tags)
    {
        foreach($tags as $key => $value)
        {
            if(is_array($value))
                $this->content = $this->parsePair($key, $value, $this->content);
            else
                $this->content = $this->parseSingle($key, (string) $value, $this->content);
        }
    }

    private function parseSingle($key, $value, $string)
    {
        return str_replace($this->l_delim . $key . $this->r_delim, $value, $string);
    }

    private function parsePair($variable, $data, $string)
    {
        if(($match = $this->matchPair($string, $variable)) === false)
            return $string;

        $str = '';
        foreach($data as $row)
        {
            $temp = $match['1'];
            foreach($row as $key => $val)
            {
                if(!is_array($val))
                    $temp = $this->parseSingle($key, $val, $temp);
                else
                    $temp = $this->parsePair($key, $val, $temp);
            }
            $str .= $temp;
        }       
        return str_replace($match['0'], $str, $string);
    }

    private function matchPair($string, $variable)
    {
        if (!preg_match("|" . preg_quote($this->l_delim) . $variable . preg_quote($this->r_delim) . "(.+?)". preg_quote($this->l_delim) . '/' . $variable . preg_quote($this->r_delim) . "|s", $string, $match))
            return false;
        return $match;
    }

}

And usage:
module.tpl:
{blog_entries}
<h3>{title}</h3>
<p>{body}</p>
{/blog_entries}

module.php:
$tpl = new Template();
$tpl->loadTemplate('path/to/tpl/file/module.tpl');
$data = array(
              'blog_title'   => 'My Blog Title',
              'blog_heading' => 'My Blog Heading',
              'blog_entries' => array(
                                      array('title' => 'Title 1', 'body' => 'Body 1'),
                                      array('title' => 'Title 2', 'body' => 'Body 2'),
                                      array('title' => 'Title 3', 'body' => 'Body 3'),
                                      array('title' => 'Title 4', 'body' => 'Body 4'),
                                      array('title' => 'Title 5', 'body' => 'Body 5')
                                      )
            );

$tpl->parseTags($data);
print($tpl->content);

I was inspired by CodeIgniter parser class.

