Static Page method for thinkphp

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.