你的位置:HBcms宏博内容管理系统 模板设计 正文
模板设计
  1. 留言板
内容搜索
热门内容
  1. 几套推荐的HBcms模板下..
  2. 如何修改模板文件?怎..
  3. Smarty模板插件下载,..
  4. HBcms的模板语言是自己..
  5. smarty官方文档下载
  6. 模板变量说明,如何制..
  7. HBcms的Smarty模板结构..
  8. 做模板的基础:掌握基..
  9. 如何在模板里加上会员..
  10. 吐血推荐让流量暴涨的..
推荐内容
  1. 如何修改模板文件?怎..
  2. HBcms的模板语言是自己..
  3. 修改添加留言表单的外..
  4. 如何设计一个漂亮的公..
  5. 为留言板内容录入框增..
  6. 会员查看自己的留言列..
  7. 会员查看自己的留言详..
  8. 让导航条里的外部链接..
  9. 产品或图片频道如何自..
  10. 随机显示栏目文章的首..
HBcms模板中如何限定标题文字长度
  • 原作者:hbcms 添加时间:2023-06-13 发表时间:2006-07-30 人气:6422
  • hbcms模板应用增加了截取函数 cn_truncate

    函数目的:
    1。在模板中直接截取中文字符
    2。可保留原来的字符串style,如 font 等


    用法:
    cn_truncate 用法大致同官方的 truncate 函数,如下:

    <{$item_info.title|cn_truncate:18:"...":true}>

    第1个参数 18 表示截取 18 个汉字

    第2个参数 ... 表示,如果多余18个汉字,则显示 ...

    第3个参数 true 表示保留文字的初始颜色。 false 表示去掉颜色。

    已知的BUG:
    只做到了保留第一个style,而且有BUG(可查考源代码),请高手完善。

    本函数的最新完善版本可在  http://hbcms.com/cms/template/    中下载


    源代码如下:

    CODE:
    <?php
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */


    /**
     * Smarty truncate modifier plugin
     *
     * Type:     modifier<br>
     * Name:     truncate<br>
     * Purpose:  Truncate a string to a certain length if necessary,
     *           optionally splitting in the middle of a word, and
     *           appending the $etc string or inserting $etc into the middle.
     * @link     http://www.hbcms.com/
     * @author   http://www.hbcms.com/
     * @param string
     * @param integer
     * @param string
     * @param boolean $keep_first_style 是否保留第一个style
     * @return string

     * $keep_first_style 理解范例:

    $s = '<A HREF="http://www.hbcms.com/"><FONT COLOR="red"><U><B>宏博<U>内容</U>管<B>理</B>系 &nbsp;统&nbsp;
    <P>
    <FONT SIZE="2" COLOR="blue">HBCMS</FONT> 是一个可以免费使用的内容管理系统,您甚至可以用<A HREF="http://www.hbcms.com/">她来做商业网站</A>。点这里查看详细版权说明

    <BR>&nbsp;<BR>
    是否有错误
    <HR>
    已知bug,截取:<FONT SIZE="2" COLOR="blue">乌木乌木乌<U>木乌木</U>乌木乌木乌木乌木乌木</font>有异常
    <P>
    希望大家改善</B></U></FONT></A>';
    echo $s . '<hr><HR><HR>smarty_modifier_cn_truncate:<HR>';
    echo smarty_modifier_cn_truncate($s,30,'。。。',1);
    exit();
     */

    function smarty_modifier_cn_truncate($string$strlen 20$etc '...',
                                      
    $keep_first_style false)
    {
        
    $strlen $strlen*2;
        
    $string trim($string);
        if ( 
    strlen($string) <= $strlen )    {
            return 
    $string;
        }
        
    $str strip_tags($string);
        
    $j 0;
        for(
    $i=0;$i<$strlen;$i++) {
          if(
    ord(substr($str,$i,1))>0xa0$j++; 
        }
        if(
    $j%2!=0$strlen++; 
        
    $rstr=substr($str,0,$strlen);
        if (
    strlen($str)>$strlen  ) {$rstr .= $etc;} 

        if ( 
    $keep_first_style == true && ereg('^<(.*)>$',$string) )    {
            if ( 
    strlen($str) <= $strlen )    {
                return 
    $string;
            }
            
    $start_pos strpos($string,substr($str,0,4));
            
    $end_pos strpos($string,substr($str,-4));
            
    $end_pos $end_pos+4;
            
    $rstr substr($string,0,$start_pos) . $rstr substr($string,$end_pos,strlen($string));
        }

        return 
    $rstr

    }

    /* vim: set expandtab: */

    ?>

     

    官方的函数是 truncate ,可惜只支持单字节的英文字符,用法如下:

    Example 5-20. truncate

        
    $smarty
    = new Smarty
    ;
    $smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.'
    );
    $smarty->display('index.tpl'
    );

    ?>

    where index.tpl is:

    {$articleTitle}
    {$articleTitle|truncate}
    {$articleTitle|truncate:30}
    {$articleTitle|truncate:30:""}
    {$articleTitle|truncate:30:"---"}
    {$articleTitle|truncate:30:"":true}
    {$articleTitle|truncate:30:"...":true}

    This will output:

    Two Sisters Reunite after Eighteen Years at Checkout Counter.
    Two Sisters Reunite after Eighteen Years at Checkout Counter.
    Two Sisters Reunite after...
    Two Sisters Reunite after
    Two Sisters Reunite after---
    Two Sisters Reunite after Eigh
    Two Sisters Reunite after E...

     

     

  • 点这里复制本页地址发送给您QQ/MSN上的好友
  • 相关文章
  • 相关评论
    • 2007-06-10评论人:jones
    • very good!
  • 本文章所属分类:首页 模板设计