الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

محرك قوالب يقرأ ملفات الxml من برمجتي

بدأه f8athy في 14 مارس 2010 · 1 رد · 589 مشاهدة · في منتدى تطوير المواقع بـ PHP
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

بسم الله الرحمن الرحيم

السلام عليكم

ما رأيكم في نظام القوالب هذا

أولا سأشرح لكم هذا النظام وطريقة استخدامه

قمت ملف أسميته download.php الذي سأستخدم فيه نظام القوالب وبوضع مجلدين هما includes فيه الملف TPL_engine.class.php وهو محرك القوالب والمجلد الثاني هو themes فيه مجلد الستايل main والذي يحتوي على ثلاث مجلدات xml لملف الستايل و img لصور الستايل وال cache للملفات المؤقته

والآ سأشرح لكم محتوى الملف TPL_engine.class.php

   <?php
      include './includes/system/DB_engine.php'; //هذا فقط لطلب كلاس قاعدة البيانات
 class themes
 {
       var  $portal_name = "MY SMART POWER PORTAL";  // متغيير اسم المجلة
       var  $img_path    = "themes/main/img/";  // متغيير مسار صور الستايل
       var  $theme_dir   = "themes";   // مسار مجلد الستايلات
       var  $theme_name  = "Main";     // مجلد الستايل
       var  $theme_file  = "style_xml.xml";  // ملف الستايل
       var  $cache       = "cache";  // مجلد الملفات المؤقته
       var  $xml_file;   // متغير مسار ملف الستايل
       var  $data_xml_file;  // متغير محتوى ملف الستايل
       var  $title;  // عنوان الصفحة
       var  $def;   // متغير محتوى متغيير الاستبدال
       var  $exp;  // العبارة المستبدله

       function load_file($load_file)   // دالة فتح ملف
       {
           $file_open        = @fopen($load_file, "r") or die ("<P>لم يستطع فتح الملف!" .$load_file);
           $file_data        = fread($file_open, filesize("$load_file"));
           return $file_data;
       }

       function xml_reader()          // دالة قراءة ملف xml
       {

          $this->data_xml_file        =  $this->load_file($this->xml_file);
          $data_xml                   =  new SimpleXMLElement($this->data_xml_file);
          return $data_xml;
       }

       function def_var($var,$exp)     // دالة تحميل وحفظ متغير الاستبدال
       {
             $this->def = $var;
             $this->exp = $exp;
       }

       function _to_array($temp,$exp,$tables)   // دالة تحويل جدول قاعدة البيانات الى مصفوفة مع الاستبدال
       {

           if (is_array($exp))
           {
                $def         =  "<?";

                 foreach ( $exp as $key => $value )
                 {
                      $def         .=   '$tpl_'."$tables".'['."$key"."] = $value ;";
                 }

                $def           .=  " ?>";
           }

          $sql_data  = $def;
             $temp      = eregi_replace("{@sql@}",$sql_data,$temp);
             return $temp;
       }

       function _assigne($child)    // دالة الاستبدال
       {
             $name        = $this->portal_name;
             $title       = $name." - ".$this->title;
             $img         = $this->img_path;
             if (empty($this->def))
             {
                $def = '';
             }
             else
             {
                if (is_array($this->exp))
                {
                     $def         =  "<?";

                     foreach ( $this->exp as $key => $value )
                     {
                       $def         .=  " $this->def[$key] = $value ;";
                     }

                     $def           .=  " ?>";
                }
                else
                {
                     $def         = "<? $this->def = '$this->exp' ; ?>";
                }
             }

             $child = eregi_replace("{@title@}",$title,$child);
             $child = eregi_replace("{@x@}",$def,$child);
             $child = eregi_replace("{@img@}",$img,$child);
             $child = eregi_replace("{@portal_name@}",$name,$child);

             return $child;
       }

       function tpl_call($tpl_name)        //  دالة تحليل وتفكيك ملف ال xml إلى قوالب
       {
            $theme      = $this->xml_reader();
            $tpl_path   = $theme->xpath('/'.$this->theme_name.'/'.$tpl_name.'');

            while(list( , $tpl) = each($tpl_path))
            {
                  return $tpl;
            }
       }

       function tpl_cache($file_dir,$tpl_name,$cache_dir,$tables)   // دالة انشاء ملف القالب المؤقت
       {
           global $db;
           $tpl = $this->tpl_call($tpl_name);


           if (is_array($tables))
           {
                $db->connect_db();
                foreach ($tables as $k =>$v)
                {
                      $tpl_query   = $db->select($v,$k);
                    $tpl_fetch   = $db->fetch_db_array($tpl_query);
                }
           }
           $tpl = $this->_to_array($tpl,$tpl_fetch,$k);
           $tpl = $this->_assigne($tpl);
           if(!is_dir($cache_dir))
           {
              $mkdir          = @mkdir ($cache_dir, 0755, true);
              @fclose ($mkdir);
           }


           $writefile       = @fopen($file_dir,"w") or die ('لم يتم إنشاء ملف القالب');
           $addtofile       = @fwrite($writefile,$tpl) or die ('لم يتم ملء ملف القالب');
           @fclose ($writefile);
           @fclose ($addtofile);
       }

       function _in_page()   // دلة تحديد عنوان الصفحة
       {
                $self      = $_SERVER['PHP_SELF'];
             $self2     = explode('/',$self);
             $self_file = explode('.',$self2[2]);

                switch ($self_file[0])
             {
               case "index" :
                            $this->title = "الصفحة الرئيسية";
                            break;
               case "article":
                            $this->title = "المقالات";
                            break;
               case "article_det":
                            $this->title = "المقالات - تفاصيل ";
                            break;
               case "picture":
                            $this->title = "معرض الصور";
                            break;
               case "picture_det":
                            $this->title = "معرض الصور - تفاصيل";
                            break;
               case "download":
                            $this->title = "مركز التحميل";
                            break;
               case "download_det":
                             $this->title = "مركز التحميل - تفاصيل";
                             break;
             }
       }

       function page_title($tpl_name)    // دالة تحديد اسم القوالب المتماثلة بعنوان الصفحة
       {
          if ($tpl_name == "Head")
          {
            $this->_in_page();
            return $page_title = $tpl_name."_".$this->title;
          }
          else
          {
            return $tpl_name;
          }
       }

       function display($tpl_name,$tables="")   // دالة إظار وطلب القالب
       {

          $this->xml_file         = $this->theme_dir."/".$this->theme_name."/xml/".$this->theme_file;
          $tpl_name_mod           = $this->page_title($tpl_name);
          $file_dir               = $this->theme_dir."/".$this->theme_name."/".$this->cache."/".$tpl_name_mod .".ch";
          $cache_dir              = $this->theme_dir."/".$this->theme_name."/".$this->cache;
          $readfile               = @fopen($file_dir,"r");

          if (!$readfile or @filemtime($this->data_xml_file)>@filemtime($readfile))
          {
             $this->tpl_cache($file_dir,$tpl_name,$cache_dir,$tables);

          }


          @fclose ($readfile);
          include($file_dir);  // template file
          $this->def_var('','');
       }


 }
?>

طريقة الاستعمال

   <?php
      include './includes/TPL_engine.class.php';  // طلب الكلاس


      $themes = new themes;

      $tables      = array('seting' => array('idseting','portal_name')); //  تعريف جداول وحقول المستخدمة في القالب

      $themes->display("Head"); // إظهار قالب الهيد

      $themes->display("Header",$tables); // إظهار قالب الهيدر مع تمكين قاعدة البيانات

      $x = "hello";  // تعريف المتغير X

      $themes->def_var('$y',$x);  // حفظ قيمة المتغير X ليتم استبدالها مكان y
      $themes->display("Ads"); // إظهار القالب ads


?>

الملف مرفق

ارجو الرد وتعليقكم على هذا الكلاس

xml_styles.zip

564x.jpg
#2

غريب وينكم لا ردود لا تعليق

564x.jpg

مواضيع مشابهة