PHPµÄ×Ô¶¨ÒåÄ£°åÒýÇæ

5年以前  |  阅读数:677 次  |  编程语言:PHP 

Ç°ÃaeµÄ»°

ÔÚ´o¶aÊýµÄÏiÄ¿×eÖУ¬¿ª¢Ò»¸oWeb³ÌÐo¶¼»a³oÏÖÕaÑuµÄÁ÷³Ì£º¼Æ»®ÎĵµÌa½»Ö®ºo£¬Ç°¶Ë¹¤³ÌʦÖÆ×÷ÁËÍøÕ¾µÄÍa¹ÛÄ£ÐÍ£¬È»ºo°ÑËu½»¸øºo¶Ë¹¤³Ìʦ£¬ËuÃÇʹÓúo¶Ë´uÂeʵÏÖ³ÌÐoÂß¼­£¬Í¬Ê±Ê¹ÓÃÍa¹ÛÄ£ÐÍ×o³É»u±¾¼Ü¹¹£¬È»ºo¹¤³Ì±»µ»Øµ½Ç°¶Ë¹¤³Ìʦ¼ÌÐøÍeÉÆ¡£¾ÍÕaÑu¹¤³Ì¿ÉÄÜÔÚºo¶Ë¹¤³ÌʦºÍÇ°¶Ë¹¤³Ìʦ֮¼aÀ´À´»Ø»ØºÃ¼¸´Î¡£ÓÉÓÚºo¶Ë¹¤³Ìʦ²»¸ÉÔ¤ÈκÎÏa¹ØHTML±eÇ(C)£¬Í¬Ê±Ò²²»ÐeҪǰ¶Ë´uÂeºÍºo¶Ë´uÂe»iºÏÔÚÒ»Æð¡£Ç°¶Ë¹¤³ÌʦֻÐeÒªÅaÖÃÎļþ£¬¶¯Ì¬Çø¿eºÍÆaËuµÄ½çÃae²¿*Ö£¬²»±ØҪȥ½Ó´¥ÄÇÐ(C)´i×Û¸´Ôӵĺo¶Ë´uÂe¡£Òo´Ë£¬ÕaʱºoÓÐÒ»¸oºÜºÃµÄÄ£°aÖ§³Ö¾ÍÏԵúÜÖØÒªÁË¡£±¾ÎĽ«Ïeϸ½eÉÜPHPÖеÄÄ£°aÒýÇae

¸ÅÊo

ʲôÊÇÍøվģ°a£¿×¼ÈµØ˵£¬ÊÇÖ¸ÍøÕ¾Ò³ÃaeÄ£°a£¬¼´Ã¿¸oÒ³Ãae½oÊÇÒ»¸o°aʽ£¬°uÀ¨½a¹¹¡¢ÑuʽºÍÒ³Ãae²¼¾Ö£¬ÊÇ´´½¨ÍøÒ³ÄÚÈݵÄÑu°a£¬Ò²¿ÉÒÔÀi½aΪÒÑÓеÄÍøÒ³¿o¼Ü¡£¿ÉÒÔ½«Ä£°aÖÐÔ­ÓеÄÄÚÈÝÌae»»³É´ÓþÎñÆ÷¶ËÊý¾Ý¿aÖж¯Ì¬ÄÚÈÝ£¬Ä¿µÄÊÇ¿ÉÒÔ±£³ÖÒ³Ãae*ç¸ñÒ»ÖÂ

PHPÊÇÒ»ÖÖHTMLÄÚǶʽµÄÔÚþÎñÆ÷¶ËÖ´ÐеĽű¾ÓiÑÔ£¬ËuÒÔ´o²¿ÖPHP¿ª¢³oÀ´µÄWebÓ¦Ó㬳oʼµÄ¿ª¢Ä£°a¾ÍÊÇ»iºÏ²aµÄÊý¾Ý±a³Ì¡£ËaȻͨ¹ýMVCÉe¼Æģʽ¿ÉÒ԰ѳÌÐoÓ¦ÓÃÂß¼­ÓeÍøÒ³³ÊÏÖÂß¼­Ç¿ÖÆÐÔÖÀe£¬µ«Ò²Ö»Êǽ«Ó¦ÓóÌÐoµÄÊaÈe¡¢´¦ÀiºÍÊa³oÖ¿ª£¬ÍøÒ³³ÊÏÖÂß¼­(ÊÓͼ)»¹»aÓÐHTML´uÂeºÍPHP³ÌÐoÇ¿ñiºÏÔÚÒ»Æð¡£PHP½Å±¾µÄ±aдÕß±ØÐe¼ÈÊÇÍøÒ³Ée¼ÆÕߣ¬ÓÖÊÇPHP¿ª*¢Õß

ÏÖÔÚÒѾ­Óкܶa½a¾o½°¸£¬¿ÉÒÔ½«ÍøÕ¾µÄÒ³ÃaeÉe¼ÆºÍPHPÓ¦ÓóÌÐo¼¸ºoÍeÈ«ÖÀe¡£ÕaÐ(C)½a¾o½°¸³ÆΪ¡°Ä£°aÒýÇae¡±£¬ËuÃÇÕýÔÚÖð²½Ïu³ýÓÉÓÚȱ¦²a´ÎÖÀe¶ø´øÀ´µÄÄÑÌa¡£Ä£°aÒýÇaeµÄÄ¿µÄ£¬¾ÍÊÇÒª´iµ½ÉÏÊoÌaµ½µÄÂß¼­ÖÀeµÄ¹¦ÄÜ¡£ËuÄÜÈóÌÐo¿ª¢ÕßרעÓÚ×ÊÁϵĿØÖÆ»oÊǹ¦ÄܵĴi³É¡£Òo´Ë£¬Ä£°aÒýÇaeºÜÊʺϹ«Ë¾µÄWeb¿ª¢ÍŶÓʹÓã¬Ê¹Ã¿¸oÈ˶¼ÄÜ*¢»ÓÆaר³¤

Ä£°aÒýÇae¼¼ÊoµÄºËÐıȽϼoµ¥¡£Ö»Òª½«Ç°¶ËÒ³ÃaeÖ¸¶¨ÎªÄ£°aÎļþ£¬²¢½«Õa¸oÄ£°aÎļþÖж¯Ì¬µÄÄÚÈÝ£¬ÈçÊý¾Ý¿aÊa³o¡¢Óû§½»»¥µÈ²¿Ö£¬¶¨Òa³ÉʹÓÃÌØÊa¡°¶¨½çu¡±°uº¬µÄ¡°±aÁ¿¡±£¬È»ºo*ÅÔÚÄ£°aÎļþÖÐÏaÓ¦µÄλÖᣵ±Óû§a¯ÀÀʱ£¬ÓÉPHP½Å±¾³ÌÐo´o¿ª¸ÃÄ£°aÎļþ£¬²¢½«Ä£°aÎļþÖж¨ÒaµÄ±aÁ¿½øÐÐÌae»»¡£ÕaÑu£¬Ä£°aÖеÄÌØÊa±aÁ¿±»Ìae»»Îª²»Í¬µÄ¶¯Ì¬ÄÚÈÝʱ£¬¾Í»aÊa³oÐeÒªµÄÒ³Ãae

Ä¿Ç°£¬¿ÉÒÔÔÚPHPÖÐÓ¦ÓõIJ¢ÇұȽϳÉÊiµÄÄ£°aÓкܶa£¬ÀýÈçSmarty¡¢PHPLIB¡¢IPBµÈ¼¸Ê®ÖÖ¡£Ê¹ÓÃÕaÐ(C)ͨ¹ýPHP±aдµÄÄ£°aÒýÇae£¬¿ÉÒÔÈôuÂeÂoÂç¸u¼ÓÇaÎu£¬½a¹¹¸u¼ÓºÏÀi»¯¡£Ò²¿ÉÒÔÈÃÍøÕ¾µÄά»¤ºÍ¸uбaµÃ¸uÈÝÒ×£¬´´ÔiÒ»¸o¸u¼ÓÁ¼ºÃµÄ¿ª¢»¾³£¬Èÿª¢ºÍÉe¼Æ¹¤×÷¸uÈÝÒ×½aºÏÔÚÒ»Æð¡£µ«ÊÇ£¬Ã»ÓÐÄÄÒ»¸oPHPÄ£°aÊÇ×iºÏÊÊ¡¢×iÍeÃÀµÄ¡£ÒoΪPHPÄ£°a¾ÍÊÇ´oÖÚ»¯µÄ¶«Î÷£¬²¢²»ÊÇÕe¶Ôij¸oÈË¿ª¢µÄ¡£Èç¹uÄÜÔÚ¶ÔÄ£°aµÄÌصa¡¢Ó¦ÓÃÓÐÇa³þµÄÈÏʶ»u´¡ÉÏ£¬³aÖÈÏʶµ½Ä£°aµÄÓÅÊÆÁÓÊÆ£¬¾Í¿ÉÒÔÖªµÀÊÇñÑ¡ÔñʹÓÃÄ£°aÒýÇae»oÑ¡ÔñʹÓÃÄĸoÄ£°aÒýÇae

×Ô¶¨ÒaÄ£°aÒýÇaeÀa

×Ô¶¨ÒaÄ£°aÒýÇae£¬Äܹ»¸uºÃµÄÕÆÎÕÄ£°aÒýÇaeµÄ¹¤×÷»uÖÆ£¬ÎªÑ§Ï°Smarty×oºÃ×¼±¸¡£¸uÖØÒªµÄÊÇ£¬ÊoÓÚ×Ô¼ºµÄPHPÄ£°aÒýÇaeÓÀÔ¶²»Êǹ̶¨²»±aµÄ£¬¿ÉÒÔ¸u¾ÝÏiÄ¿µÄÐeҪΪÆaÁ¿Éi¶¨ÖÆ

ÔÚÏÂÀýÖУ¬Í¨¹ýÇ°Ãae½eÉܵÄÄ£°aÒýÇae¸ÅÄi´´½¨ÁËÊoÓÚ×Ô¼ºµÄÒ»¸o¼oµ¥Ä£°aÒýÇae£¬¿ÉÒÔÓÃÀ´´¦ÀiÄ£°aµÄ»u±¾¹¦ÄÜ¡£ÀýÈ磺±aÁ¿Ìae»»¡¢ÖÖ§½a¹¹¡¢Êý×eÑ­»±eÀu£¬ÒÔ¼°Ä£°aÖ®¼aÏa»¥Ç¶Ì׵ȣ¬ÈçÏÂËuʾ£º


    <?php
     /**
      file: mytpl.class.php ÀaÃuΪMyTplÊÇ×Ô¶¨ÒaµÄÄ£°aÒýÇae
      ͨ¹ý¸ÃÀa¶ÔÏo¼ÓÔØÄ£°aÎļþ²¢½aÎo£¬½«½aÎoºoµÄ½a¹uÊa³o 
     */
     class Mytpl {
      public $template_dir = 'templates';  //¶¨ÒaÄ£°aÎļþ´ae*ŵÄĿ¼ 
      public $compile_dir = 'templates_c';  //¶¨Òaͨ¹ýÄ£°aÒýÇae×eºÏºoÎļþ´ae*ÅĿ¼
      public $left_delimiter = '<{';   //ÔÚÄ£°aÖÐǶÈe¶¯Ì¬Êý¾Ý±aÁ¿µÄ×o¶¨½ç*uºÅ
      public $right_delimiter = '}>';   //ÔÚÄ£°aÖÐǶÈe¶¯Ì¬Êý¾Ý±aÁ¿µÄÓÒ¶¨½ç*uºÅ
      private $tpl_vars = array();    //ÄÚ²¿Ê¹ÓõÄÁÙʱ±aÁ¿
      /** 
       ½«PHPÖÐ*ÖÅaµÄÖµ»a±£´aeµ½³ÉÔ±ÊoÐÔ$tpl_varsÖУ¬ÓÃÓÚ½«Ä£°aÖжÔÓ¦µÄ±aÁ¿½øÐÐÌae»» 
       @param string $tpl_var ÐeÒªÒ»¸o×Ö*u´®²ÎÊý×÷Ϊ¹ØÁªÊý×eϱe£¬ÒªºÍÄ£°aÖеıaÁ¿Ãu¶ÔÓ¦ 
       @param mixed $value  ÐeÒªÒ»¸o±eÁ¿ÀaÐ͵ÄÖµ£¬ÓÃÀ´*ÖÅa¸øÄ£°aÖбaÁ¿µÄÖµ  
      */
      function assign($tpl_var, $value = null) { 
       if ($tpl_var != '')     
        $this->tpl_vars[$tpl_var] = $value;
      }
      /** 
       ¼ÓÔØÖ¸¶¨Ä¿Â¼ÏµÄÄ£°aÎļþ£¬²¢½«Ìae»»ºoµÄÄÚÈÝÉu³É×eºÏÎļþ´ae*ŵ½ÁiÒ»¸oÖ¸¶¨Ä¿Â¼ÏÂ
       @param string $fileName Ìa¹(C)Ä£°aÎļþµÄÎļþÃu            
      */
       function display($fileName) { 
       /* µ½Ö¸¶¨µÄĿ¼ÖÐÑ°ÕÒÄ£°aÎļþ */
       $tplFile = $this->template_dir.'/'.$fileName; 
       /* Èç¹uÐeÒª´¦ÀiµÄÄ£°aÎļþ²»´aeÔÚ,ÔoÍ˳o²¢±¨¸ae´iÎo */
       if(!file_exists($tplFile)) {     
        die("Ä£°aÎļþ{$tplFile}²»´aeÔÚ£¡");
       }
       /* »ñÈ¡×eºÏµÄÄ£°aÎļþ£¬¸ÃÎļþÖеÄÄÚÈݶ¼ÊDZ»Ìae»»¹ýµÄ */
       $comFileName = $this->compile_dir."/com_".$fileName.'.php'; 
       /* ÅжÏÌae»»ºoµÄÎļþÊÇ*ñ´aeÔÚ»oÊÇ´aeÔÚµ«ÓиĶ¯£¬¶¼ÐeÒªÖØд´½¨ */
       if(!file_exists($comFileName) || filemtime($comFileName) < filemtime($tplFile)) {
        /* µ÷ÓÃÄÚ²¿Ìae»»Ä£°a*½*¨ */
        $repContent = $this->tpl_replace(file_get_contents($tplFile)); 
        /* ±£´aeÓÉϵͳ×eºÏºoµÄ½Å±¾Îļþ */
        file_put_contents($comFileName, $repContent);
       }
       /* °uº¬´¦ÀiºoµÄÄ£°aÎļþÊa³o¸ø¿Í»§¶Ë */
       include($comFileName);     
      }
      /** 
       ÄÚ²¿Ê¹ÓõÄ˽ÓÐ*½*¨£¬Ê¹ÓÃÕýÔo±i´iʽ½«Ä£°aÎļþ'<{ }>'ÖеÄÓi¾aÌae»»Îª¶ÔÓ¦µÄÖµ»oPHP´uÂe 
       @param string $content Ìa¹(C)´ÓÄ£°aÎļþÖжÁÈeµÄÈ«²¿ÄÚÈÝ×Ö*u´® 
       @return $repContent   *µ»ØÌae»»ºoµÄ×Ö*u´®
      */
      private function tpl_replace($content) {
       /* ½«×oÓÒ¶¨½ç*uºÅÖУ¬ÓÐÓ°ÏiÕýÔoµÄÌØÊa*uºÅתÒa ÀýÈ磬<{ }>תÒa\<\{ \}\> */
       $left = preg_quote($this->left_delimiter, '/');
       $right = preg_quote($this->right_delimiter, '/');
       /* Æ¥ÅaÄ£°aÖи÷ÖÖ±eʶ*uµÄÕýÔo±i´iʽµÄģʽÊý×e */
       $pattern = array(  
        /* Æ¥ÅaÄ£°aÖбaÁ¿ ,ÀýÈ磬"<{ $var }>" */
        '/'.$left.'\s*\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*'.$right.'/i',  
        /* Æ¥ÅaÄ£°aÖÐif±eʶ*u£¬ÀýÈç "<{ if $col == "sex" }> <{ /if }>" */
        '/'.$left.'\s*if\s*(.+?)\s*'.$right.'(.+?)'.$left.'\s*\/if\s*'.$right.'/ies',
        /* Æ¥Åaelseif±eʶ*u, ÀýÈç "<{ elseif $col == "sex" }>" */
        '/'.$left.'\s*else\s*if\s*(.+?)\s*'.$right.'/ies', 
        /* Æ¥Åaelse±eʶ*u, ÀýÈç "<{ else }>" */
        '/'.$left.'\s*else\s*'.$right.'/is', 
        /* ÓÃÀ´Æ¥ÅaÄ£°aÖеÄloop±eʶ*u£¬ÓÃÀ´±eÀuÊý×eÖеÄÖµ, ÀýÈç "<{ loop $arrs $value }> <{ /loop}>" */
        '/'.$left.'\s*loop\s+\$(\S+)\s+\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*'.$right.'(.+?)'.$left.'\s*\/loop\s*'.$right.'/is',
        /* ÓÃÀ´±eÀuÊý×eÖеļuºÍÖµ,ÀýÈç "<{ loop $arrs $key => $value }> <{ /loop}>" */
        '/'.$left.'\s*loop\s+\$(\S+)\s+\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*=>\s*\$(\S+)\s*'.$right.'(.+?)'.$left.'\s*\/loop \s*'.$right.'/is', 
        /* Æ¥Åainclude±eʶ*u, ÀýÈ磬'<{ include "header.html" }>' */
        '/'.$left.'\s*include\s+[\"\']?(.+?)[\"\']?\s*'.$right.'/ie'     
       );
       /* Ìae»»´ÓÄ£°aÖÐʹÓÃÕýÔo±i´iʽƥÅaµ½µÄ×Ö*u´®Êý×e */
       $replacement = array( 
        /* Ìae»»Ä£°aÖеıaÁ¿ <?php echo $this->tpl_vars["var"]; */
        '<?php echo $this->tpl_vars["${1}"]; ?>',  
        /* Ìae»»Ä£°aÖеÄif×Ö*u´® <?php if($col == "sex") { ?> <?php } ?> */
        '$this->stripvtags(\'<?php if(${1}) { ?>\',\'${2}<?php } ?>\')',  
        /* Ìae»»elseifµÄ×Ö*u´® <?php } elseif($col == "sex") { ?> */
        '$this->stripvtags(\'<?php } elseif(${1}) { ?>\',"")', 
        /* Ìae»»elseµÄ×Ö*u´® <?php } else { ?> */
        '<?php } else { ?>', 
        /* ÒÔÏÂÁ½ÌoÓÃÀ´Ìae»»Ä£°aÖеÄloop±eʶ*uΪforeach¸ñʽ */
        '<?php foreach($this->tpl_vars["${1}"] as $this->tpl_vars["${2}"]) { ?>${3}<?php } ?>', 
        '<?php foreach($this->tpl_vars["${1}"] as $this->tpl_vars["${2}"] => $this->tpl_vars["${3}"]) { ?>${4}<?php } ?>', 
        /*Ìae»»includeµÄ×Ö*u´®*/
        'file_get_contents($this->template_dir."/${1}")'    
       );
       /* ʹÓÃÕýÔoÌae»»º¯Êý´¦Ài */
       $repContent = preg_replace($pattern, $replacement, $content);  
       /* Èç¹u»¹ÓÐÒªÌae»»µÄ±eʶ,µÝ¹eµ÷ÓÃ×Ô¼ºÔÙ´ÎÌae»» */
       if(preg_match('/'.$left.'([^('.$right.')]{1,})'.$right.'/', $repContent)) {  
        $repContent = $this->tpl_replace($repContent);    
       } 
       /* *µ»ØÌae»»ºoµÄ×Ö*u´® */
       return $repContent;          
      }
       /**
       ÄÚ²¿Ê¹ÓõÄ˽ÓÐ*½*¨£¬ÓÃÀ´½«Ìo¼þÓi¾aÖÐʹÓõıaÁ¿Ìae»»Îª¶ÔÓ¦µÄÖµ
       @param string $expr  Ìa¹(C)Ä£°aÖÐÌo¼þÓi¾aµÄ¿ªÊ¼±e¼Ç   
       @param string $statement Ìa¹(C)Ä£°aÖÐÌo¼þÓi¾aµÄ½aÊø±e¼Ç 
       @return strin    ½«´¦ÀiºoµÄÌo¼þÓi¾aÏaÁ¬ºo*µ»Ø 
      */
      private function stripvtags($expr, $statement='') {
       /* Æ¥Åa±aÁ¿µÄÕýÔo */
       $var_pattern = '/\s*\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*/is'; 
       /* ½«±aÁ¿Ìae»»ÎªÖµ */
       $expr = preg_replace($var_pattern, '$this->tpl_vars["${1}"]', $expr); 
       /* ½«¿ªÊ¼±e¼ÇÖеÄÒýºÅתÒaÌae»» */
       $expr = str_replace("\\\"", "\"", $expr);
       /* Ìae»»Ói¾aÌaºÍ½aÊø±e¼ÇÖеÄÒýºÅ */
       $statement = str_replace("\\\"", "\"", $statement); 
       /* ½«´¦ÀiºoµÄÌo¼þÓi¾aÏaÁ¬ºo*µ»Ø */
       return $expr.$statement;        
      }
     }
    ?>

ÔÚMytplÀaÖÐÉuÃ÷µÄ¶a¸o½¨ÖУ¬³ý±»a×°¹ýµÄ½¨Ö®Ía£¬Ö»ÓÐÁ½¸o¹«Óн¨assign()ºÍdisplay()ÔÚ´´½¨¶ÔÏoÒÔºo¿ÉÒÔ±»µoÓá£ÆaÖÐassign()½¨ÓÃÀ´½«PHP½Å±¾ÖеÄÊý¾ÝÖÅa¸øÄ£°aÖжÔÓ¦µÄ±aÁ¿£¬display()½¨ÔoÓÃÀ´½«Ìض¨µÄtemplatesĿ¼ÏµÄÄ£°aÎļþ¼ÓÔص½PHP½Å±¾ÖС£Í¬Ê±½«Ä£°aÎļþÖÐʹÓá°<{¡±ºÍ¡°>>¡±±e¼ÇÉuÃ÷µÄ×Ô¶¨ÒaÄ£°aÓi¾a£¬Æ¥Åa³oÀ´²¢Ìae»»³ÉÏa¶ÔÓ¦µÄPHPÓi¨¸ñʽ£¬È»ºo½«Ìae»»ºoµÄÄÚÈݱ£´aeÔÚÌض¨µÄtemplates_cĿ¼Ï¡£ÔÚÔËÐÐʱ»¹Òª±aÒe³ÉÒ»¸oÇÄ£°a¼¼ÊoµÄPHPÎļþ£¬²¢½«ÆaÒÔÄ£°aÎļþÃu¼ÓÉÏ¡°com_¡±Ç°×ººÍ¡°.php¡±µÄÀ(C)Õ¹ÃuÐÎʽ±£´ae¡£ÔÙͨ¹ýinclude()º¯Êý½«´¦ÀiºoµÄÄ£°aÎļþ°uº¬£¬²¢Ê¹ÓÃPHP½aÎoºo*¢Ë͸ø¿Í»§¶Ë

ʹÓÃÄ£°aÒýÇae

ʹÓÃ×Ô¶¨ÒaµÄÄ£°aÒýÇae±È½ÏÈÝÒ×£¬¶¼ÊÇ×Ô¼º¶¨ÒaµÄÓi¨¸ñʽ¡£µ«Òª¼Çס£¬ËuÓÐÁ÷ÐеÄÄ£°aÒý±½a¾o½°¸¶¼×ñѭͬÑuµÄÒ»×eÏaͬµÄºËÐÄʵÏÖÔ­Ôo£¬¾ÍÊÇÓe±a³ÌÓiÑÔÒ»Ñu£¬Ñ§Ï°ÁËÒ»ÖÖÓiÑԾͿÉÒÔ¸uÈÝÒ×µØÕÆÎÕÆaËuÓiÑÔ¡£Ê¹ÓÃÄ£°aÒýÇae×iÖ÷ÒªµÄÔ­Òo¾ÍÊǽ«Ç°¶Ë¹¤³ÌʦºÍºo¶Ë¹¤³ÌʦµÄ¹¤×÷Ö¿ª£¬ËuÒÔÄ£°aÒýÇae²»½oºo¶Ë¹¤³ÌʦÐeҪʹÓã¬Ç°¶Ë¹¤³ÌʦҲÐeҪʹÓÃ

1¡¢ºo¶Ë¹¤³Ìʦ¶ÔÄ£°aÒýÇaeµÄʹÓÃ

ÔÚPHP½Å±¾Öаuº¬Ä£°aÒýÇaeÀaËuÔÚµÄÎļþ¡£ÈçÏÂËuʾ£º

require("mytpl.class.php"); //°uº¬Ä£°aÒýÇaeÀa£¬Ïaµ±ÓÚÄ£°aÒýÇae°²×°

´´½¨Ä£°aÒýÇaeÀaµÄ¶ÔÏo²¢¶ÔÒ»Ð(C)³ÉÔ±ÊoÐÔ½øÐгoʼ»¯¸³Öµ¡£ÈçÏÂËuʾ£º

$tpl=new MyTpl(); //´´½¨Ä£°aÒýÇaeÀaµÄ¶ÔÏo£¬Ò²¿ÉÒÔ¸u¾Ý²ÎÊý¶Ô³ÉÔ±³oʼ»¯

½«¶¯Ì¬Êý¾Ý(°uÀ¨±eÁ¿ºÍÊý×eÀaÐ͵ÄÊý¾Ý£¬ÀýÈç´ÓÊý¾Ý¿aµÄ±iÖлñµÃµÄÊý¾ÝÊý×e)ʹÓÃÄ£°aÒýÇae¶ÔÏoÖеÄassign()½¨ÖÅa¸øÄ£°aÎļþ£¬Õa¸o½¨¿ÉÒÔʹÓöa´Î£¬½«ÈÎÒa¶a¸o±aÁ¿ÖÅa¸øÄ£°a¡£ÈçÏÂËuʾ£º


    $tpl->assign("var","this is a value"); //¿ÉÒÔ*ÖÅa±eÁ¿ÀaÐÍÊý¾Ý£¬¿ÉÒÔʹÓöa´Î
    $tpl->assign("arr",array(array(1,2),array("a","b"))); //Ò²¿ÉÒÔ*ÖÅaÊý×e°uÀ¨¶aάÊý×e

ÔÚPHP½Å±¾ÖÐͨ¹ýµ÷ÓÃÄ£°a¶ÔÏoÖеÄdisplay()½¨£¬²¢½«Ä£°aÎļþÃu×÷Ϊ²ÎÊý´«Èe£¬¾Í»a¼ÓÔØÖ¸¶¨Ä¿Â¼ÖжÔÓ¦µÄÄ£°aÎļþµ½PHP½Å±¾ÖС£ÔÙͨ¹ýÄ£°aÒýÇaeÖеÄÌae»»½¨¶ÔÄ£°aÖÐ×Ô¶¨ÒaµÄÓi*¨½øÐнaÎo£¬È»ºoÊa³o´¦ÀiºoµÄÄ£°a¡£ÈçÏÂËuʾ£º

$tpl->display("test.tpl"); //²ÎÊý¡°test.tpl¡±ÎªÌض¨Ä¿Â¼ÏµÄÄ£°aÎļþ

2¡¢Ç°¶Ë¹¤³Ìʦ¶ÔÄ£°aÒýÇaeµÄʹÓÃ

Ç°¶Ë¹¤³ÌʦÐeÒª½«±aдµÄÄ£°aÎļþ´ae*ŵ½Ö¸¶¨µÄĿ¼ÖУ¬Õa¸oĿ¼ÊÇͨ¹ýÔÚÄ£°a¶ÔÏoÖÐʹÓÃ$template_dirÊoÐÔÖ¸¶¨µÄ£¬Ä¬ÈϵÄÉeÖÃÊǵ±Ç°Ä¿Â¼Ïµġ°templates¡±Ä¿Â¼¡£ÁiÍa£¬Ä£°aÎļþµÄÃuÃuÒÔ¼°ºo׺ÃuµÄÉeÖÿÉÒÔËaeÒa£¬ÀýÈçindex.tpl¡¢test.htm¡¢header.tp;µÈ

Ä£°aÎļþÊÇͨ¹ýʹÓÃHTML¡¢CSSÒÔ¼°javascriptµÈWebǰ̨ÓiÑÔÒÔ±aдµÄ´¿¾²Ì¬¸º¶ø¡£µ«¿ÉÒÔÔÚÄ£°aÎļþÖÐʹÓá°<{¡±ºÍ¡°}>¡±Á½¸oÖ¸ouÖмa¶¨ÒaÒ»¸o±aÁ¿(ÀaËÆPHPÖеıaÁ¿¸ñʽ)£¬¸Ã±aÁ¿¿ÉÒÔ½ÓÊܲ¢Êa³oÓÉPHP½Å±¾ÖÐÖÅa¹ýÀ´µÄ¶¯Ì¬Êý¾Ý¡£ÔÚÄ£°aÖÐʹÓõġ°<{¡±ºÍ¡°}>¡±Á½¸oÖ¸o*u¶Ô£¬Ò²¿ÉÒÔ¸u¾Ý¸oÈË°®ºÃÔÚÄ£°aÒýÇaeÀaÖÐÐ޸ġ£ÈçÏÂËuʾ£º

ÐÕÃu£º<{$name}>£¬ÄeÁa£º<{$age}>,ÐÔ±ð:<{$sex}> //Ä£°aÖÐʹÓÃռλ*u

Èç¹uÔÚPHP½Å±¾ÖÐÊǽ«Êý×eÖÅa¸øÄ£°a£¬Ò²¿ÉÒÔÔÚÄ£°aÖнøÐбeÀu£¬»¹¿ÉÒÔͨ¹ýǶÌ׵Ľʽ±eÀu¶aάÊý×e¡£Ê¹ÓõÄÊÇÔÚÄ£°aÒýÇaeÖж¨ÒaµÄ¡°<{loop}>¡±±e¼Ç¶Ô£¬Ê¹ÓõĽʽºÍPHPÖÐforeach½a¹¹µÄÓi¨¸ñʽÏaËÆ¡£ÈçÏÂËuʾ£º


    <{loop $arr $value }>     //±eÀuÊý×e$arrÖеÄÔªËØÖµ
     Êý×eÖеÄÔªËØÖµ<{$value}>   //ÿ´Î±eÀuÊa³oÔªËØÖеÄÖµ
    <{/loop}>        //ÔÚÄ£°aÖбeÀuÊý×eµÄ½aÊø±e¼Ç
    <{loop $arr $key=>$value }>   //±eÀuÊý×e$arrÖеÄÔªËØϱeºÍÔªËØÖµ
     Êý×eÖеÄÔªËؼu<{$key}>    //ÿ´Î±eÀuÊa³oÔªËØÖеÄϱe
     Êý×eÖеÄÔªËØÖµ<{$value}>   //ÿ´Î±eÀuÊa³oÔªËØÖеÄÖµ
    <{/loop}>        //ÔÚÄ£°aÖбeÀuÊý×eµÄ½aÊø±e¼Ç
    <{loop $arr $value }>     //±eÀuÊý×e$arrÖеÄÔªËØÖµ
     <{loop $arr $data }>    //ʹÓÃǶÌ×±e¼Ç±eÀu¶þάÊý×e
      Êý×eÖеÄÔªËØÖµ<{$value}>  //ÿ´Î±eÀuÊa³oÔªËØÖеÄÖµ
     <{/loop}>       //ÔÚÄ£°aÖбeÀuÊý×eµÄÄÚ²a½aÊø±e¼Ç
    <{/loop}>        //ÔÚÄ£°aÖбeÀuÊý×eµÄÍa²a½aÊø±e¼Ç

Ä£°aÒýÇae»¹¿ÉÒÔ½aÎoÔÚÄ£°aÎļþÖÐʹÓÃÌØÊa±e¼Ç±aдµÄÖÖ§½a¹¹£¬Ói¨ç¸ñÒ²ÊǺÍPHPµÄÖÖ§½a¹¹ÀaËÆ¡£ÊÇͨ¹ýÔÚÄ£°aÎļþÖÐʹÓá°<{if}>¡±±e¼Ç¶ÔʵÏÖÑ¡Ôñ½a¹¹£¬Ò²¿ÉÒÔʵÏÖ¶aÂ*ÖÖ§ºÍǶÌ×ÖÖ§µÄÑ¡Ôñ½a¹¹¡£ÈçÏÂËuʾ£º 


    <{if($var=="red")}>
     <p style="color:red">ÕaÊÇ¡°ºiÉ«¡±µÄ×Ö</p>
    <{elseif($var=="green")}> 
     <p style="color:green">ÕaÊÇ¡°ÂÌÉ«¡±µÄ×Ö</p>
    <{else}>
     <{if($size=20)}>
      <p style="font-size:20">ÕaÊÇ¡°20px¡±´oСµÄ×Ö</p>
     <{/if}>
    <{/if}>

ÔÚ×Ô¶¨ÒaµÄÄ£°aÒýÇaeÖУ¬Ò²Ìi¼ÓÁËÔÚÄ£°aÎļþÖаuº¬ÆaËuÄ£°aÎļþµÄ¹¦ÄÜ¡£¿ÉÒÔʹÓá°<{include¡®×ÓÄ£°aÎļþÃu'}>¡±±e¼Ç½«×ÓÄ£°a°uº¬µ½µ±Ç°Ä£°aÖУ¬»¹Ö§³ÖÔÚ×ÓÄ£°aÖÐÔٴΰuÀ¨ÁiÍaµÄ×ÓÄ£°a¡£ÈçÏÂËuʾ£º

<{include 'other.tpl' }>

*ʹÓÃʾÀýÖÎo**

ͨ¹ýÔÚ³ÌÐoÖмÓÔØÄ£°aÒýÇae¿ÉÒÔ½«Ç°¶ËÓiÑÔÓeºo¶ËÓiÑԵĴuÂeÖ¿ª¡£Ê×ÏÈÔÚPHP³ÌÐoÖлñÈ¡Êý¾Ý¿aÖдae´¢µÄÊý¾Ý£¬ÔÙͨ¹ý¼ÓÔØÄ£°aÒýÇae½«Êý¾ÝÖÅa³oÈ¥£¬È»ºo½«Ä£°aÎļþÔÙͨ¹ýÄ£°aÒýÇae¼ÓÔز¢´¦ÀiºoÊa³o¡£ËuÒÔPHP³ÌÐoÖ»ÊÇ´´½¨¶¯Ì¬Êý¾Ý£¬¼ÓÔØÄ£°aÒýÇae²¢½«¶¯Ì¬Êý¾ÝÖÅa¸øÄ£°a£¬Íe³ÉÁËPHP³ÌÐoµÄ¹¤×÷¡£¶øÄ£°aµÄÉeÖ­Ò²Ö»ÐeҪǰ¶Ë¹¤³Ìʦ¶ÀÁ¢Íe³É£¬Ê¹ÓÃHTML¡¢CSS¼°javascriptµÈǰ̨ҳÃaeÉe¼ÆÓiÑÔ±aд¡£ÁiÍa£¬ÔÚÄ£°aÎļþÖл¹ÐeҪʹÓÃÄ£°aÒýÇae¿ÉÒÔ½aÎoµÄ±e¼Ç£¬½«PHPÖÐÖÅa¹ýÀ´µÄ¶¯Ì¬Êý¾ÝÔÚÄ£°aÖÐÒýÓÃ

1¡¢Êý¾Ý¿aµÄÉe¼Æ

¼ÙÉeÊý¾Ý¿aþÎñÆ÷ÔÚ¡°localhost¡±Ö÷»uÉÏ£¬Á¬½ÓµÄÓû§ÃuºÍÃÜÂeÖ±ðΪ¡°admin¡±ºÍ¡°123456¡±£¬ÔÚ¸Ã*þÎñÆ÷ÉÏ´´½¨Ò»¸oÃuΪ¡°mydb¡±µÄÊý¾Ý¿a£¬²¢ÔÚ¸ÃÊý¾Ý¿aÖд´½¨Ò»¸oÃuΪ¡°User¡±µÄÓû§±i¡£´´½¨¸Ã±iµÄSQL-ËѯÓi¾aÈçÏÂËuʾ£º


    CREATE TABLE User(
     id SMALLINT(3) NOT NULL AUTO_INCREMENT,
     name VARCHAR(10) NOT NULL DEFAULT '',
     sex VARCHAR(4) NOT NULL DEFAULT '',
     age SMALLINT(2) NOT NULL DEFAULT '0',
     email VARCHAR(20) NOT NULL DEFAULT '',
     PRIMARY KEY (id)
    );

Óû§±iUser´´½¨Íe³ÉÒÔºo£¬½Ó×Å¿ÉÒÔÏo¸Ã±iÖвaÈeÒ»Ð(C)Êý¾Ý×÷ΪʾÀýÑÝʾʹÓã¬SQL²eѯÓi¾aÈçÏÂËuʾ:


    INSERT INTO User (name,sex,age,email) VALUES 
    ("a","ÄÐ",27,"a@a.com"),
    ("b","Å®",22,"b@b.com"),
    ("c","Å®",30,"c@c.com"),
    ("d","Å®",24,d@d.com);

2¡¢Ä£°aµÄÉe¼Æ

Ä£°aµÄÉe¼Æ²»Òª³oÏÖÈκεÄPHP´uÂe£¬¿ÉÒÔÓÉÇ°¶ËÈËÔ±À´Íe³É¡£ÔÚ×Ô¶¨ÒaµÄÄ£°aÒýÇaeÖУ¬¹ae¶¨ÁËÒªµ½Ö¸¶¨µÄĿ¼ÖÐȥѰÕÒÄ£°aÎļþ£¬Õa¸oÌض¨µÄĿ¼¿ÉÒÔÔÚ´´½¨Ä£°aÒýÇae¶ÔÏoʱָ¶¨£¬Ò²¿ÉÒÔʹÓÃĬÈϵÄĿ¼ÉeÖã¬Ä¬ÈÏ¿ÉÒÔ½«Ä£°aÎļþ´aeÅÔÚµ±Ç°Ä¿Â¼Öеġ°templates¡±Ä¿Â¼Ï¡£±¾Àý¹²ÐeÒªÈý¸oÄ£°aÎļþmain.tpl¡¢header.tplºÍfooter.tpl£¬¶¼´aeÅÔÚÕa¸oĬÈϵÄĿ¼ÉeÖÃÖС£ÕaÈý¸oÄ£°aÎļþµÄ´uÂeÈçÏÂËuʾ£º

Ä£°aµÄÍ*²¿Îļþheader.tpl


    <!DOCTYPE html>
    <html lang="en">
    <head>
     <meta charset="UTF-8">
     <title> <{$title}> </title>
    </head>
    <body>

Ä£°aµÄβ²¿Îļþfooter.tpl


      <div style="width:200px;margin: 0 auto;">##### <{$author}> #####</div>  
     </body>
    </html>

Ö÷Ä£°aÎļþmain.tpl


    <{include 'header.tpl'}>
     <table border="1" align="center" width="500">
      <{ loop $users $user }>
       <tr> 
        <{loop $user $u }>

          <{if $u == "ÄÐ" }>
           <td style="color:green">
          <{elseif $u == "Å®"}>
           <td style="color:red">
          <{else}>
           <td>
          <{/if}>

           <{$u}></td>    
        <{/loop}>
       </tr>
      <{/loop}>
     </table>
    <{include 'footer.tpl'}>

Îļþmain.tplÊÇÖ÷Ä£°aÎļþ£¬ÔÚ¸ÃÎļþÖÐʹÓÃ<{include"header.tpl"}>ºÍ<{include"footer.tpl"}>Á½¸o±e¼ÇÖ±ðÔÚ¸ÃÎļþµÄ¶¥²¿ºÍµ×²¿£¬½«¶ÀÁ¢µÄͲ¿ºÍβ²¿Ä£°aÎļþ°uº¬µ½Õa¸oÖ÷Ä£°aÎļþÖС£²¢ÔÚ¸ÃÎļþÖÐʹÓÃ<{tableName}>±e¼Ç»ñÈ¡´ÓPHPÖж¯Ì¬ÖÅa¹ýÀ´µÄ±iÃu£¬ÒÔ¼°Ê¹ÓÃË«²a<{loop}>±e¼ÇǶÌ×£¬±eÀu´ÓPHPÖж¯Ì¬ÖÅa¹ýÀ´µÄÔÚÊý¾Ý¿aÖлñÈ¡µ½µÄ¶þάÊý×e$Users£¬»¹ÔÚ<{loop}>±e¼ÇÖÐʹÓÃÌo¼þÑ¡Ôñ±e¼Ç<{if}>×eºÏ£¬½«Êý¾ÝÖÐÐÔ±ðΪ¡°ÄС±µÄ±i¸ñ±³¾°ÉeÖÃΪºiÉ«ºÍÒ»Ð(C)ÆaËuÅжϡ£±»°uº¬½øÀ´µÄͲ¿Ä£°aÎļþheader.tplºÍβ²¿Ä£°aÎļþfooter.tplҲͬÑu¿ÉÒÔ»ñÈ¡´ÓPHPÖж¯Ì¬ÖÅa¸øÄ£°aµÄÊý¾Ý

3¡¢PHP³ÌÐoÉe¼Æ

ͨ¹ýÄ£°aÒýÇaeµÄʹÓã¬PHP³ÌÐoÔ±ÔÚ±aд´uÂeʱ£¬Ö»ÐeÒªPHPÒ»ÖÖÓiÑԾͿÉÒÔÁË£¬²»ÓÃÔÙȥʹÓÃHTML¡¢CSSÒÔ¼°javascriptµÈÒ³ÃaeÉe¼ÆÓiÑÔÍe³ÉÇ°¶ËµÄ¹¤×÷ÁË¡£ÏÂÃaeÊÇÒ»¸oÎļþÃuΪindex.phpµÄPHP½Å±¾Îļþ£¬ºÍÄ£°aÒýÇaeÀaËuÔÚµÄÎļþmytpl_class.phpÔÚͬһ¸oĿ¼Ï¡£´uÂeÈçÏÂËuʾ£º


    <?php
     //°uº¬Ä£°aÒýÇaeÀa
     include "mytpl.class.php";
     //´´½¨Ä£°aÒýÇae¶ÔÏo
     $tpl = new Mytpl;
     //Á¬½ÓÊý¾Ý¿a
     $pdo = new PDO("mysql:host=localhost;dbname=mydb", "admin", "123456");
     //Ö´ÐÐSQLÓi¾a
     $stmt = $pdo -> prepare("select id, name, sex,age,email from User order by id");
     $stmt ->execute();
     $data = $stmt -> fetchAll(PDO::FETCH_ASSOC);
     //ÕaÊÇ´ÓÊý¾Ý¿a»ñÈ¡µÄ¶¯Ì¬Êý¾Ý£¬ÐeÒªÔÚÄ£°aÖÐÏÔʾ
     $tpl->assign('title',"×Ô¶¨ÒaÄ£°aÒýÇae");$tpl->assign('auto',"С»ð²ñ");
     $tpl->assign('users',$data);
     $tpl -> display("main.tpl");
    ?>

ÔÚÉÏÃaeµÄPHP½Å±¾ÎļþÖУ¬Í¨¹ýPDO¶ÔÏoÁ¬½ÓMySQLþÎñÆ÷£¬²¢»ñÈ¡Óû§±iUserÖеÄÈ«²¿¼Ç¼£¬²¢ÒÔPHPµÄ¶þάÊý×e±aÁ¿ÐÎʽ±£´aeÔÚ±aÁ¿dataÖС£½Ó×ÅʹÓðuº¬½øÀ´µÄµ±Ç°Ä¿Â¼Ïµġ°mytplclss.php¡±Îļþ£¬´´½¨²¢³oʼ»¯Ä£°aÒýÇaeÀaµÄ¶ÔÏodataÖС£½Ó×ÅʹÓðuº¬½øÀ´µÄµ±Ç°Ä¿Â¼Ïµġ°mytplclss.php¡±Îļþ£¬´´½¨²¢³oʼ»¯Ä£°aÒýÇaeÀaµÄ¶ÔÏotpl¡£ÔÙͨ¹ý¸Ã¶ÔÏoÖеÄassign()½¨ÏoÄ£°aÖÅaÒ»Ð(C)Êý¾Ý£¬È»ºoʹÓøöÔÏoÖеÄdisplay()½¨ÔØÈeÄ£°aÎļþmain.tpl¡£²¢½«Ä£°aÖбe¼ÇµÄÌØÊa±aÁ¿Ìae»»Îª´ÓPHPÖÐ*ÖÅaµÄ¶¯Ì¬Êý¾Ý£¬´¦ÀiÍe±ÏÒÔºoÊa³oÄ£°aÒ³Ãae¡£Ò³ÃaeµÄÊa³o½a¹uÈçÏÂËuʾ

ÏÞÓÚ¸÷ÖÖ²»Í¬µÄÌo¼þÏÞÖÆ£¬±ÈÈçʱ¼a¡¢¾­Ñe£¬×oÒ»¸o×Ô¶¨ÒaµÄPHPÄ£°aÒýÇaeÊÇ*dz£À§Äѵġ£Æaʵ£¬ÐeÒªµÄ²¢²»ÊÇÖØй¹ÔiÒ»¸oPHPÄ£°a£¬¶øÊÇÑ¡ÔñÒ»¸o×iÌu½u×Ô¼ºµÄPHPÄ£°a¼ÓÒÔ¸ÄÔi

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu±¾ÎĵÄÄÚÈݶԴo¼ÒµÄѧϰ»oÕß¹¤×÷ÄÜ´øÀ´Ò»¶¨µÄ°iÖu£¬Í¬Ê±Ò²Ï£Íu¶a¶aÖ§³Ö½Å±¾Ö®¼Ò£¡

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例