Sample code:
require('libs/Smarty.class.php');
$tpl=new Smarty();
$tpl->template_dir='./templates/';
$tpl->compile_dir='./templates_c';
$tpl->config_di
Sample code: require('libs/Smarty.class.php'); $tpl=new Smarty(); $tpl->template_dir='./templates/'; $tpl->compile_dir='./templates_c'; $tpl->config_dir='./config/'; $tpl->cache_dir='./cache/'; $tpl->left_delimiter='right_delimiter='}>'; ob_start(); //Open the output buffer $tpl->assign('s_title',$_POST['title']);//Set the website title //The following is to accept the passed variables and assign them to the template page $tpl->assign("title",$_POST['title']); $tpl->assign("content",stripslashes($_POST['content'])); $tpl->assign("time",date("Y-m-d")); $tpl->display("tpl.html"); $this_my_f=ob_get_contents();//Read buffer data ob_end_clean();//Clear buffer data //------------------------Create folder--------------------- ------ $dir_name =date("Ymd"); //Use the current date to create the directory where the static page that should be generated will be stored. if (!is_dir("webpage/".$dir_name)) //First determine whether this directory has been created! If none, create this directory first { mkdir("webpage/".$dir_name); } $filename="tpl.html"; //-------------------------The path to save the static page------------------ -- if(tohtmlfile_cjjer($filename,$this_my_f)){ echo ("Page generated successfully"); }else{ echo ("") } ?> function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content) { //$dir_name =date("Ymd"); //Use the current date to create the directory where the static page that should be generated will be stored. //if (!is_dir($dir_name)) //First determine whether this directory has been created! If none, create this directory first //{ //mkdir($dir_name); //} if (is_file ($file_cjjer_name)){ @unlink ($file_cjjer_name); } $cjjer_handle = fopen ($file_cjjer_name,"w"); if (!is_writable ($file_cjjer_name)){ return false; } if (!fwrite ($cjjer_handle,$file_cjjer_content)){ return false; } fclose ($cjjer_handle); //Close the pointer return $file_cjjer_name; } The biggest function of Smarty is to cache template pages. That is to say, two steps can be completed through Smarty: compilation + parsing Step one: compile. It means to replace the tag of the template file with pure PHP, and then save it in the cache location. The saved file extension is PHP. I call this step compilation (this is my own name, not official) Step 2: Analysis. That is to say, it just parses and executes the PHP file you just compiled~~No need to explain this further. Let’s get to the point and add the following code to the Smarty.class.php file. function MakeHtmlFile($file_name, $content) { //Create the directory if it does not exist if (!file_exists (dirname($file_name))) { if (!@mkdir (dirname($file_name), 0777)) { die($file_name."Directory creation failed!"); } } if(!$fp = fopen($file_name, "w")){ echo "File opening failed!"; return false; } if(!fwrite($fp, $content)){ echo "File writing failed!"; fclose($fp); return false; } fclose($fp); chmod($file_name,0666); } The function of this function is to save the file~~ The calling method is as follows require '../libs/Smarty.class.php'; $smarty = new Smarty; //.........Omit variable definition and assignment //$smarty->display('index.tpl'); $content=$smarty->fetch("index.tpl"); $smarty->MakeHtmlFile('./index.html',$content);//Generate smarty generates static page summary: A method to separate templates when generating static pages The usual approach is to read the template and use regular expressions to replace the variables in the template with the values we want to generate a static page. After some expert guidance, it turned out that SMARTY has this function. After some research, it turned out to be very convenient and easy to use. The key points are as follows: ob_start();//Open buffer $smarty->assign(“a”,$a); $smarty->display("temp.html"); $html_content= ob_get_contents(); //Read the data in the buffer ob_end_clean();//Close the buffer The content in $htm_content is what you want, just write it to the page. |