The static technology of PHP template

Source: Internet
Author: User

The static of the PHP page is very necessary, especially in the CMS system, some content once generated, basically will not change, then if the page is static with HTML, it will undoubtedly reduce the burden of Service parsing PHP page. The following is a book to learn about the static PHP technology, record for a rainy-night.

Whether using a framework or a simple script, the principle is basically the same: using PHP for file manipulation, replacing dynamic elements in HTML templates.

A simple example:

1. Create a template (template.html)

<title> an article </title>
<body>
<div id = "headline" >%headline%</div>
<div id = "Content" >%content%</div>
</body>


Template is very simple, to fill the 2 data is the article title (%headline%), the article content (%content%), these things are so-called dynamic elements.

2. Static script (tostatic.php)

Code <?php

The Replace function is used to replace the keywords in the content read from the template file with the contents of the variable

function Replace ($row, $headline = ", $content =")

{

Replace a keyword in a parameter

$row = Str_replace ("%headline%", $headline, $row);

$row = Str_replace ("%content%", $content, $row);

Returns the replaced result

return $row;

}


Main program


$connection = mysql_connect ("localhost", "username", "password") or Die (Mysql_error ());

$database = mysql_select_db ($connection, "dbname") or Die (Mysql_error ());


Newly added article Information

$headline = $_post[' headline ');

$content = $_post[' content '];


Generate file name, use date time here

$filename = ' S '. Date ("Ymdhis"). HTML ';

Execute SQL statement

$sql = "INSERT into news values (' $headline ', ' $content ', ' $filename ')";

$res = mysql_query ($sql);


Determines whether the insert succeeds based on the BOOL type variable returned by the SQL execution statement

if ($res)

{

Template file pointer

$f _tem = fopen ("template.html", "R");

The generated file pointer

$f _new = fopen ($filename, "w");

Iterate through a template file, reading one line at a time

while (!feof ($f _tem))

{

$row = fgets ($f _tem);

Replace keywords in read-in content

$row = Replace ($row, $headline, $content);

Writes the replaced content to the generated HTML file

Fwrite ($f _new, $row);

}

Close file pointer

Fclose ($f _new);

Fclose ($f _tem);

Tips

echo "ok!";

}

Else

echo "failed!";

Mysql_close ();

?>


3. The general CMS will record the content is browsed information, such as the number of views or the browser's IP information, static pages to record this information, you can add a long and wide 0 in the template picture, point to the Count script.

Take the number of records viewed as an example:

In this way, the count can be placed in counter.php without disrupting the static nature of the page.

The static technology of PHP template

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.