Two ways to generate HTML static pages in PHP

Source: Internet
Author: User
Tags fread html page php template

The two methods are briefly described below:

First, use the output control function of PHP to get the static page string, and then write to the new file.

Instructions for use:

1, the instantiation of

The code is as follows Copy Code

$cache = new Cache (); 2, set cache time and cache directory

$cache = new Cache ('/any_other_path/');

The first parameter is the number of cached seconds, and the second parameter is the cached path, configured as needed.
By default, the cache time is 3,600 seconds, and the cache directory is cache/

3. Read cache

The code is as follows Copy Code

$value = $cache->get (' Data_key '); 4, write Cache

$value = $cache->put (' Data_key ', ' data_value '); Full instance:

$cache = new cache ();

Data $key from the cache by reading the key value
$values = $cache->get ($key);

If no data is cached
if ($values = = False) {
Insert code here ...
Data written to the key value $key
$cache->put ($key, $values);
} else {
Insert code here ...
}
Cache.class.php

<?php
Class Cache {
Private $cache _path;//path for the cache
Private $cache _expire;//seconds that cache expires

Cache constructor, optional expiring time and cache path
Public Function Cache ($exp _time=3600, $path = "cache/") {
$this->cache_expire= $exp _time;
$this->cache_path= $path;
}

Returns the filename for the cache
Private Function FileName ($key) {
return $this->cache_path.md5 ($key);
}

Creates new cache files with the given data, $key = = Name of the cache, data the info/values to store
The public function is put ($key, $data) {
$values = serialize ($data);
$filename = $this->filename ($key);
$file = fopen ($filename, ' w ');
if ($file) {//able to create the file
Fwrite ($file, $values);
Fclose ($file);
}
else return false;
}

Returns cache for the given key
Public function Get ($key) {
$filename = $this->filename ($key);
if (!file_exists ($filename) | |!is_readable ($filename)) {//can ' t read the cache
return false;
}
if (Time () < (Filemtime ($filename) + $this->cache_expire)} {//cache for the key is not expired
$file = fopen ($filename, "R");/Read Data file
if ($file) {//able to open the file
$data = Fread ($file, FileSize ($filename));
Fclose ($file);
Return Unserialize ($data);//return the values
}
else return false;
}
else return False;//was expired your need to create new
}
}
?>


Second, using template generation

What is a template? If you have used the "Save as template" in Dreamwerver, you should know that the template is used to unify the style of things. It only allows you to modify a part of the page, and of course this "part" is determined by you. The template that this article says here is the meaning. (in addition, PHP template technology also includes phplib, smarty, etc., this is not what this article says)

The concept of the template combined with this article is more specific: The art of doing a page first, and then we take this page as a template (note that this template is not necessary to use the code such as EditRegion3, this code is dreamwerver to facilitate their own design and get the logo), Replace what we need to change in this template with a character that can be distinguished from HTML, such as "{title}", "[title]". You only need to replace the data with these strings when generating static pages. That's what the template means.

Steps:

1. Create a new PHP page and an HTML page [Template page]; Note: If the data is invoked from the database, the data is saved as an array and then recycled;
2. On the PHP page, open the HTML page-> read the contents of the HTML page-> replace the parameters-> New (open) a new HTML page-> to write the replacement content to the new file-> close new file-> build success;

The code is as follows Copy Code

$open = fopen ("template.htm", "R"); Open template file
$content = Fread ($open, FileSize ("template.htm")); Read the contents of the template file
Print_r ($content);
$content = Str_replace ("{title}", "Test title", $content);//Replace
$content = Str_replace ("{contents}", "Test Content", $content);

$newtemp = fopen ("1.htm", "w");//build, open a nonexistent (new) page in write mode
Fwrite ($newtemp, $content);//write what you just replaced to a new file
Fclose ($newtemp);
echo "Generation";

PHP Batch generation HTML test:

The code is as follows Copy Code

//Suppose the data transferred from the database is stored in two Dimension array $arr
$arr = Array ("News title One", "news content One"), Array ("News title Two", "News content two");  

foreach ($arr as $key =>$ Value) {
  $title = $value [0];
  $contents = $value [1];
 //echo $title. ". $contents. ';
  $path = $key. HTML ';
  $open = fopen ("template.htm", "R");//Open template file
  $handle = fread ($open, FileSize ("template.htm"));// Read template file contents

  $content = Str_replace ("{title}", $title, $handle);//replace
  $content = Str_replace ("{ Contents} ", $contents, $handle);

  $newtemp = fopen ($path, "w");//Open a nonexistent (new) page by writing
 fwrite ($newtemp, $content);//write what you just replaced into a new file
 fclose ($newtemp);
 echo "Build";
}

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.