ThinkPHP提供了灵活的静态文件生成功能,可以在输出模板的同时生成需要的静态文件. 使用:
$this->buildHtml('静态文件', '静态路径','模板文件');
静态路径如果留空的话 默认保存在HTML_PATH(默认的HTML_PATH路径位于项目目录下面的Html目录,如果没有的话手动创建)定义的路径下面,,静态文件可以随意设置,也可以包括路径,如果不存在的路径系统会自动创建。
看下TP的buildhtml的源文件Action.class.php:
1
2
3
protected function buildHtml( $htmlfile = '' , $htmlpath = '' , $templateFile = '' , $charset = '' , $contentType = 'text/html' ) {
return $this -> view -> buildHtml ( $htmlfile , $htmlpath , $templateFile , $charset , $contentType ) ;
}
View.class.php类部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
+----------------------------------------------------------
* @ access public
+----------------------------------------------------------
* @ htmlfile 生成的静态文件名称
* @ htmlpath 生成的静态文件路径
* @ param string $templateFile 指定要调用的模板文件
* 默认为空 由系统自动定位模板文件
* @ param string $charset 输出编码
* @ param string $contentType 输出类型
+----------------------------------------------------------
* @ return string
+----------------------------------------------------------
*/
public function buildHtml( $htmlfile , $htmlpath = '' , $templateFile = '' , $charset = '' , $contentType = 'text/html' ) {
$content = $this -> fetch ( $templateFile , $charset , $contentType ) ;
$htmlpath = ! empty ( $htmlpath ) ?$htmlpath : HTML_PATH;
$htmlfile = $htmlpath . $htmlfile . C( 'HTML_FILE_SUFFIX' ) ;
if ( ! is_dir ( dirname ( $htmlfile ) ) )
// 如果静态目录不存在 则创建
mk_dir( dirname ( $htmlfile ) ) ;
if ( false === file_put_contents ( $htmlfile , $content ) )
throw_exception( L( '_CACHE_WRITE_ERROR_' ) ) ;
return $content ;
}
fetch类部分:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
+----------------------------------------------------------
* 加载模板和页面输出
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $templateFile 模板文件名 留空为自动获取
* @param string $charset 模板输出字符集
* @param string $contentType 输出类型
* @param string $display 是否直接显示
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
public function fetch( $templateFile = '' , $charset = '' , $contentType = 'text/html' , $display = false )
{
$GLOBALS [ '_viewStartTime' ] = microtime ( TRUE ) ;
if ( null === $templateFile )
// 使用null参数作为模版名直接返回不做任何输出
return ;
if ( empty ( $charset ) ) $charset = C( 'DEFAULT_CHARSET' ) ;
// 网页字符编码
header ( "Content-Type:" . $contentType . "; charset=" . $charset ) ;
header ( "Cache-control: private" ) ; //支持页面回跳
//页面缓存
ob_start ( ) ;
ob_implicit_flush ( 0 ) ;
if ( ! file_exists_case( $templateFile ) )
// 自动定位模板文件
$templateFile = $this -> parseTemplateFile ( $templateFile ) ;
$engine = strtolower ( C( 'TMPL_ENGINE_TYPE' ) ) ;
if ( 'php' == $engine ) {
// 模板阵列变量分解成为独立变量
extract ( $this -> tVar , EXTR_OVERWRITE) ;
// 直接载入PHP模板
include $templateFile ;
} elseif ( 'think' == $engine && $this -> checkCache ( $templateFile ) ) {
// 如果是Think模板引擎并且缓存有效 分解变量并载入模板缓存
extract ( $this -> tVar , EXTR_OVERWRITE) ;
//载入模版缓存文件
include C( 'CACHE_PATH' ) . md5 ( $templateFile ) . C( 'TMPL_CACHFILE_SUFFIX' ) ;
} else {
// 模板文件需要重新编译 支持第三方模板引擎
// 调用模板引擎解析和输出
$className = 'Template' . ucwords ( $engine ) ;
require_cache( THINK_PATH. '/Lib/Think/Util/Template/' . $className . '.class.php' ) ;
$tpl = new $className ;
$tpl -> fetch ( $templateFile , $this -> tVar , $charset ) ;
}
$this -> templateFile = $templateFile ;
// 获取并清空缓存
$content = ob_get_clean ( ) ;
// 模板内容替换
$content = $this -> templateContentReplace ( $content ) ;
// 布局模板解析
$content = $this -> layout ( $content , $charset , $contentType ) ;
// 输出模板文件
return $this -> output ( $content , $display ) ;
}
考虑的很全面.