Example of using Smarty to generate static files in PHP

Source: Internet
Author: User
Tags foreach mkdir php file

  First have to populate the template with content that needs to be static

The   code is as follows: #eg. This is the static homepage   function Staticindex () {  $newslist = $article->getarticles (null,54, ' DESC ', 1,6,false,1,2, ", 0,0,1);    if ($newslist) {    foreach ($newslist as $k => $v) {      $newslist [$k] [' Title_ All '] = $v [' title '];       $newslist [$k] [' title '] = Cutstr ($v [' title '],36, ' ... ');    }     $smarty->assign (' newslist ', $newslist);   &nbsp   $content = '; $content = $smarty->fetch (' index.html ', true);//This is the function of the smarty generated static page $static _name = root_path. ' Index.html '; This is to generate a static page the current path file fopen ($static _name, ' a ');//Open this file @file_put_contents ($static _name, $content);/finally write return true;  //Static list page, different by category by row static   function staticcontent () {///required static words of the number   $ids = array ();/Get all content     $ids = $this->getlistids ();//This method gets all the content, the following red Word part corresponds to its method    foreach ($ids as $k => $value) {   //echo $ value[' catid '];     if (!file_exists (root_path. ' demo/')) {//Judge root directoryFace has no this folder, if not then create demo this folder      mkdir (root_path. ' demo/');    }      if (!file_exists (root_path. ' demo/'. $value [' catid ']) {//Determine if there is a corresponding category folder under this folder       mkdir (root_path. ' demo/'. $value [' catid ']);      }       $html _content = $this->getdemocontent ($value [' demoid ']);       $static _name = root_path. ' demo/'. $value [' catid ']. /'. $value [' demoid ']. HTML ';      fopen ($static _name, ' a ');       @file_put_contents ($static _name, $html _content);    }   return true;  }    //Take out the page ID function that needs to be statically getlistids () {    $sql = "Select * from {$this->tablepre}demo o Rder by demoid ASC ";     $rs = $this->db->getall ($sql);    if ($rs) {     return $rs    }else{   return false;    }} &nbs P Content Single page static function Getdemocontent ($id) {   global $smarty, $view _templAtes, $admin _templates;    loadmodel (' demo ');     $demo = new demo ();     $content = ';     $smarty->template_dir = Root_path $view _templates;     $getMobanOne = $this->getmobandetail ($id);     $mobandetail = $demo->mobanlist ($id);     foreach ($mobandetail as $k => $v) {        $smarty->assign ($k, $v);     &NB Sp &NBSP}     $this->catid = $getMobanOne [' catid '];     $smarty->assign (' Pre_title ', $mobandetail [' membername ']);     $smarty->assign (' Mobandetail ', $mobandetail);     $content = $smarty->fetch (' demo_show.html ', true);     $smarty->template_dir = Root_path $view _templates;    return $content;       used to generate static page every time to execute PHP file, and then to generate, now I write this is controlled by people, want to generate on the generation, very convenient, hope to bring convenience to everyone, I wish you a happy work.  

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.