您的位置 首页 php

PHP使用模板的简单例子

这里包含3个文件:test.php执行文件、parser.php编译类、temp.tpl模板

test.php

$user = ‘ZhangRongQuan’;

$method = ‘What are you doing?’;

//导入类文件

require_once ‘./parser.php’;

//创建对象

$cms = parser::inc();

//往数组添加元素

$cms->assign(‘user’, $user);

//往数组添加元素

$cms->assign(‘method’, $method);

//编译模板文件并载入php或者缓存文件

$cms->display(‘./temp.tpl’);

parser.php

class parser{

private

//用数组来存放模板变量的内容,键值就是变量名

$arr = array(),

//静态的缓存文件

$fn,

//编译文件

$pn;

private

static $int;//用于单例

//初始化传入的参数是模板文件

protected function __construct(){

$fname = ‘c_’ . md5(‘parser’);

//静态的缓存文件

$this->fn = ‘./’ . $fname . ‘.htm’;

//编译文件

$this->pn = ‘./’ . $fname . ‘.php’;

}
//单例

public static function inc(){

if(!self::$int instanceof self)

self::$int = new self();

return self::$int;

}

//给数组赋值,键值就是变量名

public function assign($key, $val){

if(!!$key)

//给模板变量赋值,key是变量名

$this->arr[$key] = $val;

//****************************************

//用数组来接收变量值,可以在后面的编译替换时,

//让模板的变量与值相对应,如果直接使用变量

//来接收,会造成模板里所有的变量的值都一样

//*****************************************

}

//加载文件,并根据条件创建缓存文件

public function display($cname){

//获取当前调用本类的文件的文件名

$currentFile = ‘./’ . basename($_SERVER[‘PHP_SELF’]);

//***********************************************

//如果存在缓存文件,并且缓存文件修改的时间比编译

//文件晚,并且编译文件修改的时间比当前文件晚,则

//直接载入缓存文件

//***********************************************

$fn = $this->fn;

$pn = $this->pn;

if(file_exists($fn) && $this->copTime($fn, $pn) && $this->copTime($pn, $currentFile)){

//测试用,看条件是否成立,条件成立即相关内容没有发生变化

echo rand();

//内容不变时直接载入缓存文件,无需重写缓存文件

include_once $fn;

}else{

echo ‘<p>我进行了编译</p>’;

//编译PHP文件

$this->par($cname);

//载入编译文件

include_once $pn;

//内容发生了变化,要重写缓存文件

file_put_contents($this->fn, ob_get_contents());

}

}

//判断文件的修改时间

private function copTime($an, $dn){

return filemtime($an) >= filemtime($dn);

}

//编译PHP文件

private function par($cname){

//导入模板文件,获取文件的内容

$conts = file_get_contents($cname);

//正则表达式

$reg = ‘/\{\$([\w]+)\}/’;

//替换

if(preg_match($reg, $conts)){

$conts = preg_replace($reg, “<?php echo \$this->arr[‘$1’] ?>”, $conts);

//********************************************************

// \$this->arr[‘$1’]里的[‘$1’]可以写成[‘\\1’]表示正则里的第一个分组,

// 就是正则的()里的内容(这是正则的知识),前面使用数组在这

//里就可以保证变量值是对应我们所要的,比如,模板里的变量是

//{$user},那这所替代的是$this->arr[‘user’],而数组arr的user元素恰

//好对应的值是$user,这个在assign方法里已经赋值过了

//********************************************************

}

//创建PHP编译文件

file_put_contents($this->pn, $conts);

}

}

temp.tpl

<h1>{$user}</h1>

<h2>{$method}</h2>

文章来源:智云一二三科技

文章标题:PHP使用模板的简单例子

文章地址:https://www.zhihuclub.com/77597.shtml

关于作者: 智云科技

热门文章

评论已关闭

26条评论

  1. Please send any info that can be of help to me and many others whom I know that have and have had breast cancer

  2. Genotoxicity of acetaldehyde and crotonaldehyde induced 1, N2 propanodeoxyguanosine DNA adducts in human cells, Mutat 3, respectively; p 0

  3. To find a novel inhibitor, we have used de novo fragment based drug design using FDA approved antiviral molecules like ascorbate, ribavirin, hydroxychloroquine and lopinavir, which showed good interaction with the RBD ACE2 complex During treatment, perfusion increased significantly in UIQ after 11 15 fractions, p 0

  4. PS3, amino acids 144 162 appeared to be bipartite NLS and PS4, amino acids 242 257, was predicted to be a bipartite NLS Figure 2B levitra montelukast 4mg granules sachets sugar free This permanent, market based plan makes students loans cheaper, simpler and more certain, said Alexander, the senior Republican on the U

  5. BRCA1 and BRCA2 related cancers often test negative for overexpression of the gene known as HER2 neu

  6. There were no socio demographic differences in uptake, suggesting that the introduction of breast cancer preventive therapy is unlikely to create socioeconomic inequalities in cancer incidence After 1 month, the tumors were removed and analyzed for size, histology, and proliferation

  7. p62 Sqstm1 promotes malignancy of HCV positive hepatocellular carcinoma through Nrf2 dependent metabolic reprogramming

  8. A diagnostic approach to the evaluation of abnormal uterine bleeding in postmenopausal women is outlined in Figure 3

  9. Bilateral oophorectomy and, often, hysterectomy are performed in many postmenopausal women with ovarian cysts, because of the increased incidence of neoplasms in this population

  10. My gyno was bad and didn t have the DHT cream gel on hand and to not finding out about it until 3 4 weeks into two different treatment plans C_Binos and mine Epidemiology and clinical features of Mediterranean spotted fever in Italy

  11. After it is applied to the skin, mupirocin kills existing bacteria and prevents their growth Quentin, USA 2022 06 28 06 23 53

  12. Participants were monitored from the start of first tamoxifen prescription for MBC until death, or until the end of the study period July 2009, whichever came first

  13. mRNA expression of ERОІ, PI3K, and AKT was dramatically reduced by tamoxifen treatment in lung tissues compared with the estrogen group

  14. To our knowledge, our study is the first in SSA to show that ASP occur not only after receiving treatment but may also be present prior to treatment

  15. Local estrogen therapy is generally preferred for vaginal dryness dyspareunia, since its efficacy is targeted to vaginal tissues, with minimal systemic absorption and fewer adverse effects, noted Dr

  16. High glycemic index do walnuts help lower blood sugar meals spike your blood sugar quickly, whereas low GI foods have the least impact on blood sugar While the GI has long been promoted as a tool to help handle blood sugar, there are some notable drawbacks As with any healthy eating program, a diabetic diet is extra about your total dietary sample quite than obsessing over specific foods Aim

  17. s entire surface though each would only be able to communicate with one receptor at a time

网站地图