<p>asp生成静态页主要涉及三个方面:模板,参数,fso.</p> <p>asp生成静态页主要涉及三个方面:模板,参数,fso.</p> <p>1,模板:这个其实就是页面的框架,以下为模板的例子:<br /> ###############################这就是一个模板###############################<br /> &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>&quot;&gt;<br /> &lt;html xmlns=&quot;<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>&quot;&gt;<br /> &lt;head&gt;<br /> &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt;<br /> &lt;title&gt;$title$&lt;/title&gt;<br /> &lt;link href=&quot;/Blog/css/style1.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;<br /> &lt;meta name=&quot;description&quot; content=&quot;$desc$&quot; /&gt;<br /> &lt;meta name=&quot;keywords&quot; content=&quot;$keywords$&quot; /&gt;<br /> &lt;/head&gt;<br /> &lt;body&gt;<br /> &lt;!--头部--&gt;<br /> $top$ ////这些就是替换参数<br /> &lt;!--头部结束--&gt;<br /> &lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;760&quot; style=&quot;margin-top:2px;&quot;&gt;<br /> &lt;tr&gt;<br /> &lt;td width=&quot;160&quot; valign=&quot;top&quot; bgcolor=&quot;#eeeef2&quot; style=&quot;border-right:1px #ddd solid&quot;&gt;<br /> $web_i$ ////这些就是替换参数 <br /> &lt;/td&gt; <br /> &lt;!--右侧专题介绍--&gt;<br /> &lt;td valign=&quot;top&quot; bgcolor=&quot;#eeeef2&quot; style=&quot;width:265px;height:20px;border-left:1px #fff solid;border-right:1px #ccc solid&quot;&gt; <br /> $pic$ ////这些就是替换参数<br /> &lt;/td&gt;<br /> &lt;td valign=&quot;top&quot; style=&quot;border-left:1px #fff solid&quot; bgcolor=&quot;#eeeef2&quot;&gt;<br /> $rec_text$ ////这些就是替换参数 <br /> &lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;/table&gt;<br /> &lt;!--专栏结束--&gt;<br /> $b_ban$ ////这些就是替换参数<br /> &lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;<br /> &lt;tr&gt;<br /> &lt;td width=&quot;540&quot; valign=&quot;top&quot; style=&quot;border-right:1px #ccc solid&quot;&gt;<br /> $c_list$ ////这些就是替换参数<br /> &lt;/td&gt;<br /> &lt;td width=&quot;218&quot; valign=&quot;top&quot; style=&quot;border-left:1px #fff solid&quot; bgcolor=&quot;#eeeef2&quot;&gt;<br /> $focus_text$ ////这些就是替换参数<br /> &lt;/td&gt;<br /> &lt;/tr&gt;<br /> &lt;/table&gt;<br /> $link$ ////这些就是替换参数<br /> $bottom$ ////这些就是替换参数<br /> &lt;/body&gt;<br /> &lt;/html&gt;#########################结束##################</p> <p><br /> 我们在制作动态页面时,也是先做好静态框架,然后把动态内容放进去,而模板就是未放进动态内容的页面,而这些动态内容都被参数替换了,就像上面的模板中:$top$,$web_i$,$pic$,$rec_text$,$c_list$,$focu_text$,$link$,$bottom$,这些参数是自己定义,你可以写成任何形式,不过最好是用字符加字母,这些参数是原本要显示动态内容的地方.<br /> 如果不用模板的话,那就需要在动态页面中制作了,比如top=top&quot;要输出的内容&quot;&amp;vbcrlf--(&amp;vbcrlf,就是让代码另起一行),这样做是比较麻烦的,因为原本的静态框架也要写在里面.<br /> 如果是模板生成的话,当然这些模板都是要存入数据库的,首先要从数据库中取出模板:<br /> ##############取栏目页面模板####################<br /> set rs_m=server.createobject(&quot;adodb.recordset&quot;)<br /> sql=&quot;select content from mb where id=2&quot;<br /> rs_m.open sql,conn,1,1<br /> main=rs_m(&quot;content&quot;)<br /> rs_m.close<br /> set rs_m=nothing</p> <p>然后就是逐个替换模板中的参数,例如以下;<br /> #####################原本的动态内容#############<br /> response.write &quot;&lt;div class=&quot;&quot;badv&quot;&quot;&gt;&quot;<br /> set rs_b=server.CreateObject(&quot;adodb.recordset&quot;)<br /> sql=&quot;select * from add_banner where p_column='&quot;&amp;column&amp;&quot;' and isselect='y' and p_size='760*80'&quot;<br /> rs_b.open sql,conn,1,1<br /> if rs_b.eof and rs_b.bof then<br /> response.write &quot;&lt;img src=&quot;/Blog/&quot;images/adv3.gif&quot;&quot; width=&quot;&quot;760&quot;&quot; height=&quot;&quot;80&quot;&quot; /&gt;&quot;<br /> else<br /> response.write &lt;img src=&quot;&quot;&quot;&amp;rs_b(&quot;picture&quot;)&amp;&quot;&quot;&quot; width=&quot;&quot;760&quot;&quot; height=&quot;&quot;80&quot;&quot; /&gt;&quot;<br /> end if<br /> response.write &quot;&lt;/div&gt;&quot;&amp;vbcrlf<br /> ######################生成静态页定义的动态内容############<br /> b_ban=&quot;&lt;div class=&quot;&quot;badv&quot;&quot;&gt;&quot;<br /> set rs_b=server.CreateObject(&quot;adodb.recordset&quot;)<br /> sql=&quot;select * from add_banner where p_column='&quot;&amp;column&amp;&quot;' and isselect='y' and p_size='760*80'&quot;<br /> rs_b.open sql,conn,1,1<br /> if rs_b.eof and rs_b.bof then<br /> b_ban=b_ban&amp;&quot;&lt;img src=&quot;/Blog/&quot;images/adv3.gif&quot;&quot; width=&quot;&quot;760&quot;&quot; height=&quot;&quot;80&quot;&quot; /&gt;&quot;<br /> else<br /> b_ban=b_ban&amp;&quot;&lt;img src=&quot;&quot;&quot;&amp;rs_b(&quot;picture&quot;)&amp;&quot;&quot;&quot; width=&quot;&quot;760&quot;&quot; height=&quot;&quot;80&quot;&quot; /&gt;&quot;<br /> end if<br /> b_ban=b_ban&amp;&quot;&lt;/div&gt;&quot;&amp;vbcrlf<br /> ######################################</p> <p>从上面可以看出,原来的response.write被替换为b_ban=b_ban&amp;,接下来就需要replace来替换模板中的参数了,我们把模板赋给了main,在模板中我们定义的参数为$b_ban$,那么加上下面的话:<br /> main=replace(main,&quot;$b_ban$&quot;,b_ban),b_ban就是动态内容,b_ban是可以随便定义的,你也可以定义为ban,那么main=replace(main,&quot;$b_ban&quot;,ban),这里字义为b_ban是为了更清晰些.<br /> 其它的参数替换跟这个道理是一样的.当把模板中所有的参数都替换完以后,就需要fso来生成了.<br /> fso是iis内置的组件,所以只要安装了iss,就可以使用fso,它的定义格式如下:<br /> set fso(这个也是自己定义,在这里我把它定义为fso)=server.createobject(&quot;scripting.filesystemobject&quot;) //这样就定义了fso<br /> set fout=fso.createtextfile(server.mappath(&quot;index.htm&quot;)) //这里是定义fout在当前的服务器上生成名为index.htm(后缀也是自己定义的,可以是html,sthml等静态后辍)的文件,这个文件目前是空白.<br /> fout.write main //写入静态页面的内容,这样index.htm页面就算是完全生成了,<br /> fout.close //关闭fout<br /> set fout=nothing //清空fout<br /> set fso=nothing //清空fso<br /> 这样从模板生成的静态页面就完成了.<br /> 如果不使用模板的话,那就不必使用replace而是直接使用fout.wirte.<br /> 例如我们在这个页面有top,body,bottom三个参数,那就是<br /> set fso(这个也是自己定义,在这里我把它定义为fso)=server.createobject(&quot;scripting.filesystemobject&quot;) //这样就定义了fso<br /> set fout=fso.createtextfile(server.mappath(&quot;index.htm&quot;)) //这里是定义fout在当前的服务器上生成名为index.htm(后缀也是自己定义的,可以是html,sthml等静态后辍)的文件,这个文件目前是空白.<br /> fout.write top //写入静态页面的内容<br /> fout.write body //写入静态页面的内容<br /> fout.write bottom//写入静态页面的内容<br /> fout.close //关闭fout<br /> set fout=nothing //清空fout<br /> set fso=nothing //清空fso<br /> 这样直接生成静态页面就完成了.</p>
返回顶部 留言