Php implements simple code separation template engine

Source: Internet
Author: User
Use the Replace function, namely the str_replace function of php, to Replace the keywords in the content read in the template file with the content in the variable, thus achieving simple template separation. Use the Replace function, namely the str_replace function of php, to Replace the keywords in the content read in the template file with the content in the variable, thus achieving simple template separation.
Template.htm:


% Title %


% Title %

%body%


 

Php file:
// Replace function is used to Replace the keyword in the content read from the template file with the content in the variable
Function Replace ($ row)
{
// Define the variable to replace
$ Title = "article title ";
$ Body = "this is the subject of the article ";
// Replace the keyword in the parameter
$ Row = str_replace ("% title %", $ title, $ row );
$ Row = str_replace ("% body %", $ body, $ row );
// Return the replaced result
Return $ row;
}
// Template file pointer
$ F_tem = fopen ("template.htm", "r ");
// Generated file pointer
$ F_new = fopen ("new.htm", "w ");
// Read the template file cyclically and read a row each time
While (! Feof ($ f_tem ))
{
$ Row = fgets ($ f_tem );
$ Row = Replace ($ row); // Replace the keyword in the read content
Fwrite ($ f_new, $ row); // write the replaced content to the generated HTML file
}
// Close the file pointer
Fclose ($ f_new );
Fclose ($ f_tem );
?>

Generate a new html page: new.html


Article title


Article title

Here is the subject of the article


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.