Pain: The world's smallest and simplest PHP template engine (common version) _ php template

Source: Internet
Author: User
Tags php template tmp file
PHP is the world's smallest and simplest template engine ------ Pain (normal version). Unlike smarty, all variables are included in the {@}, which helps you understand the template engine. Package download

Pain. php
The code is as follows:
Class Pain
{
Public $ var = array ();
Public $ tpl = array ();
// This is the method to assign vars to the template
Public function assign ($ variable, $ value = null)
{
$ This-> var [$ variable] = $ value;
}
Public function display ($ template_name, $ return_string = false)
{
// First find whether the tmp file in tmp dir exists.
If (file_exists ("tmp/temp_file.php "))
{
Unlink ("tmp/temp_file.php ");
}
Extract ($ this-> var );
$ Tpl_content = file_get_contents ($ template_name );
$ Tpl_content = str_replace ("{@"," $ Tpl_content = str_replace ("@}", "?> ", $ Tpl_content );
// Create a file in the/tmp dir and put the $ tpl_contentn into it, then
// Use 'include 'method to load it!
$ Tmp_file_name = "temp_file.php ";
// $ Tmp is the handler
$ Tmp = fopen ("tmp/". $ tmp_file_name, "w ");
Fwrite ($ tmp, $ tpl_content );
Include "tmp/". $ tmp_file_name;
}
}
?>

Test. php
The code is as follows:
Require_once "Pain. php ";
$ Pain = new Pain ();
$ Songyu = "songyu nb ";
$ Zhangyuan = "zhangyuan sb ";
$ Pain-> assign ("songyu", $ songyu );
$ Pain-> assign ("zhangyuan", $ zhangyuan );
$ Pain-> display ("new_file.html ");
?>

New_file.html
The code is as follows:
Http://www.w3.org/TR/html4/strict.dtd>



New_file


{@ $ Songyu @}

{@ $ Zhangyuan @}

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.