Klasa wczytująca szablon:
<?php
class TemplateEngine {
/* Rysuje szablon */
public function drawTemplate($sTitle) {
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'; echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'; echo '<script type="text/javascript" src="_js/tiny_mce/tiny_mce.js"></script>'; echo '<script type="text/javascript"> tinyMCE.init({
// General options
mode : "textareas",
theme : "advanced",
skin : "o2k7",
plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,in
sertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,f
ullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups,au
tosave",
// Theme options
theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justif
ycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizesele
ct",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,out
dent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|
,insertdate,inserttime,preview,|,forecolor,backcolor",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iesp
ell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acro
nym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
// Example content CSS (should be your site CSS)
content_css : "css/content.css",
// Drop lists for link/image/media/template dialogs
template_external_list_url : "lists/template_list.js",
external_link_list_url : "lists/link_list.js",
external_image_list_url : "lists/image_list.js",
media_external_list_url : "lists/media_list.js",
// Replace values for the template plugin
template_replace_values : {
username : "Some User",
staffid : "991234"
}
});
</script>';
}
/* Wycztuje szablon */
public function loadTemplate($sTemplateName) {
if(file_exists($_SERVER['DOCUMENT_ROOT'] . APP_WEB_PATH
. '/_administrator/_templates/' . $sTemplateName . '.tpl')) { $template = fopen($_SERVER['DOCUMENT_ROOT'] . APP_WEB_PATH
. '/_administrator/_templates/' . $sTemplateName . '.tpl', 'r'); while(!feof($template)) { $this->_tpl
.= fgets($template); }
}
}
public function readContentMode($sModeTemplate) {
$this->_tpl
= str_replace('<input type="hidden" value="%_content_%">', $this->loadTemplate($sModeTemplate), $this->_tpl
); }
public function changeString($stringToChange) {
$this->_tpl
= str_replace('%_content_inner_%', $stringToChange, $this->_tpl
); }
public function changeIntroducedString($sIntroducedString, $sStringToChange) {
$this->_tpl
= str_replace($sIntroducedString, $stringToChange, $this->_tpl
); }
private $_tpl;
}
?>
Szablon zapisany w pliku .tpl:
<form method="POST" action="admin-panel.php?mode=add-article"> <td>%_content_inner_%
</td> <td><input type="text" name="new_article_title"></td> <td><textarea id="elm2" name="article_short_description" rows="15" cols="80" style="width: 80%">%_article_short_description_%
</textarea></td> <td><textarea id="elm3" name="article_full_description" rows="15" cols="80" style="width: 80%"></textarea></td> <td><input type="submit" value="Zapisz zmiany"></td>
Wywołuję najpierw metodę loadTemplate następnie:
/* Edycja artykułu */
} else if($_GET['mode'] == 'edit-article') {
$objCategory = new category();
$result = $objCategory->getCategoryList();
$html = '<select name="category_id">';
foreach ($result as $value) {
$html .= '<option value="' . $value['wp_category_id'] . '">' . $value['wp_category_name'] . '</option>';
}
$html .= '</select>';
$objTpl->changeString($html);
$objTpl->changeIntroducedString('%_article_short_description_%', 'fds');
}
$objTpl->drawTemplate('Witaj ' . $_SESSION['user_name'] . ' w panelu zarządzania treścią!');
niestety druga instrukcja $objTpl->changeIntroducedString('%_article_short_description_%', 'fds'); nie zamienia mi łańcucha na 'fds' przy czym '%_article_short_description_%' nie jest drukowany w oknie przeglądarki.