This article mainly introduces the example of using smarty to generate static files in PHP. if you need it, you can refer to the example below. First, you need to fill the content that requires static content into the template.
The code is as follows:
# Eg. this is the static homepage
Function staticIndex (){
$ Newslist = $ article-> getArticles (null, 54, 'desc', false, '', 1 );
If ($ newslist ){
Foreach ($ newslist as $ k => $ v ){
$ Newslist [$ k] ['title _ all'] = $ v ['title'];
$ Newslist [$ k] ['title'] = cutstr ($ v ['title'], 36 ,'... ');
}
$ Smarty-> assign ('newlist', $ newslist );
}
$ Content = '';
$ Content = $ smarty-> fetch('index.html ', true); // This is the built-in function for generating static pages.
$ Static_name = ROOT_PATH.'index.html '; // This is the current path file of the static page.
Fopen ($ static_name, 'A'); // open this file
@ File_put_contents ($ static_name, $ content); // write it
Return true;
}
// Static list page, static by category
Function staticContent () {// number of lines requiring static calls
$ Ids = array (); // get all content
$ Ids = $ this-> getListIds (); // this method obtains all the content. the following red part corresponds to its method.
Foreach ($ ids as $ k => $ value ){
// Echo $ value ['catid'];
If (! File_exists (ROOT_PATH. 'Demo/') {// Check whether there is any folder in the root directory. if not, create the demo folder.
Mkdir (ROOT_PATH. 'Demo /');
}
If (! File_exists (ROOT_PATH. 'Demo/'. $ value ['catid']) {// Check 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;
}
// Obtain the page ID to be static
Function getListIds (){
$ SQL = "select * from {$ this-> tablepre} demo order by demoid asc ";
$ Rs = $ this-> db-> getAll ($ SQL );
If ($ rs ){
Return $ rs;
} Else {
Return false;
}
}
// Static content on a single page
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 previously used static page must execute the php file every time before it can be generated. now I write this file under the control of people. it is very convenient to generate it if you want to generate it, I hope this will bring convenience to you and wish you a pleasant job.