0

我正在处理我的模板类,我没有任何好主意在我的 tpl 文件中制作可编辑的 LOOP。(我没有使用 Smarty 或其他框架)。我有这样的 .tpl 文件:

  <ul>
    {TABLE_ROWS}
  </ul>

{TABLE_ROWS} 在 PHP 循环中解析:

                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);

但如果页面模板完全改变,则无效。然后我必须在我的模块中编辑代码。

我试图解决它如下:

  <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>

或类似的东西和这个在PHP中解析为循环,但我没有任何好主意

这是 loadTemplate 方法:

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');
    }
}

还有 parseTags 方法:

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);
}

谢谢你的帮助。

4

1 回答 1

0

如果有人有同样的问题,我有解决方案。我的新模板类:

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;
    }

}

和用法:

模块.tpl:

{blog_entries}
<h3>{title}</h3>
<p>{body}</p>
{/blog_entries}

模块.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);

我受到 CodeIgniter 解析器类的启发。

于 2013-08-29T17:36:15.140 回答