问答项目---自定义标签调用栏目信息!
第一步:修改:修改Application/Common/conf/config.php
增加如下一条配置;
'TAGLIB_BUILD_IN' => 'Cx,Common\Tag\My', //加载自定义标签
<?php return array ( 'TAGLIB_BUILD_IN' => 'Cx,Common\\Tag\\My', ); ?>
cx是内置的标签库,Common\Tag\My是自定义的标签库位置;
示例是在Application/Common/Tag目录下建的My.class.php
第二步:
新建My.class.php文件
命名空间要和配置项中填写的一致;如下图:
<?php namespace Common\Tag; use Think\Template\TagLib; class My extends TagLib{ };
简单示例:
<?php namespace Common\Tag; use Think\Template\TagLib; class My extends TagLib{ // 定义标签 protected $tags = array( 'topcates'=> array('attr'=>'limit') ); public function _topcates($attr,$content){ //调用父类 $limit = isset($attr['limit']) ? $attr['limit'] : ''; // 组合PHP代码的字符串 $str = '<?php '; $str .= '$where = array("pid" =>0);'; $str .= '$_topcatesResult = M("category")->where($where)->limit('.$limit.')->select();'; $str .= 'foreach($_topcatesResult as $v):'; $str .= 'extract($v);?>'; $str .= $content; $str .= '<?php endforeach;?>'; return $str; } };
前台调用:
<topcates limit='3'>
<li><a href="{:U('List/index',array('id'=>$id))}">{$name}</a></li>
</topcates>

浙公网安备 33010602011771号