Home > Backend Development > PHP Tutorial > How to batch generate html in Smarty_PHP tutorial

How to batch generate html in Smarty_PHP tutorial

WBOY
Release: 2016-07-13 17:35:36
Original
993 people have browsed it

After some exploration, I finally found a method, write it down for everyone’s advice:
First add such a method to the Smarty class in smartt.class.php:
PHP code:
/ /Parameter 1: HTML file saving path, Parameter 2: written content
function MakeHtmlFile($file_name, $c)
{
if(!$fp = fopen($file_name, "wa" ))
{
echo "File writing failed!";                                                                                                                              

Question: All my articles call the news.tpl template, so how do I batch generate them?

Let’s take a look at news.php first
PHP code:

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 benefits of writing it like this? I can read the content as an array

So how to generate:
Look at the following code:
Very simple
PHP code:

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/news_".$v[0].".html",$s->fetch("news.html",null, null, false));
}

?>



It’s so easy!


PHP code:




<{$news.titles}>


< table width="800" border="0" align="center" cellpadding="0" cellspacing="0" style="border:solid #CCCCCC 1px">


< td height="25" colspan="6" align="center" bgcolor="#eeeeee"><{$news.titles}>


Author:
<{$news.author}>
;td width="134" align="left"><{$news.sj}>
  Keywords:
  <{$news.keyword}>

< ;tr>
Core tip:
<{$news.sumary}>


<{$news.contents}>






Summary: Mainly use smarty’s fetch method and file read and write operations


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508318.htmlTechArticleAfter some exploration, I finally found the method and wrote it down for your guidance: First, in smartt.class.php Smarty class adds such a method: PHP code: //Parameter 1: Save html file...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template