Home >Backend Development >PHP Tutorial > smarty 列表插件显示标题长度和连接解决思路

smarty 列表插件显示标题长度和连接解决思路

WBOY
WBOYOriginal
2016-06-13 13:42:53936browse

smarty 列表插件显示标题长度和连接
现在在写一个站, 要在smarty 插件那里写一个列表插件,要截取标题长度。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php function smarty_function_list($params, &$smarty)
    {
        extract($params);
        $cate = !empty($cate) ? trim($cate) : '';//表名
        $items = !empty($items) ? trim($items) : '*';//字段
        $length=!empty($length) ? trim ($length) :'10';//长度
        $where = !empty($where) ? trim($where) : '';//条件
        $order = !empty($order) ? trim($order) : '';//排序
        $page = !empty($page) ? intval($page) : 0;//分页
        $pagesize = !empty($pagesize) ? intval($pagesize) : 20;
        $varname = !empty($varname) ? trim($varname) : '';

        global $db;
        global $table;
        $sql="select $items from ".$table[$cate];
        if(!empty($where))
        {
            $sql.=" where $where ";
        }
        if(!empty($order))
        {
          $sql.=" order by $order";
        }
        $page=max($page,1);
        if($pagesize<1)
        {
            $pagesize=10;
        }
        $rs=$db->PageExecute($sql,$pagesize,$page);
        $rs=$rs->getRows();
        $smarty->assign($length,$length);
        $smarty->assign($varname, $rs);
        unset($rs);
    }
?>


上面是插件代码;
HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
{--list cate="articles" items="Title" length=4 varname="rs"--}
{--foreach from=$rs item=r--}
{--$r.Title--}<br>
{--/foreach--}


上面是模板代码。
现在就是要截取标题长度,和标题连接
$r.Titletruncatecn:$length:"...":true
这种的是不行的!各位师兄,师姐谢谢了!

------解决方案--------------------
探讨

$length是哪里来的?

如果是变了的话,试下这个呢
$r.Title|truncate:`$length`:"...":true
要不就指定一个具体数字
$r.Title|truncate:30:"...":true
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn