Dotclear

Changeset 2574:b1eb712bc56f


Ignore:
Timestamp:
11/21/13 10:07:30 (12 years ago)
Author:
Dsls
Branch:
default
Message:

Template engine backported to clearbricks, closes #1862

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • .hgsubstate

    r2549 r2574  
    1 a51d747b2601fd10b50926a017e25764abe0c872 inc/libs/clearbricks 
     15779e9453cf50ec3c90c6a260061c29d2ebac064 inc/libs/clearbricks 
  • inc/public/class.dc.template.php

    r2566 r2574  
    234234     } 
    235235 
    236      protected function compileFile($file) 
    237      { 
    238           $fc = file_get_contents($file); 
    239  
    240           $this->compile_stack[] = $file; 
    241  
    242           # Remove every PHP tags 
    243           if ($this->remove_php) 
    244           { 
    245                $fc = preg_replace('/<\?(?=php|=|\s).*?\?>/ms','',$fc); 
    246           } 
    247  
    248           # Transform what could be considered as PHP short tags 
    249           $fc = preg_replace('/(<\?(?!php|=|\s))(.*?)(\?>)/ms', 
    250           '<?php echo "$1"; ?>$2<?php echo "$3"; ?>',$fc); 
    251  
    252           # Remove template comments <!-- #... --> 
    253           $fc = preg_replace('/(^\s*)?<!-- #(.*?)-->/ms','',$fc); 
    254  
    255           # Lexer part : split file into small pieces 
    256           # each array entry will be either a tag or plain text 
    257           $blocks = preg_split( 
    258                '#(<tpl:\w+[^>]*>)|(</tpl:\w+>)|({{tpl:\w+[^}]*}})#msu',$fc,-1, 
    259                PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); 
    260  
    261           # Next : build semantic tree from tokens. 
    262           $rootNode = new tplNode(); 
    263           $node = $rootNode; 
    264           $errors = array(); 
    265           foreach ($blocks as $id => $block) { 
    266                $isblock = preg_match('#<tpl:(\w+)(?:(\s+.*?)>|>)|</tpl:(\w+)>|{{tpl:(\w+)(\s(.*?))?}}#ms',$block,$match); 
    267                if ($isblock == 1) { 
    268                     if (substr($match[0],1,1) == '/') { 
    269                          // Closing tag, check if it matches current opened node 
    270                          $tag = $match[3]; 
    271                          if (($node instanceof tplNodeBlock) && $node->getTag() == $tag) { 
    272                               $node->setClosing(); 
    273                               $node = $node->getParent(); 
    274                          } else { 
    275                               // Closing tag does not match opening tag 
    276                               // Search if it closes a parent tag 
    277                               $search = $node; 
    278                               while($search->getTag() != 'ROOT' && $search->getTag() != $tag) { 
    279                                    $search = $search->getParent(); 
    280                               } 
    281                               if ($search->getTag() == $tag) { 
    282                                    $errors[] = sprintf( 
    283                                         __('Did not find closing tag for block <tpl:%s>. Content has been ignored.'), 
    284                                         html::escapeHTML($node->getTag())); 
    285                                    $search->setClosing(); 
    286                                    $node = $search->getParent(); 
    287                               } else { 
    288                                    $errors[]=sprintf( 
    289                                         __('Unexpected closing tag </tpl:%s> found.'), 
    290                                         $tag);; 
    291                               } 
    292                          } 
    293                     } elseif (substr($match[0],0,1) == '{') { 
    294                          // Value tag 
    295                          $tag = $match[4]; 
    296                          $str_attr = ''; 
    297                          $attr = array(); 
    298                          if (isset($match[6])) { 
    299                               $str_attr = $match[6]; 
    300                               $attr = $this->getAttrs($match[6]); 
    301                          } 
    302                          $node->addChild(new tplNodeValue($tag,$attr,$str_attr)); 
    303                     } else { 
    304                          // Opening tag, create new node and dive into it 
    305                          $tag = $match[1]; 
    306                          $newnode = new tplNodeBlock($tag,isset($match[2])?$this->getAttrs($match[2]):array()); 
    307                          $node->addChild($newnode); 
    308                          $node = $newnode; 
    309                     } 
    310                } else { 
    311                     // Simple text 
    312                     $node->addChild(new tplNodeText($block)); 
    313                } 
    314           } 
    315  
    316           if (($node instanceof tplNodeBlock) && !$node->isClosed()) { 
    317                $errors[] = sprintf( 
    318                     __('Did not find closing tag for block <tpl:%s>. Content has been ignored.'), 
    319                     html::escapeHTML($node->getTag())); 
    320           } 
    321  
    322           $err = ""; 
    323           if (count($errors) > 0) { 
    324                $err = "\n\n<!-- \n". 
    325                     __('WARNING: the following errors have been found while parsing template file :'). 
    326                     "\n * ". 
    327                     join("\n * ",$errors). 
    328                     "\n -->\n"; 
    329           } 
    330  
    331           return $rootNode->compile($this).$err; 
    332      } 
    333  
    334236     public function compileBlockNode($tag,$attr,$content) 
    335237     { 
     
    342244          $this->core->callBehavior('templateInsideBlock',$this->core,$this->current_tag,$attr,array(&$content)); 
    343245 
    344           if (isset($this->blocks[$this->current_tag])) { 
    345                $res .= call_user_func($this->blocks[$this->current_tag],$attr,$content); 
    346           } elseif ($this->unknown_block_handler != null) { 
    347                $res .= call_user_func($this->unknown_block_handler,$this->current_tag,$attr,$content); 
    348           } 
     246          $res .= parent::compileBlockNode($this->current_tag,$attr,$content); 
    349247 
    350248          # --BEHAVIOR-- templateAfterBlock 
     
    362260          $res = $this->core->callBehavior('templateBeforeValue',$this->core,$this->current_tag,$attr); 
    363261 
    364           if (isset($this->values[$this->current_tag])) { 
    365                $res .= call_user_func($this->values[$this->current_tag],$attr,ltrim($str_attr)); 
    366           } elseif ($this->unknown_value_handler != null) { 
    367                $res .= call_user_func($this->unknown_value_handler,$this->current_tag,$attr,$str_attr); 
    368           } 
     262          $res .= parent::compileValueNode($this->current_tag,$attr,$str_attr); 
    369263 
    370264          # --BEHAVIOR-- templateAfterValue 
     
    372266 
    373267          return $res; 
    374      } 
    375  
    376      public function setUnknownValueHandler($callback) 
    377      { 
    378           if (is_callable($callback)) { 
    379                $this->unknown_value_handler = $callback; 
    380           } 
    381      } 
    382  
    383      public function setUnknownBlockHandler($callback) 
    384      { 
    385           if (is_callable($callback)) { 
    386                $this->unknown_block_handler = $callback; 
    387           } 
    388268     } 
    389269 
Note: See TracChangeset for help on using the changeset viewer.

Sites map