用smarty实现纯静态化的文件发布有两种方法,也就是纯HTML文件生成的方法,包括以下两种方法:
方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #b1b100">require_once</span><span style="color: #009900">(</span><span style="color: #0000ff">"./config/config.php"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_start" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_start</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$id</span><span style="color: #339933">=</span><span style="color: #000088">$_GET</span><span style="color: #009900">[</span>id<span style="color: #009900">]</span><span style="color: #339933">;</span>
<span style="color: #000088">$sql</span><span style="color: #339933">=</span><span style="color: #0000ff">"select * from table_name where id='<span style="color: #006699;font-weight: bold">$id</span>'"</span><span style="color: #339933">;</span>
<span style="color: #000088">$result</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_query" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_query</span></a><span style="color: #009900">(</span><span style="color: #000088">$sql</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$rs</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/mysql_fetch_object" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">mysql_fetch_object</span></a><span style="color: #009900">(</span><span style="color: #000088">$result</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">assign</span><span style="color: #009900">(</span><span style="color: #0000ff">"showtitle"</span><span style="color: #339933">,</span><span style="color: #000088">$rs</span><span style="color: #339933">-></span><span style="color: #004000">title</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">assign</span><span style="color: #009900">(</span><span style="color: #0000ff">"showcontent"</span><span style="color: #339933">,</span><span style="color: #000088">$rs</span><span style="color: #339933">-></span><span style="color: #004000">content</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">display</span><span style="color: #009900">(</span><span style="color: #0000ff">"content.html"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$this_my_f</span><span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_get_contents" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_get_contents</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
<a rel="noopener noreferrer nofollow" href="http://www.php.net/ob_end_clean" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">ob_end_clean</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$filename</span> <span style="color: #339933">=</span> <span style="color: #0000ff">"<span style="color: #006699;font-weight: bold">$id</span>.html"</span><span style="color: #339933">;</span>
tohtmlfile_cjjer<span style="color: #009900">(</span><span style="color: #000088">$filename</span><span style="color: #339933">,</span><span style="color: #000088">$this_my_f</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #666666;font-style: italic">// 文件生成函数</span>
<span style="color: #000000;font-weight: bold">function</span> tohtmlfile_cjjer<span style="color: #009900">(</span><span style="color: #000088">$file_cjjer_name</span><span style="color: #339933">,</span><span style="color: #000088">$file_cjjer_content</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_file" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_file</span></a> <span style="color: #009900">(</span><span style="color: #000088">$file_cjjer_name</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
<span style="color: #339933">@</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/unlink" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">unlink</span></a> <span style="color: #009900">(</span><span style="color: #000088">$file_cjjer_name</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//存在,就删除</span>
<span style="color: #009900">}</span>
<span style="color: #000088">$cjjer_handle</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/fopen" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">fopen</span></a> <span style="color: #009900">(</span><span style="color: #000088">$file_cjjer_name</span><span style="color: #339933">,</span><span style="color: #0000ff">"w"</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//创建文件</span>
<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_writable" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_writable</span></a> <span style="color: #009900">(</span><span style="color: #000088">$file_cjjer_name</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span> <span style="color: #666666;font-style: italic">//判断写权限</span>
<span style="color: #b1b100">return</span> <span style="color: #009900;font-weight: bold">false</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/fwrite" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">fwrite</span></a> <span style="color: #009900">(</span><span style="color: #000088">$cjjer_handle</span><span style="color: #339933">,</span><span style="color: #000088">$file_cjjer_content</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
<span style="color: #b1b100">return</span> <span style="color: #009900;font-weight: bold">false</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span> 
<a rel="noopener noreferrer nofollow" href="http://www.php.net/fclose" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">fclose</span></a> <span style="color: #009900">(</span><span style="color: #000088">$cjjer_handle</span><span style="color: #009900">)</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//关闭指针</span>
<span style="color: #b1b100">return</span> <span style="color: #000088">$file_cjjer_name</span><span style="color: #339933">;</span> <span style="color: #666666;font-style: italic">//返回文件名</span>
<span style="color: #009900">}</span>
<span style="color: #000000;font-weight: bold">?></span>

方法二:

smarty中有一个获取模板页内容方法fetch(), 它的声明原形是这样的:

1
2
3
4
5
6
<span style="color: #000000;font-weight: bold"><?php</span> 
 
<span style="color: #000000;font-weight: bold">function</span> fetch<span style="color: #009900">(</span><span style="color: #000088">$resource_name</span><span style="color: #339933">,</span> <span style="color: #000088">$cache_id</span> <span style="color: #339933">=</span> <span style="color: #009900;font-weight: bold">null</span><span style="color: #339933">,</span> 
  <span style="color: #000088">$compile_id</span> <span style="color: #339933">=</span> <span style="color: #009900;font-weight: bold">null</span><span style="color: #339933">,</span> <span style="color: #000088">$display</span> <span style="color: #339933">=</span> <span style="color: #009900;font-weight: bold">false</span><span style="color: #009900">)</span> 
 
<span style="color: #000000;font-weight: bold">?></span>

第一个参数为模板名称, 第二个参数为缓存的id, 第三个参数为编译id, 第四个参数为是否显示模板内容. 生成静态页我们就需要用到这个方法.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<span style="color: #000000;font-weight: bold"><?php</span> 
   <span style="color: #000088">$smarty</span> <span style="color: #339933">=</span> <span style="color: #000000;font-weight: bold">new</span> Smarty<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
  <span style="color: #666666;font-style: italic">//其它模板替换语法... </span>
 
   <span style="color: #666666;font-style: italic">//下面这句取得页面中所有内容, 注意最后一个参数为false </span>
  <span style="color: #000088">$content</span> <span style="color: #339933">=</span> <span style="color: #000088">$smarty</span><span style="color: #339933">-></span><span style="color: #004000">fetch</span><span style="color: #009900">(</span><span style="color: #0000ff">'模板名称.tpl'</span><span style="color: #339933">,</span> <span style="color: #009900;font-weight: bold">null</span><span style="color: #339933">,</span> <span style="color: #009900;font-weight: bold">null</span><span style="color: #339933">,</span> <span style="color: #009900;font-weight: bold">false</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
  <span style="color: #666666;font-style: italic">//下面将内容写入至一个静态文件 </span>
  <span style="color: #000088">$fp</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/fopen" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">fopen</span></a><span style="color: #009900">(</span><span style="color: #0000ff">'news.html'</span><span style="color: #339933">,</span> <span style="color: #0000ff">'w'</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
  <a rel="noopener noreferrer nofollow" href="http://www.php.net/fwrite" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">fwrite</span></a><span style="color: #009900">(</span><span style="color: #000088">$fp</span><span style="color: #339933">,</span> <span style="color: #000088">$content</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
  <a rel="noopener noreferrer nofollow" href="http://www.php.net/fclose" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">fclose</span></a><span style="color: #009900">(</span><span style="color: #000088">$fp</span><span style="color: #009900">)</span><span style="color: #339933">;</span> 
 
  <span style="color: #666666;font-style: italic">//OK, 到这里这个news.html静态页就生成了, 你可以处理你下一步的工作了 </span>
<span style="color: #000000;font-weight: bold">?></span>