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>