Pain the world's smallest and simplest php template engine (normal edition) _php template

Source: Internet
Author: User
Tags php template tmp file
Package download

pain.php
Copy Code code as follows:

<?php
Class Pain
{
Public $var =array ();
Public $tpl =array ();
This is the assign VARs to the template
Public function assign ($variable, $value =null)
{
$this->var[$variable]= $value;
}
Public function display ($template _name, $return _string=false)
{
The 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.php
Copy Code code 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.html
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en"
"Http://www.w3.org/TR/html4/strict.dtd" >
<meta http-equiv=" Content-type "content=" text/ html Charset=utf-8 "/>
<title>new_file</title>
<body>
{@ $songyu @}< ;br/>
{@ $zhangyuan @}
</body>

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.