特别注意:
获取渲染的内容
Tinkphp5.1+ 需要使用 $this->fetch()->getContent();
5.1之前 $this->fetch();
在Tink/Controller基类中添加以下代码
/**
* 创建静态页面
* @access protected
* @htmlfile 生成的静态文件名称
* @htmlpath 生成的静态文件路径
* @param string $templateFile 指定要调用的模板文件
* 默认为空 由系统自动定位模板文件
* @return string
*/
protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
$content = $this->fetch($templateFile)->getContent();
$htmlpath = !empty($htmlpath) ? $htmlpath : TEMP_HTML_PATH;
$htmlfile = $htmlpath . $htmlfile . '.'.config('url_html_suffix');
if(is_file($htmlfile)){
unlink($htmlfile);
}
$File = new \think\template\driver\File();
$File->write($htmlfile, $content);
return $content;
}
“ TEMP_HTML_PATH ” 常量在/public/index.php 中定义就好,表示要存放的静态文件位置
列表分页使用示例:
/**
* 生成新闻列表和详情页
* @author clip
* @date 2018/9/23
* @param $param
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function create_news_html()
{
$page_size = 8;
$count = NewsModel::m_count([]);
$all_page = ceil($count / $page_size);//总页数
for ($i = 1; $i <= $all_page; $i++) {
if ($i == 0) {
//第一页
$new_index = 'index_1';
$pageing_html = self::s_paging_number($i, $all_page, 'news');
} else {
$new_index = 'index_' . $i;
$pageing_html = self::s_paging_number($i, $all_page, 'news');
}
$list = NewsModel::m_list(['page' => $i, 'page_size' => $page_size]);
self::assign('list', $list);
self::assign('pageing_html', $pageing_html);
//生成新闻列表
self::buildHtml($new_index, TEMP_HTML_PATH . 'news/', 'Index/news/index');
//生成新闻详情页
for ($i = 1; $i <= count($list); $i++) {
self::assign('info', $list[$i - 1]);
self::buildHtml('info_' . $list[$i - 1]['id'], TEMP_HTML_PATH . 'news/info/', 'Index/news/info');
}
}
}
/**
* 分页页码显示数量
* @author clip
* @date 2018/9/23
* @param $paging_current_page
* @param $paging_last_page
* @return string
*/
public static function s_paging_number($paging_current_page, $paging_last_page, $type)
{
//分页显示页面数量,取值范围 3-n
$paging_number = 7;
if ($paging_number < 3) {
$paging_number = 3;
}
if (($paging_number % 2) == 0) {
if ($paging_last_page <= $paging_number) {
$paging_start = 1;
$paging_end = $paging_last_page;
} else if ($paging_current_page < ($paging_number / 2 + 1)) {
$paging_start = 1;
$paging_end = $paging_number;
} else if ($paging_current_page >= ($paging_number / 2 + 1) && (($paging_current_page + ($paging_number / 2 - 1)) <= $paging_last_page)) {
$paging_start = $paging_current_page - ($paging_number / 2);
$paging_end = $paging_current_page + ($paging_number / 2 - 1);
} else {
$paging_start = $paging_last_page - $paging_number + 1;
$paging_end = $paging_last_page;
}
} else {
if ($paging_last_page <= $paging_number) {
$paging_start = 1;
$paging_end = $paging_last_page;
} else if ($paging_current_page < ceil($paging_number / 2)) {
$paging_start = 1;
$paging_end = $paging_number;
} else if ($paging_current_page >= ceil($paging_number / 2) && (($paging_current_page + floor($paging_number / 2)) <= $paging_last_page)) {
$paging_start = $paging_current_page - floor($paging_number / 2);
$paging_end = $paging_current_page + floor($paging_number / 2);
} else {
$paging_start = $paging_last_page - ($paging_number - 1);
$paging_end = $paging_last_page;
}
}
$html = '';
if ($type == "news") {
for ($i = $paging_start; $i <= $paging_end; $i++) {
if ($i != $paging_current_page) {
$html .= "<a href='/news/index_" . $i . ".html'>" . $i . "</a> ";
} else {
$html .= "<a class='active' href='/news/index_" . $i . ".html'>$i</a>";
}
}
}
return $html;
}