Home > Backend Development > PHP Tutorial > Example of using smarty to generate static files in PHP_PHP Tutorial

Example of using smarty to generate static files in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:31:53
Original
855 people have browsed it

First, fill the template with the content that needs to be staticized


Copy code The code is as follows:

#eg. This is a static homepage

function staticIndex(){

$newslist = $article->getArticles(null,54,'DESC',1,6,false,1,2,'',0,0,1);
if($newslist){
foreach($newslist as $k=>$v){
$newslist[$k]['title_all'] = $v['title'];
$newslist[$k][ 'title'] = cutstr($v['title'],36,'...');
}
$smarty->assign('newslist',$newslist);
}

$content = '';
$content = $smarty->fetch('index.html',true);//This is Smarty’s own function to generate static pages
$static_name = ROOT_PATH.'index.html';//This is the current path file for generating static pages
fopen($static_name,'a');//Open this file
@file_put_contents($static_name,$content); //Finally write in
return true;
}

//Static list page, staticized according to different categories

function staticContent(){//Number of static words required
$ids = array();//Get all content
$ids = $this->getListIds();//This Method to get all the content, the red part below corresponds to its method
foreach($ids as $k=>$value){
//echo $value['catid'];
if(! file_exists(ROOT_PATH.'demo/')){//Determine whether there is this folder under the root directory. If not, create the demo folder
mkdir(ROOT_PATH.'demo/');
}
if(!file_exists(ROOT_PATH.'demo/'.$value['catid'])){//Judge whether there is a corresponding category folder under this folder
mkdir(ROOT_PATH.'demo/'.$ value['catid']);
}
$html_content = $this->getDemoContent($value['demoid']);
$static_name = ROOT_PATH.'demo/'.$value[ 'catid'].'/'.$value['demoid'].'.html';
fopen($static_name,'a');
@file_put_contents($static_name,$html_content);
}
return true;
}

//Take out the page ID that needs to be staticized
function getListIds(){
$sql = "select * from {$this- >tablepre}demo order by demoid asc";
$rs = $this->db->getAll($sql);
if($rs){
return $rs;
}else{
return false;
}
}

//content single page staticization
function getDemoContent($id){
global $smarty,$view_templates ,$admin_templates;
loadModel(array('demo'));
$demo = new demo();
$content = '';
$smarty->template_dir = ROOT_PATH.$ view_templates;
$getMobanOne = $this->getMobanDetail($id);
$mobandetail = $demo->MobanList($id);
foreach($mobandetail as $k=>$ v){
$smarty->assign($k,$v);
}
$this->catid = $getMobanOne['catid'];
$smarty-> assign('pre_title',$mobandetail['membername']);
$smarty->assign('mobandetail',$mobandetail);
$content = $smarty->fetch('demo_show.html ',true);
$smarty->template_dir = ROOT_PATH.$view_templates;
return $content;
}

The one I used before to generate a static page required executing the PHP file every time before it could be generated. Now the one I wrote is controlled by people. It can be generated whenever you want. It is very convenient. I hope it can bring convenience to everyone. I wish you all the best. Happy work everyone.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/760286.htmlTechArticleFirst, fill the content that needs to be staticized into the template and copy the code as follows: #eg. This is Static homepage function staticIndex(){ $newslist = $article-getArticles(nul...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template