السلام عليكم
هذا الموضوع حله عند الاخ daif.net او احد الاخوة الخبراء
احاول عمل نظام قوالب خاص في موقعي وسبق ومر علي موضوع او مثال بسيط قام بكتابته الاخ daif.net
الدرس التالي هو الذي احاول تطبيقه
http://daif.net/index.php?app=artical&id=9
فقمت بعمل الكلاس التالي
كود PHP:
<?php
class Template{
var $content= array();
var $vars;
var $tempname;
function GetTemplate($tempname)
{
$this->tempname = "$tempname";
$get_main_style=mysql_query("SELECT * FROM styles WHERE main='1'");
$row_style=mysql_fetch_array($get_main_style);
$get_temp=mysql_query("SELECT * FROM templates WHERE name='$tempname' AND styleid=".$row_style[id]." AND status=1") OR die(echodie());
$row_temp=mysql_fetch_array($get_temp);
$body = $row_temp['template'];
$body = stripslashes($body);
$this->content[$this->tempname] = stripslashes($body);
mysql_free_result($get_main_style);
mysql_free_result($get_temp);
unset($row_style);
unset($row_temp);
unset($body);
}
//Function to FixTemplate the Template Tags
function FixTemplate()
{
//start loop
$this->content[$this->tempname] = preg_replace("'<LOOP NAME=\"(.+)\">'i",
"<? foreach(\$this->vars[\\1] as \$key=>\$var){ ?>",$this->content[$this->tempname]);
//end loop
$this->content[$this->tempname] = preg_replace("'</LOOP>'i","<? } ?>",
$this->content[$this->tempname]);
//Start IF Statement
$this->content[$this->tempname] = preg_replace("'<IF NAME=\"(.+)\">'i",
"<? if(\$this->vars[\\1] == 1){ ?>",$this->content[$this->tempname]);
//end IF Statment
$this->content[$this->tempname] = preg_replace("'</IF>'i","<? } ?>",
$this->content[$this->tempname]);
//Include Statement
$this->content[$this->tempname] = preg_replace("'<INCLUDE FILENAME=\"(.+)\">'i",
"<? include(\"./\\1\"); ?>",$this->content[$this->tempname]);
//Foreach Variables
$this->content[$this->tempname] = preg_replace("'{{(.+)}}'","<? echo \$var[\\1] ?>",
$this->content[$this->tempname]);
//Variables
$this->content[$this->tempname] = preg_replace("'{(.+)}'","<? echo \$this->vars[\\1] ?>",
$this->content[$this->tempname]);
}
//Function to Display template
function DisplayTemplate()
{
$this->vars = &$GLOBALS;
$this->FixTemplate();
ob_start();
//eval('?'.'>'.trim($this->content[$this->tempname]).'<'.'?');
eval('?'.'>'.trim($this->content[$this->tempname]).'<'.'?');
$this->content[$this->tempname] = ob_get_contents();
ob_end_clean();
print $this->content[$this->tempname];
}
}
?>
الكلاس يعمل بشكل سليم ولاكن في حالة وضع اكثر من متغير في نفس القالب يظهر لي الخلل التالي
Parse error: syntax error, unexpected '}', expecting ']' in /classes/class_template.php
(77) : eval()'d code on line 1
وهذا نص القالب الذي احاول استبدال منه المتغيرات
كود PHP:
<div width="{site_width}" align="{site_align}">
<br />
{die_message}
<br />
</div>
يبدو ان المشكلة هو تجاهل المتغيرات بعد الاول مما يأدي في زيادة الاقواس { } في كود ال php عند عرضه ويحدث الخلل
يرجا حل لهذه المشكلة
ملاحظة يتم استبدال القالب بمتغير واحد ولاكن اذا كان هناك اكثر من متغير لا يعمل وبظهر الخلل السابق
شكرا لكم