הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   פורום תיכנות (https://hosts.co.il/forums/forumdisplay.php?f=14)
-   -   מחלקה של Templates (https://hosts.co.il/forums/showthread.php?t=27789)

Second 01-08-06 06:47

מחלקה של Templates
 
1 קבצים מצורפים
הי אנשים |קורץ| .
לפני כמה ימים עשיתי מחלקה של Templates ורציתי לשמוע על דעתכם.

PHP קוד:

<?
class Template
{
    var 
$folder;
    var 
$files = array();
    var 
$loops = array();

    function 
Template$tpl_folder "." )
    {
        if( 
is_file($tpl_folder) )
        {
            
$this->folder $tpl_folder;
        }
    }

    function 
loadFile$file_name$tpl_file )
    {
        
$file_path $this->folder $tpl_file;

        if( 
is_file($file_path) && !array_key_exists($file_name$this->files))
        {
            
$this->loops[$file_name] = array();

            
$handle    fopen$file_path"r" );
            
$temp    fread$handlefilesize($file_path) );
            
fclose($handle);

            
$patten "/\A([\s\S]*)<!-- BEGIN (.*?) -->([\s\S]*)<!-- END (.*?) -->([\s\S]*)\z/i";
            
$count preg_match($patten$temp);
            while( 
$count != )
            {
                
$loop_name preg_replace($patten"\\2"$temp1);
                
$this->loops[$file_name][$loop_name] = preg_replace($patten"\\3"$temp1);
                
$temp preg_replace($patten"\\1{loop.\\2}\\5"$temp1);
                
$count preg_match($patten$temp);
            }

            
$this->files[$file_name] = $temp;
        }
    }

    function 
do_loop$file_name$loop_name$info )
    {

        if( 
array_key_exists($file_name$this->files) && array_key_exists($loop_name$this->loops[$file_name]) && is_array($info) )
        {
            
$temp $this->loops[$file_name][$loop_name];
            foreach( 
$info as $key=>$val )
            {
                
$temp str_replace("{".$loop_name.".".$key."}"$val$temp);

            }

            
$this->files[$file_name] = preg_replace("/\A([\s\S]*){loop.$loop_name}([\s\S]*)\z/i""\\1".$temp."{loop.$loop_name}\\2"$this->files[$file_name]);
        }
    }

    function 
block_vars$file_name$info )
    {
        if( 
array_key_exists($file_name$this->files) && is_array($info) )
        {
            foreach( 
$info as $key=>$val )
            {
                
$this->files[$file_name] = str_replace("{".$key."}"$val$this->files[$file_name]);
            }
        }
    }

    function 
out$file_name )
    {
        if( 
array_key_exists($file_name$this->files) )
        {
            
$this->files[$file_name] = preg_replace("/\{\S+\}/i"""$this->files[$file_name]);
            echo 
$this->files[$file_name];
        }
    }
}
?>

תנו הצעות לשיפורים (:

BlueNosE 01-08-06 09:55

דוגמא לשימוש?

mayden 01-08-06 13:37

אשמח אם תפרט מה הקובץ הנ"ל עושה בדיוק,
תודה.

RS324 01-08-06 20:49

אתגר לשיפור :

תעשה שאני יוכל לעשות מצבים בקבצי HTML והוא ימיר אותם ויציג מה שאני רוצה לפי התנאי לדוגמא

<if option="$_SESSION[loggedin]">
welcome back
<else>
welcome guest
</if>

תאמין לי - אתה לא יודע איך זה יעזור לך.


כל הזמנים הם GMT +2. הזמן כעת הוא 12:46.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ