шаблоны.
вот мой элементарный класс работы с шаблонами на php(точнее каркас, который потом можно дополнить всем необходимым):
Код:
<?
class CTemplate
{
var $arrTemplates = array(); // array for template files
var $strParsedTemplate = "";
var $strTemplateRootPath = "templates";
var $strExt = "txt";
var $strTagLimiter = "%";
var $strTemplateName = "";
var $arrTags = array(); // arry for tags
function voidLoadTemplate($strFileName)
{
//clear varibles(this funcion for single template)
unset($this->arrTemplates);
unset($this->strTemplateName);
$arrMatches = array();
$strFilePath = $this->strTemplateRootPath . "/" . $strFileName . "." . $this->strExt;
if (!file_exists($strFilePath))
{
echo "Error. File: <B>" . $strFilePath . "</B> not found\r\n";
exit();
}
$this->strTemplateName = $strFileName;
$this->arrTemplates[$this->strTemplateName]['CONTENT'] = $this->strGetContent($strFilePath);
$this->arrTemplates[$this->strTemplateName]['PARSED'] = false;
//preg_match("/\w+\/(\w+\.$this->strExt)/i",$strFilePath,$arrMatches);
//$strTemplateName = substr($arrMatches[1],0,(strlen($arrMatches[1])-(strlen($this->strExt)+1)));
/*echo "<PRE>\r\n";
print_r($this->arrTemplates);
echo "</PRE>\r\n";
*/
}
function voidLoadTags()
{
$arrTagsMatchesTemp = array();
$arrTagsMatches = array();
if (!empty($this->arrTemplates[$this->strTemplateName]['CONTENT']))
{
preg_match_all("/$this->strTagLimiter(\w+)$this->strTagLimiter/i",
$this->arrTemplates[$this->strTemplateName]['CONTENT'],
$arrTagsMatchesTemp);
if (!empty($arrTagsMatchesTemp))
{
foreach( $arrTagsMatchesTemp[1] as $TagName)
{
$arrTagsMatches[$TagName] = "";
}
$this->arrTemplates[$this->strTemplateName]['TAGS'] = $arrTagsMatches;
}
/*$this->arrTemplates[$this->strTemplateName]['TAGS']['TITLExxxx'] = 'mytitle';
echo "<PRE>\r\n";
print_r($this->arrTemplates[$this->strTemplateName]['TAGS']);
echo "</PRE>\r\n";*/
}
else
{
echo "Error. File: <B>" . $this->strTemplateName . "</B> is empty.\r\n";
exit();
}
unset($arrTagsMatchesTemp);
unset($arrTagsMatches);
}
function strGetContent($strFilePath)
{
return implode("",file($strFilePath));
}
function boolParseTemplate()
{
$strTagName = "";
unset($this->strParsedTemplate);
/*if ($this->arrTemplates[$this->strTemplateName]['PARSED'])
{
return true;
}*/
if (!empty($this->arrTemplates[$this->strTemplateName]['CONTENT']) &&
!empty($this->arrTemplates[$this->strTemplateName]['TAGS']))
{
$this->strParsedTemplate = $this->arrTemplates[$this->strTemplateName]['CONTENT'];
foreach ($this->arrTemplates[$this->strTemplateName]['TAGS'] as $TagName => $TagValue)
{
//$strTagName = $this->strTagLimiter . $TagName . $this->strTagLimiter;
$this->strParsedTemplate = str_replace($this->strTagLimiter . $TagName . $this->strTagLimiter,$TagValue,$this->strParsedTemplate);
/*echo "<PRE>\r\n";
print_r($this->arrTemplates[$this->strTemplateName]['CONTENT']);
echo "</PRE>\r\n";*/
//echo $this->strParsedTemplate;
}
$this->arrTemplates[$this->strTemplateName]['PARSED'] = true;
return true;
}
else
{
echo "Error. File: <B>" . $this->strTemplateName . "</B> is empty.\r\n";
exit(0);
}
return false;
}
function voidViewTemplate()
{
if ($this->arrTemplates[$this->strTemplateName]['PARSED'])
{
echo $this->strParsedTemplate;
}
}
}
?>
примеры использования:
Код:
$tempObj = new CTemplate();
$tempObj->voidLoadTemplate("eshe");
$tempObj->voidLoadTags();
$tempObj->arrTemplates['eshe']['TAGS']['TITLE'] = "My Title test";
$tempObj->arrTemplates['eshe']['TAGS']['CONTENT'] = "My Content test";
$tempObj->boolParseTemplate();
$tempObj->voidViewTemplate();
$tempObj->arrTemplates['eshe']['TAGS']['TITLE'] = "My Title test111";
$tempObj->arrTemplates['eshe']['TAGS']['CONTENT'] = "My Content test111";
$tempObj->boolParseTemplate();
$tempObj->voidViewTemplate();
сами шаблоны выглядят так:
start_template===>
<TITLE>
%TITLE%
</TITLE>
<DIV>
%CONTENT%
</DIV>
<====end_template
т.е. по сути это обычный HTML со спец. тегами.