Changeset 2610:a9bc70b5a85d for inc/public
- Timestamp:
- 12/10/13 12:26:54 (12 years ago)
- Branch:
- default
- Parents:
- 2608:3365c9df16a6 (diff), 2594:e10cf5fd3749 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/public/class.dc.template.php
r2590 r2610 235 235 } 236 236 237 protected function compileFile($file)238 {239 $fc = file_get_contents($file);240 241 $this->compile_stack[] = $file;242 243 # Remove every PHP tags244 if ($this->remove_php)245 {246 $fc = preg_replace('/<\?(?=php|=|\s).*?\?>/ms','',$fc);247 }248 249 # Transform what could be considered as PHP short tags250 $fc = preg_replace('/(<\?(?!php|=|\s))(.*?)(\?>)/ms',251 '<?php echo "$1"; ?>$2<?php echo "$3"; ?>',$fc);252 253 # Remove template comments <!-- #... -->254 $fc = preg_replace('/(^\s*)?<!-- #(.*?)-->/ms','',$fc);255 256 # Lexer part : split file into small pieces257 # each array entry will be either a tag or plain text258 $blocks = preg_split(259 '#(<tpl:\w+[^>]*>)|(</tpl:\w+>)|({{tpl:\w+[^}]*}})#msu',$fc,-1,260 PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);261 262 # Next : build semantic tree from tokens.263 $rootNode = new tplNode();264 $node = $rootNode;265 $errors = array();266 foreach ($blocks as $id => $block) {267 $isblock = preg_match('#<tpl:(\w+)(?:(\s+.*?)>|>)|</tpl:(\w+)>|{{tpl:(\w+)(\s(.*?))?}}#ms',$block,$match);268 if ($isblock == 1) {269 if (substr($match[0],1,1) == '/') {270 // Closing tag, check if it matches current opened node271 $tag = $match[3];272 if (($node instanceof tplNodeBlock) && $node->getTag() == $tag) {273 $node->setClosing();274 $node = $node->getParent();275 } else {276 // Closing tag does not match opening tag277 // Search if it closes a parent tag278 $search = $node;279 while($search->getTag() != 'ROOT' && $search->getTag() != $tag) {280 $search = $search->getParent();281 }282 if ($search->getTag() == $tag) {283 $errors[] = sprintf(284 __('Did not find closing tag for block <tpl:%s>. Content has been ignored.'),285 html::escapeHTML($node->getTag()));286 $search->setClosing();287 $node = $search->getParent();288 } else {289 $errors[]=sprintf(290 __('Unexpected closing tag </tpl:%s> found.'),291 $tag);;292 }293 }294 } elseif (substr($match[0],0,1) == '{') {295 // Value tag296 $tag = $match[4];297 $str_attr = '';298 $attr = array();299 if (isset($match[6])) {300 $str_attr = $match[6];301 $attr = $this->getAttrs($match[6]);302 }303 $node->addChild(new tplNodeValue($tag,$attr,$str_attr));304 } else {305 // Opening tag, create new node and dive into it306 $tag = $match[1];307 $newnode = new tplNodeBlock($tag,isset($match[2])?$this->getAttrs($match[2]):array());308 $node->addChild($newnode);309 $node = $newnode;310 }311 } else {312 // Simple text313 $node->addChild(new tplNodeText($block));314 }315 }316 317 if (($node instanceof tplNodeBlock) && !$node->isClosed()) {318 $errors[] = sprintf(319 __('Did not find closing tag for block <tpl:%s>. Content has been ignored.'),320 html::escapeHTML($node->getTag()));321 }322 323 $err = "";324 if (count($errors) > 0) {325 $err = "\n\n<!-- \n".326 __('WARNING: the following errors have been found while parsing template file :').327 "\n * ".328 join("\n * ",$errors).329 "\n -->\n";330 }331 332 return $rootNode->compile($this).$err;333 }334 335 237 public function compileBlockNode($tag,$attr,$content) 336 238 { … … 343 245 $this->core->callBehavior('templateInsideBlock',$this->core,$this->current_tag,$attr,array(&$content)); 344 246 345 if (isset($this->blocks[$this->current_tag])) { 346 $res .= call_user_func($this->blocks[$this->current_tag],$attr,$content); 347 } elseif ($this->unknown_block_handler != null) { 348 $res .= call_user_func($this->unknown_block_handler,$this->current_tag,$attr,$content); 349 } 247 $res .= parent::compileBlockNode($this->current_tag,$attr,$content); 350 248 351 249 # --BEHAVIOR-- templateAfterBlock … … 363 261 $res = $this->core->callBehavior('templateBeforeValue',$this->core,$this->current_tag,$attr); 364 262 365 if (isset($this->values[$this->current_tag])) { 366 $res .= call_user_func($this->values[$this->current_tag],$attr,ltrim($str_attr)); 367 } elseif ($this->unknown_value_handler != null) { 368 $res .= call_user_func($this->unknown_value_handler,$this->current_tag,$attr,$str_attr); 369 } 263 $res .= parent::compileValueNode($this->current_tag,$attr,$str_attr); 370 264 371 265 # --BEHAVIOR-- templateAfterValue … … 373 267 374 268 return $res; 375 }376 377 public function setUnknownValueHandler($callback)378 {379 if (is_callable($callback)) {380 $this->unknown_value_handler = $callback;381 }382 }383 384 public function setUnknownBlockHandler($callback)385 {386 if (is_callable($callback)) {387 $this->unknown_block_handler = $callback;388 }389 269 } 390 270 … … 1173 1053 1174 1054 if ($lastn != 0) { 1055 // Set limit (aka nb of entries needed) 1175 1056 if ($lastn > 0) { 1057 // nb of entries per page specified in template -> regular pagination 1176 1058 $p .= "\$params['limit'] = ".$lastn.";\n"; 1177 1059 } else { 1178 $p .= "\$params['limit'] = \$_ctx->nb_entry_per_page;\n"; 1060 // nb of entries per page not specified -> use ctx settings 1061 $p .= "\$params['limit'] = (\$_page_number == 1 ? \$_ctx->nb_entry_first_page : \$_ctx->nb_entry_per_page);\n"; 1179 1062 } 1180 1063 // Set offset (aka index of first entry) 1181 1064 if (!isset($attr['ignore_pagination']) || $attr['ignore_pagination'] == "0") { 1182 $p .= "\$params['limit'] = array(((\$_page_number-1)*\$params['limit']),\$params['limit']);\n"; 1065 // standard pagination, set offset 1066 $p .= "\$params['limit'] = array((\$_page_number == 1 ? 0 : (\$_page_number - 2) * \$_ctx->nb_entry_per_page + \$_ctx->nb_entry_first_page),\$params['limit']);\n"; 1183 1067 } else { 1068 // no pagination, get all posts from 0 to limit 1184 1069 $p .= "\$params['limit'] = array(0, \$params['limit']);\n"; 1185 1070 }
Note: See TracChangeset
for help on using the changeset viewer.