• 标签库


    ThinkPHP 模版中的标签库,我们使用的很多内置标签就是
    通过标签库声明而来的,当然还可以自定义扩展标签。

    一.内置标签

    模版中的这些内置标签是是在  ThinkPHP/Library/Think/Template/TagLib  下

    在完整版中提供了两个类文件: Cx.class.php (内置标签核心类)、 Html.class.php (html 扩展标签)

    观察  Cx.class.php  源码,我们发现其实就是内置标签的核心类。比如:

     1 /**
     2      * volist标签解析 循环输出数据集
     3      * 格式:
     4      * <volist name="userList" id="user" empty="" >
     5      * {user.username}
     6      * {user.email}
     7      * </volist>
     8      * @access public
     9      * @param array $tag 标签属性
    10      * @param string $content  标签内容
    11      * @return string|void
    12      */
    13 public n function _volist($tag,$content) {
    14             //核心代码省略
    15 }

    在 ThinkPHP 中,Cx.class.php 是自动加载的,所以并不需要各种配置或引入而可以直接编写标签代码即可运行。

    但如果是 Html.class.php 这种扩展性标签库,则需要通过其他方式才可以运行:

     1 /**
     2      * select标签解析
     3      * 格式: <html:select options="name" selected="value" />
     4      * @access public
     5      * @param array $tag 标签属性
     6      * @return string|void
     7      */
     8     public function _select($tag) {
     9            //核心代码省略
    10 }

    如果要使用 Html.class.php 的扩展标签,那么首先要导入这个标签:

    在 View/User/index.html 中导入:

    1 //导入扩展标签
    2  <taglib name="html" />

    在使用标签的时候,前面需要加上html:,表示这个是html标签库

    1  <html:select options="name" selected="value" /> 

    PS:Cx.class.php 因为是内置标签,所以不需要加 Cx:,但 Html.class.php 则需要
    加上 html:,否则会无法解析标签。

    在 WeiBo/Common/Conf/config.php 中将Cx和Html都设置为内置标签

    1 'TAGLIB_BUILD_IN'=>'cx,html',
    1 //设置成内置标签,则不需要 html:了
    2 <select options="name" selected="value" />

    PS:设置内置标签有一定的危险性,因为可能会出现标签的重复导致冲突。就比如select标签,本来就属于html内的标签,所以调用 Html.class.php 就会产生冲突了。

    如果没有设置为内置标签的话,那么都需要通过 tablib 进行导入,我们可以设置标签
    预加载。这样,就不需要每次都进行导入操作了。

    在 WeiBo/Common/Conf/config.php 中插入代码:

    1 //标签预加载
    2 'TAGLIB_PRE_LOAD'=>'html',

    使用时在 Html.class.php 或者 Cx.class.php 里的调用标签的注释中查看调用格式,编译后的格式可在 Runtime/Cache/Home 中的缓存文件中查看。

    二.扩展标签库

    有时感觉内置的标签不能满足项目日益繁杂的项目需求,这是可能想自己扩展一些标签
    来使用,那么自己怎么扩展标签呢?

    第一步:在 Think/Library/Think/Template/TagLib 下创建一个 Test.class.php 

    顶部的三行和 Cx.class.php 的顶部三行是一样的:

     1 namespace ThinkTemplateTagLib;
     2 use ThinkTemplateTagLib;
     3 defined('THINK_PATH') or exit();
     4 /**
     5  * Test标签库驱动
     6  */
     7 class Test extends TagLib{
     8     // 标签定义
     9     protected $tags   =  array(
    10         'mytest'    => array('attr'=>'color,border','close'=>1),
    11         );
    12     public function _mytest($tag,$content) {
    13         $color='';
    14         $border='';
    15         if(isset($tag['color'])) {
    16             $color='color:'.$tag['color'];
    17         }
    18         if(isset($tag['border'])) {
    19             $border='border:'.$tag['border'];
    20         }
    21         $css=$color.';'.$border;
    22         return '<div style="'.$css.'">'.$content.'</div>';
    23     }
    24 }

    其实就是跟着其他两个的格式来写就好。

    其中 'close'=>1 是双标签,就是在引用标签的时候要收尾,比如在 View/User/index.html 引用时:

    1 <Test:mytest color="red" border="5px solid blue">测试标签</Test:mytest>

    后面必须加上 </Test:mytest> ,否则就引用不成功

    其中  function _mytest($tag,$content)  的 $tag 就是获取 View/User/index.html 中的 color="red" border="5px solid blue" ,

    可以在  function _mytest($tag,$content)  中使用  echo $tag;  来获得它的属性, $content 则是 View/User/index.html 

    中的“测试标签

    结果如下图:

    另外当是否成功引用 ThinkPHP/Library/Think/Template/TagLib 下的文件时,可在调试工具下的文件中查看到:

  • 相关阅读:
    Jquery对象与DOM对象之间的转换
    关于Content-Type中application/x-www-form-urlencoded 和 multipart/form-data的区别及用法
    ${pageContext.request.contextPath} :JSP取得绝对路径方法
    servlet到底是什么?
    get和post的区别
    HTTP请求返回状态码详解
    XMLHttpRequest对象的readyState和status区别
    hdu 3594 仙人掌图
    hdu 4744 最小费用流
    hdu 4729 树链剖分
  • 原文地址:https://www.cnblogs.com/jacson/p/4512314.html
Copyright © 2020-2023  润新知