After some exploration, I finally found a method and wrote it out for your reference:
First, add the following method to the Smarty class in smartt. class. php:
PHP code:
// Parameter 1: html file storage path; parameter 2: written content
Function MakeHtmlFile ($ file_name, $ c)
{
If (! $ Fp = fopen ($ file_name, "wa "))
{
Echo "file opening failed! ";
Return false;
}
If (! Fwrite ($ fp, $ c ))
{
Echo "file writing failed! ";
Fclose ($ fp );
Return false;
}
Fclose ($ fp );
}
Q: All my articles call the news. tpl template. How can I generate them in batches?
Let's take a look at news. php.
PHP code:
<? Php
Include_once ("config. php ");
Include_once ("init. php ");
$ S-> assign ("title", "all news categories ");
$ ID = $ _ GET ["ID"] + 0;
$ SQL = "select * from artical where newsID = $ ID ";
$ Rs = $ db-> fetch ($ SQL );
$ S-> assign ("news", $ rs ["rec"] [0]); // Note: $ rs ["rec"] [0] is an array
$ S-> display ("news.html ");
?>
The entire template variable is only $ news. What are the advantages of this writing? I can read content in arrays.
Then how to generate:
See the following code:
Very simple
PHP code:
<? Php
Include_once ("config. php ");
Include_once ("init. php ");
$ SQL = "select * from artical ";
$ Rs = $ db-> fetch ($ SQL );
Foreach ($ rs ["rec"] as $ k => $ v)
{
$ S-> assign ("news", $ v );
$ S-> MakeHtmlFile (". /news _". $ v [0]. ". html ", $ s-> fetch (" news.html ", null, null, false ));
}
?>
It's easy!
PHP code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "[url = require">
<Html xmlns = "[url = http://www.w3.org/5o/xhtml#http://www.w3.org/5o/xhtml#/url]">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> <{$ news. titles}> </title>
<Link href = "<{$ docroot}>/CSS/main.css" rel = "stylesheet" type = "text/css"/>
</Head>
<Body>
<Table width = "800" border = "0" align = "center" cellpadding = "0" cellspacing = "0" style = "border: solid # CCCCCC 1px">
<Tr>
<Td height = "25" colspan = "6" align = "center" bgcolor = "# eeeeee"> <strong ><{$ news. titles }></strong> </td>
</Tr>
<Tr>
<Td width = "74" height = "25" align = "right"> author: </td>
<Td width = "220"> <{$ news. author}> </td>
<Td width = "40" align = "right"> time: </td>
<Td width = "134" align = "left"> <{$ news. sj}> </td>
<Td width = "110" align = "right"> keywords: </td>
<Td width = "220" align = "left"> <{$ news. keyword}> </td>
</Tr>
<Tr>
<Td height = "25" align = "right"> core tips: </td>
<Td height = "25" colspan = "5" class = "artical" style = "border: double # FF0000 3px;" ><{$ news. sumary }></td>
</Tr>
<Tr>
<Td height = "25" colspan = "6" align = "left" class = "artical" style = "padding: 10px;" ><{$ news. contents }></td>
</Tr>
</Table>
</Body>
</Html>
Summary: The smarty fetch method and file read/write operations are mainly used.