钩子

在模版文件中经常看到调用钩子的用法
如:

  1. <!--{hook/global_usernav_extra2}-->

这行代码在模版解析文件中会如此解析:

  1. $template = preg_replace("/\{hook\/(\w+?)(\s+(.+?))?\}/ie", "\$this->hooktags('\\1', '\\3')", $template);

可以看到是直接调用$this->hooktags方法,那hooktags是怎么定义的呢?请看:

  1. function hooktags($hookid, $key = '') {
  2. global $_G;
  3. $i = count($this->replacecode['search']);
  4. $this->replacecode['search'][$i] = $search = "<!--HOOK_TAG_$i-->";
  5. $dev = '';
  6. if(isset($_G['config']['plugindeveloper']) && $_G['config']['plugindeveloper'] == 2) {
  7. $dev = "echo '<hook>[".($key ? 'array' : 'string')." $hookid".($key ? '/\'.'.$key.'.\'' : '')."]</hook>';";
  8. }
  9. $key = $key !== '' ? "[$key]" : '';
  10. $this->replacecode['replace'][$i] = "<?php {$dev}if(!empty(\$_G['setting']['pluginhooks']['$hookid']$key)) echo \$_G['setting']['pluginhooks']['$hookid']$key;?>";
  11. return $search;
  12. }

钩子

在模版文件中经常看到调用钩子的用法
如:

  1. <!--{hook/global_usernav_extra2}-->

这行代码在模版解析文件中会如此解析:

  1. $template = preg_replace("/\{hook\/(\w+?)(\s+(.+?))?\}/ie", "\$this->hooktags('\\1', '\\3')", $template);

可以看到是直接调用$this->hooktags方法,那hooktags是怎么定义的呢?请看:

  1. function hooktags($hookid, $key = '') {
  2. global $_G;
  3. $i = count($this->replacecode['search']);
  4. $this->replacecode['search'][$i] = $search = "<!--HOOK_TAG_$i-->";
  5. $dev = '';
  6. if(isset($_G['config']['plugindeveloper']) && $_G['config']['plugindeveloper'] == 2) {
  7. $dev = "echo '<hook>[".($key ? 'array' : 'string')." $hookid".($key ? '/\'.'.$key.'.\'' : '')."]</hook>';";
  8. }
  9. $key = $key !== '' ? "[$key]" : '';
  10. $this->replacecode['replace'][$i] = "<?php {$dev}if(!empty(\$_G['setting']['pluginhooks']['$hookid']$key)) echo \$_G['setting']['pluginhooks']['$hookid']$key;?>";
  11. return $search;
  12. }