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);
}
感謝您的幫助。
有沒有一種方法可以處理模板中的if語句,例如如果有title2,請展示它,否則,不? – Ralf
不。現在我正在使用[Twig](http://twig.sensiolabs.org/)模板引擎的框架。我可以推薦給你。這款引擎擁有幾乎所有的功能,而他沒有的功能,只需添加一些自定義過濾器即可。 :) –