2013-08-25 38 views
0

我正在研究我的模板類,我沒有任何好主意讓我的tpl文件中的可編輯循環。 (我沒有使用Smarty或其他框架)。我有我的.tpl文件是這樣的:製作可編輯循環模板(PHP)

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

感謝您的幫助。

回答

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

} 

與用法:

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

我被啓發笨解析器類。

+0

有沒有一種方法可以處理模板中的if語句,例如如果有title2,請展示它,否則,不? – Ralf

+0

不。現在我正在使用[Twig](http://twig.sensiolabs.org/)模板引擎的框架。我可以推薦給你。這款引擎擁有幾乎所有的功能,而他沒有的功能,只需添加一些自定義過濾器即可。 :) –