Pain 全世界最小最简单的PHP模板引擎 (普通版)
2015-01-24信息快讯网
Pain.php
<?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.php
<?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
<!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>
基于PHP常用函数的用法详解
解析php DOMElement 操作xml 文档的实现代码
PHP 自定义错误处理函数的使用详解
PHP 类相关函数的使用详解
解析PHP缓存函数的使用说明
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
深入phpMyAdmin的安装与配置的详细步骤
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
解决phpmyadmin中缺少mysqli扩展问题的方法
基于ubuntu下nginx+php+mysql安装配置的具体操作步骤
PHP禁止页面缓存的代码
供参考的 php 学习提高路线分享
PHP中的strtr函数使用介绍(str_replace)
PHP中读写文件实现代码
Array of country list in PHP with Zend Framework
php环境配置之CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI比较?
Admin generator, filters and I18n
linux iconv方法的使用
linux系统上支持php的 iconv()函数的方法
Sorting Array Values in PHP(数组排序)