如何动态生成HTML页面

html-css09

如何动态生成HTML页面,第1张

以最简单的新闻网页为例,先是做一个模板页,假定其中3个地方要动态更换的:标题,内容,日期,哪么模板中这3个地方都要用特殊的名称表示。

要生成html页时,先读入html模板页,再查询数据库,把查询到标题,内容,日期的实际值替换模板中的特殊名称,并按规则命名另存这个文件。

以上是动态生成html页的基本原理,实际工作中,可灵活处理,比如一次性生成所有页,或者发布一个生成一个,可以用一个模板,也可以根据类别用3个5个模板。模板可以保存为文件文件,也可保存在数据库中等等。

?

对于 vue 来说,模板本质就是一个字符串

vue 中的模板是有逻辑的,是动态的,如 v-if、v-for 等

与 html 格式很像,但有很大区别;html 是静态的,而 vue 模板是动态的

最终还是要转化为 html 来显示,怎么才能转换为 html 来显示呢?

模板最终必须转换为 JS 代码?

因为模板有逻辑 (v-if,v-for),必须用 JS 才能实现(前端中只有 JS 是图灵完备语言)

转换为html渲染页面,必须用 JS 才能实现渲染

因此模板最终要转换为一个 JS 函数(render 函数),(render 函数是指的渲染函数,并不一定就必须是 render 这个名字)

render函数

render 函数的 with 的用法

模板中,所有信息都包含在了 render 函数中

this 即 vm

price 就是 this.price,也是 vm.parice,也是 data.price

1、ASP文件中的代码

pencat=rs.Fields.Item("m_content").Value

pencat=replace(pencat,"t_title",n_title)

pencat=replace(pencat,"t_author",n_author)

pencat=replace(pencat,"t_content",n_content)

Set fso = Server.CreateObject("Scripting.FileSystemObject")

Set fout = fso.CreateTextFile(server.mappath(fpath&"\" &fname))

fout.WriteLine pencat

fout.close

2、如下给出要生成的网页模板:

<html>

<head>

<meta http-equiv=""Content-Language"" content=""zh-cn"">

<meta http-equiv=""Content-Type"" content=""text/htmlcharset=gb2312"">

<meta name=""GENERATOR"" content=""Microsoft FrontPage 4.0"">

<meta name=""ProgId"" content=""FrontPage.Editor.Document"">

<title></title>

</head>

<body topmargin=""0"" leftmargin=""0"">

<table border=""0"" width=""760"" height=""100%"" background=""background.jpg"" >

<tr>

<td width=""752"" height=""10"" colspan=""3"">

<p align=""center"">t_title

</td>

</tr>

<tr>

<td width=""752"" height=""18"" colspan=""3"">

<div align=""center"">

</div>

<div align=""center"">

<font size=""2"">

作者:</font><font color=""#990000"">t_author</font>

<font size=""2"">

加入时间:</font><font color=""#990000"">t_date</font>

</div>

</td>

</tr>

<tr>

<td width=""15%"" height=""100%"" valign=""top"">

</td>

<td width=""70%"" height=""100%"" valign=""top"">

t_content

</td>

<td width=""15%"" height=""100%"" valign=""top"">

</td>

</tr>

</table>

</body>

</html>

3、解释

(1)pencat=rs.Fields.Item("m_content").Value

pencat为一个字符串变量。

rs.Fields.Item("m_content").Value就是如上2、网页模板的全部HTML字符

(2)pencat=replace(pencat,"t_title",n_title)

pencat=replace(pencat,"t_author",n_author)

pencat=replace(pencat,"t_content",n_content)

以上三句就是将字符串中的字串替换成为你所需要的内容,即ASP中动态获得的内容。

(3)Set fso = Server.CreateObject("Scripting.FileSystemObject")

Set fout = fso.CreateTextFile(server.mappath(fpath&"\" &fname))

fout.WriteLine pencat

fout.close

以上为将刚刚组合所得的网页代码写入文件的过程。第一句定义fso文件,第二句创建输出流文件,其中fpath为你想要存储的文件的路径,fname为文件名;后两句是将字符串写入文件和关闭输出流文件。