Originally thinkphp with the generation of static page function buildhtml, easy to use! This method is not written in the latest handbook, so let's introduce it to you.
protected function buildhtml ($htmlfile = ", $htmlpath =", $templateFile = ") { $content = $this->fetch ($templateFile); $htmlpath = !empty ($htmlpath)? $htmlpath: html_path; $htmlfile = $htmlpath. $htmlfile. C (' Html_file_suffix '); if (!is_dir (dirname ($htmlfile))) // Create If the static directory does not exist mk_dir (DirName ($htmlfile)); if (False === file_put_contents ($htmlfile, $content)) throw_exception (L (' _cache_write_error_ '). ': '. $htmlfile); return $content; } |
How to use:
$this->buildhtml (' Static file ', ' Static path ', ' template file '); |
parameter one : Static files, refers to the generated static file name, the file save path complete is: Static path/static file. For example, static file Settings A/index. Then the saved path is the item
Target path/html/a/index.html (default static path under the Html folder of the project path, no can be created by itself)
parameter two : Static path, the above has explained the default path, 3.0 can add parameters in the Portal file to change the static path.
Define (' Html_path ', './'); (the static path is defined as the Web site root directory)
parameter three : template file, I feel the official description is not correct, it should be the target module, is the module that needs to generate static files. Format: module name: operation. For example, to generate index under the A method is a static file, that is index:a. If empty, the static file for the current operation is generated by default.
To generate a static page, we want to select a static template page, the principle of static Web page is to replace the variables in the template, so we need to replace the value of the variable to get out , such as
$this->assign (' WebName ', $websites [' name ']), $this->assign (' SiteID ', $siteid); $this->assign (' type ', $type ); $this->assign (' header ', $websites [' templatedirectory ']. ' @Public_header '); $this->assign (' footer ', $websites [' templatedirectory ']. ' @Public_footer '); |
The static template page has the corresponding {$header},{$footer}, and so on, after using the Buildhtml method, the variables are replaced. is not very simple.
Static Page method for thinkphp