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

How to batch generate html in Smarty_PHP tutorial

Jul 13, 2016 pm 05:35 PM
html smarty one time Write batch turn up method generate of

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:

<?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 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:

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

?>



It’s so easy!


PHP code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1 /DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html xmlns="[url=http ://www.w3.org/1999/xhtml]http://www.w3.org/1999/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="134" align="left"><{$news.sj}></td>
  <td width="110" align="right">Keywords: </td>
  <td width="220" align="left"><{$news.keyword}></td>
</tr>
&lt ;tr>
<td height="25" align="right" >Core tip: </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: 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...

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

Nested Table in HTML

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Table Border in HTML

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

HTML margin-left

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Moving Text in HTML

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

HTML Ordered List

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

HTML onclick Button

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

See all articles