WIKI使用導(dǎo)航
站長百科導(dǎo)航
站長專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計算
- 微博營銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺
- WIKI程序與應(yīng)用
- 美國十大主機(jī)
CmsTop/模板常用函數(shù)
- str_cut
將給定的字符串截取一定的長度,中文一個字為 2 個長度。
函數(shù)原型:
/* * @param $string 待處理的字符串 * @param $length 要截取的長度,中文一個字為 2 個長度 * @param $dot 超出長度用什么字符替代,默認(rèn)為 ... * @param $charset 要處理的字符串的編碼,可選,默認(rèn)為 utf-8 */ function str_cut($string, $length, $dot = '...', $charset = 'utf-8') {...}
常用方法: 截取文章標(biāo)題 15 個中文字符,超出部分不顯示
{str_cut($r[title], 30, )}
截取內(nèi)容描述 50 個中文字符,超出部分用默認(rèn)的 ... 代替
{str_cut(description($r[contentid]), 100)}
- description
獲取指定內(nèi)容的描述,如果有的話。
函數(shù)原型:
/** * @param $contentid 內(nèi)容ID * @param $modelid 模型ID,可選 */ function description($contentid, $modelid = null) {...}
使用方法: 在內(nèi)容列表中截取描述
{content size="2" orderby="`weight` DESC"} <p>{str_cut(description($r[contentid]), 100, '')}</p> {/content}
- thumb
截取圖片為指定的的高寬,可指定是否返回絕對路徑和默認(rèn)圖片。
需要注意的是,截取是按比例處理的,當(dāng)要截取的比例和原圖的比例不吻合時,會返回不超過指定高寬的最合適的大小,因此截取出來的圖片大小并不總是指定的高寬。
如果原圖不存在,且未指定默認(rèn)圖片,將返回 IMG_URL . 'images/nopic.gif'。
函數(shù)原型:
/* * @param $img 待處理的圖片 * @param $width 要裁剪的寬度 * @param $height 要裁剪的高度 * @param $is_abs 是否返回絕對路徑,默認(rèn)返回絕對地址 * @param $default 當(dāng)指定的圖片不存在時返回的默認(rèn)的圖片 */ function thumb($img, $width, $height, $is_abs = 1, $default = null) {...}
常用方法: 截取內(nèi)容的縮略圖為 100 x 50 的大小
{thumb($r[thumb], 100, 50)}
將返回類似 http://cmstop.com/upload/2010/0611/1285555983557.jpg 的地址
截取內(nèi)容的縮略圖為 100 x 50 的大小,并返回相對地址
{thumb($r[thumb], 100, 50, 0)}
將返回類似 2010/0611/1285555983557.jpg 的地址
- table
讀取指定表的內(nèi)容,如果不指定主鍵值,則返回該表的所有內(nèi)容;如果指定了主鍵值,則返回該條記錄; 如果進(jìn)一步指定了字段名稱,則返回這條記錄指定字段的值。
慎重使用諸如:table('content') 或 table('article') 的寫法,不指定主鍵 ID 將返回該內(nèi)容表的所有內(nèi)容,除非你真的知道自己在做什么。
函數(shù)原型:
/** * @param $table 要讀取的表名稱,不需要前綴 * @param $id 主鍵的值(僅支持主鍵為單列的情況) * @param $field 指定要讀取的列名 */ function table($table, $id = null, $field = null)
常用方法: 在 {content} 標(biāo)簽中讀取欄目名稱
{table('category', $r[catid], 'name')}
在 {content} 標(biāo)簽中讀取文章內(nèi)容
{table('article', $r[contentid], 'content')}
- channel
獲得所有頻道(頂級欄目)。
函數(shù)原型:
// 不接受參數(shù) function channel() {...}
使用方法:
<ul> {loop channel() $catid $category} <li><a href="{$category[url]}">{$category[name]}</a></li> {/loop} </ul>
- subcategory
獲得指定欄目的子欄目。
函數(shù)原型:
/** * @param $catid 指定欄目的欄目ID * @param $tree 是否要返回樹形結(jié)構(gòu)的數(shù)組 * @param $subcategory 初始化的數(shù)組,如果指定,返回的結(jié)果將和該數(shù)組合并 */ function subcategory($catid, $tree = false, & $subcategory = array()) {...}
使用方法: 獲得欄目 ID 為 1 的所有子欄目的最新 10 條內(nèi)容
{loop subcategory(1) $catid $category} <h3><a href="{$category[url]}">{$category[name]}</a></h3> <ul> {content catid="$catid" size="10" orderby="`published` DESC"} <li><a href="{$r[url]}"{if $r[color]} style="color:{$r[color]}"{/if}>{str_cut($r[title], 50, '')}</a></li> {/content} </ul> {/loop}
- text_format
實體化字符串中的HTML標(biāo)簽,將空格替換為 ,并將換行轉(zhuǎn)換為 <br />。
函數(shù)原型:
/* * @param $string 待處理的字符串 */ function text_format($string) {...}
- config
返回 ./cmstop/config/ 目錄里面指定的配置文件或配置項。
函數(shù)原型:
/** * @param $file 要讀取的配置文件 * @param $key 要讀取的配置項,不指定 $key 則返回一個數(shù)組 * @param $default 指定的配置項不存在時的默認(rèn)值 */ function config($file, $key = null, $default = null) {...}
常用方法: 讀取配置文件中的網(wǎng)站字符集,如果沒有定義則使用 UTF-8
{config('config', 'charset', 'UTF-8')}
- setting
讀取系統(tǒng)或各個擴(kuò)展的配置。
函數(shù)原型:
/** * @param $app 要讀取的擴(kuò)展名稱 * @param $var 要讀取的配置名稱,不指定則返回該擴(kuò)展的所有配置 */ function setting($app, $var = null) {...}
常用方法: 讀取發(fā)布評論是否審核
{setting('comment', 'ischeck')}
- url
根據(jù)當(dāng)前設(shè)定的URL模式返回對應(yīng)的鏈接。
URL 模式是在 ./cmstop/config/config.php 里面設(shè)置的,支持 standard, pathinfo, querystring, rewrite 四種模式,僅對前臺有效。
函數(shù)原型:
/** * @param $aca 應(yīng)用名稱/控制名稱/動作名稱 * @param $params 附加參數(shù) * @param $is_full 是否返回絕對鏈接 */ function url($aca, $params = null, $is_full = false) {...}
常用方法: 內(nèi)容頁中獲取當(dāng)前內(nèi)容的評論鏈接
<a href="{APP_URL}{url('comment/comment/index', 'contentid=' . $contentid)}">查看評論</a>
- pages
根據(jù)指定參數(shù)返回分頁 HTML。
函數(shù)原型:
/** * @param $total 內(nèi)容的總數(shù) * @param $page 當(dāng)前分頁 * @param $pagesize 每頁分頁大小 * @param $offset 偏移量 * @param $url 分頁所使用的 URL 規(guī)則,不指定則使用當(dāng)前請求的 URL * @param $mode 為 true 則將轉(zhuǎn)換 URL 中的 & 為 & */ function pages($total, $page = 1, $pagesize = 20, $offset = 2, $url = null, $mode = false) {...}
常用方法: 列表頁分頁
{pages($total, $page, $pagesize, 2, $urlrule)}
- where_mintime / where_maxtime
SQL 查詢輔助函數(shù),構(gòu)造 SQL 附加 WHERE 的時間語句。
函數(shù)原型:
function where_mintime($field, $mintime) {...} function where_maxtime($field, $maxtime) {...}
常用方法:
<?php $sql = "SELECT * FROM `cmstop_content` WHERE " . where_mintime('published', '2010-11-04'); // 結(jié)果類似: $sql = "SELECT * FROM `cmstop_content` WHERE published >= 1288828800"; ?>
- modelid
根據(jù)指定的模型別名得到模型ID。
函數(shù)原型:
/** * @param $alias 模型的英文別名 function modelid($alias) {...}
- username
根據(jù)用戶ID獲得用戶名
函數(shù)原型:
/** * @param $userid 用戶ID */ function username($userid) {...}
使用方法:
{content size="10" orderby="`published` DESC} <span>由 {username($r[createdby])} {if $r[iscontribute]}投稿于{else}發(fā)表于{/if} {date('Y-m-d', $r[published])}</span> {/content}
- userid
根據(jù)用戶名獲得用戶ID
函數(shù)原型:
/** * @param $username 用戶名 */ function userid($username) {...}
- space_url
根據(jù)專欄 ID 獲得專欄地址。
函數(shù)原型:
/** * @param $spaceid 專欄ID,請注意不是用戶ID */ function space_url($spaceid) {...}