Pain: the world's smallest and simplest PHP template engine (Basic Edition)

Source: Internet
Author: User
Tags php template tmp file

Package download

Pain. phpCopy codeThe Code is as follows: <? Php
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 ("{@", "<? Php echo ", $ tpl_content );
$ 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. phpCopy codeThe Code is as follows: <? Php
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.htmlCopy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 // EN"
Http://www.w3.org/TR/html4/strict.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml" lang = "en">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> new_file </title>
</Head>
<Body>
{@ $ Songyu} <br/>
{@ $ Zhangyuan @}
</Body>
</Html>

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.