2013-08-25 2 views
0

템플릿 클래스에서 작업하고 있는데 tpl 파일에서 편집 가능한 LOOP을 만들지 않아도됩니다. (나는 Smarty 또는 다른 프레임 워크를 사용하지 않는다).템플릿을 편집 할 수있는 루프 만들기 (PHP)

<ul> 
    {TABLE_ROWS} 
    </ul> 

가 {TABLE_ROWS가} PHP 루프에서 구문 분석 : :이처럼 내 .tpl 파일이

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

도움 주셔서 감사합니다.

답변

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 : 나는 CodeIgniter의에서 영감을했다

$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

예를 들어 템플릿의 문을 처리 할 수있는 방법이 있습니까? 만약 title2가 있다면 보여 주겠습니까? 그렇지 않으면하지 않습니까? – Ralf

+0

아니요. 이제 [Twig] (http://twig.sensiolabs.org/) 템플릿 엔진과 함께 프레임 워크를 사용하고 있습니다. 나는 너에게 그것을 추천 할 수있다. 이 엔진은 거의 모든 기능을 갖추고 있으며 사용자가 원하지 않는 기능이 있습니다. 사용자 정의 필터를 추가하기 만하면됩니다. :) –

관련 문제