smarty shares two methods to implement PHP staticization
- 2020-05-06 12:00:57
- OfStack
Method 1:
<?php
require_once("./config/config.php");
ob_start();
$id=$_GET[id];
$sql="select * from table_name where id='$id'";
$result=mysql_query($sql);
$rs=mysql_fetch_object($result);
$smarty->assign("showtitle",$rs->title);
$smarty->assign("showcontent",$rs->content);
$smarty->display("content.html");
$this_my_f= ob_get_contents();
ob_end_clean();
$filename = "$id.html";
tohtmlfile_cjjer($filename,$this_my_f);
// File generation function
function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content){
if (is_file ($file_cjjer_name)){
@unlink ($file_cjjer_name); // Exist, delete
}
$cjjer_handle = fopen ($file_cjjer_name,"w"); // Create a file
if (!is_writable ($file_cjjer_name)){ // Determine write permission
return false;
}
if (!fwrite ($cjjer_handle,$file_cjjer_content)){
return false;
}
fclose ($cjjer_handle); // Close the pointer
return $file_cjjer_name; // Return file name
}
?>
Method 2:
There is a method fetch() in smarty to get the template page content, and its declaration prototype looks like this:
<?php
function fetch($resource_name, $cache_id = null,
$compile_id = null, $display = false)
?>
The first parameter is the template name, the second parameter is the cached id, the third parameter is the compiled id, and the fourth parameter is whether to display the template content
<?php
$smarty = new Smarty();
// Other template replacement syntax ...
// The following sentence captures all the content on the page , Notice that the last parameter is false
$content = $smarty->fetch(' Template name .tpl', null, null, false);
// Next, write to a static file
$fp = fopen('news.html', 'w');
fwrite($fp, $content);
fclose($fp);
//OK, So here we go news.html The static page is generated , You can take care of your next work
?>