Example of using smarty to generate static files in PHP. First, we need to fill in the content to be static in the template to copy the code as follows: # eg. this is the functionstaticIndex () {$ newslist $ article-getArticles (nul first needs to fill the content to be static in 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.
Example code: # eg. this is the function staticIndex () {$ newslist = $ article-getArticles (nul...