[php]代码库
一:在配置中载入标签库
'APP_AUTOLOAD_PATH' => '@.TagLib',
'TAGLIB_BUILD_IN' => 'Cx,Hd', //Cx是tp里的核心标签 Hd是自己写的自定义标签名
二:写一个自定义标签类
<?php
import('TagLib');
/*
*自定义标签库
*/
Class TagLibHd extends TagLib{
Protected $tags =array(
'nav' => array('attr' => 'limit,order','close' => 1), //定义属性 close=>1闭合标签
'hot' => array('attr' => 'limit','close' => 1),
'new' => array('attr' => 'limit','close' => 1)
);
Public function _nav ($attr,$content) {
$attr = $this->parseXMLAttr($attr);
$str = <<<str
<?php
\$_nav_cate = M('cate')->order("{$attr['order']}")->select();
import('Class.Category',APP_PATH);
\$_nav_cate = Category::unlimitedForLayer(\$_nav_cate);
foreach (\$_nav_cate as \$_nav_cate_v) :
extract(\$_nav_cate_v);
\$url = U('/c_'.$id);
?>
str;
$str .= $content;
$str .= '<?php endforeach;?>';
return $str;
}
Public function _hot ($attr,$content) {
$attr = $this->parseXMLAttr($attr);
$limit = $attr['limit'];
$str = <<<str
<?php
\$field = array('id','title','hits');
\$_hot_blog = M('blog')->field(\$field)->limit({$limit})->order('hits DESC')->select();
foreach (\$_hot_blog as \$_hot_value) :
extract(\$_hot_value);
\$url = U('/'.\$id);
?>
str;
$str .= $content;
$str .= '<?php endforeach;?>';
return $str;
}
Public function _new ($attr,$content) {
$attr = $this->parseXMLAttr($attr);
$limit = $attr['limit'];
$str = <<<str
<?php
\$field = array('id','title','time','hits');
\$_hot_blog = M('blog')->field(\$field)->limit({$limit})->order('time DESC')->select();
foreach (\$_hot_blog as \$_hot_value) :
extract(\$_hot_value);
\$url = U('/'.\$id);
?>
str;
$str .= $content;
$str .= '<?php endforeach;?>';
return $str;
}
}
?>
三:在模板中使用标签