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.
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.
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 @}