发新话题
打印

从smarty复杂的语法中解放出来,执行速度要比smarty快20倍

从smarty复杂的语法中解放出来,执行速度要比smarty快20倍

[Copy to clipboard]
PHP CODE:
mctemplate (mini Compiled templdate)简介
mctemplate是一个支持大型WEB应用程序的模板引擎
.
mctemplate有什么特色
普通模板引擎工作方式你的PHP脚本指定一个HTML模板,指定动态内容并显示。模板分析器使用指派的内容替换模板内所有的占位符,然后显示给用户这意味着,每次你要输出一些内容,程序都要花上好多时间去进行字符串的处理和正则表达式的工作。
template 的工作方式类似模板编译,他把模板转换为可执行的PHP脚本,并且保存起来以备以后重用。当一个新的模板在第一次被使用时,The first time a 
new template is processed模板内所有的占位符被替换为简单的可输出指定内容的PHP代码元素。据个例子,模板片断 {$title},将被转换为 如果你指定内容给正确的变量将再也不需要进行模板分析了程序要做得仅仅是自己包含并执行便以后的模板这通常会戏剧性的减少模板引擎的运行时间.

mctemplate 支持:


" 普通变量替换 (字符串,等等)
重复的内容块 (嵌套数组BEGIN..END)
" 基本的逻辑控制结构 (IF..ELSEIF..ELSE)
可定制的扩展模块 (输出过滤大小写转换格式输出等等.)
" 模板编译 (HTML 模板被转换为可执行的PHP代码)
输出缓存 (重用输出页面从而提速你的程序)
" 生成静态文件
模板文件中、直接包含php代码,因此可以直接执行php函数
" 宏定义 






让我们用一些简单的例子开始,告诉你如何使用 template:

通常的流程是先建立一个WEB页面,展示所需要的效果. 创建简单的页面版式可以使用一个网页编辑器,例如Dreamweaver或者Homesite. 为了获得更为友好的页面版式效果,可以使用一个专业的设计工具,例如Photoshop、 Paintshop Pro或者Gimp等等.

预览:

Hello World!

现在我们来看看达到我们期望的页面版式的HTML源代码:

HTML-源代码 (hello_world.html):


Hello World!


然后开始令人激动的一步: 分离内容和设计. 我们把HTML源代码之中的内容元素替换为模板占位符. 模板占位符被分配一个唯一的名称并且使用一个特有的标签包含起来,这样我们在随后的程序之中可以识别他们. 标题 Hello World! 被替换为一个叫做TITLE 的占位符. 我们是用大括弧来标记占位符, 似的我们的模板引擎可以发现: {TITLE}. 最后HTML模板如下:

HTML-模板 (hello_world.tpl.html):


{$title}


使内容整合到模板,我们需要做如下工作:
调用 template 类 
" 创建一个 template 分析器 对象 
告诉 template 要使用的HTML模板 
" 指定内容给关联的占位符 
处理模板 
" 输出结果 




以下的PHP程序完成所需的工作:
hello_world.php:

<?php

require_once "
template.class.php";
$page = new template("
hello_world.tpl.html");
$title ='Hello World!';
include $page->compiledFile();

?>

就这么简单 – 我们的 Hello World 例程已经准备运行了. 

比Smarty要快上20倍!
mctemplate 直接支持php, php有多少函数就支持多少函数啦 支持多种功能和函数, 但是在你的程序运行期间,仅仅很少一部分时间用在模板处理上.
从从复杂的smarty学习中解放出来。 经测试一个hello 程序,mctemplate比smarty快4倍

文件包含程序比smarty 快20倍!! 







template 配置


string $template_dir
模板文件目录,默认为 ./templdates/ 
可使用全局变量$_CONFIG['template_dir']设置

string $temp_dir
模板编译保存目录,默认为./templates_c/
注意:确保PHP对该目录可写

string $cache_dir
输出缓存目录,默认为./cache/
注意:确保PHP对该目录可写

int $cache_lifetime
默认输出缓存周期


基本方法
常量表示
{name}被解析成,表示显示常量name的值,其中的“name”由英文字母、数字和下划线组成首字母必须是英文字母或者下划线。

例子1:标量赋值

<?php

$template=new template('template.html');

define('TITLE,'Sample Text');;

include $template->compiledFile();

?>

模板(template.html):

{TITLE} 

输出:

Sample Text 

例子2: 多个标量赋值

<?php

$template=new template('user.html');

$name='John Doe' ;
$group='Admin';
$age = '42' ;

include $template->compiledFile();

?>

模板(user.html):

Name: {$name}
Group: {$group}
Age:{$age}

输出:

Name:John Doe
Group: Admin
Age:42

例子3: 使用数组给多个标量赋值

<?php

$user=array(
'NAME'=> 'John Doe', 
'GROUP' => 'Admin',
'AGE'=> '42',
);

$template=new template('user.html');


include $template->compiledFile();

?>

模板(user.html):

Name:{$user['NAME']}
Group: {$user['GROUP']}
Age:{$user['AGE']}

输出:

Name:John Doe
Group: Admin
Age:42

例子4: 列表赋值(links.php)

<?php

$links=array(
array(
'TITLE' => 'PHP',
'URL'=> 'http://www.php.net/',
),
array(
'TITLE' => 'Apache',
'URL'=> 'http://www.php.net/',
),
array(
'TITLE' => 'MySQL',
'URL'=> 'http://www.mysql.com/',
),
);

$template=new template('links.html');

include $template->compiledFile();

?>

模板(links.html): 结构名称lnks对应数组


Sample Links 



[url=http://www.dophp.net/{$v[]{$v['TITLE']}[/url]







输出:


Sample Links 

[url=http:
//www.php.net/]PHP[/url]

[url=http://www.apache.org/]Apache[/url]

[url=http://www.mysql.com/]MySQL[/url]




例子5: 用缓存 (2_use_cache.php)

template::useCache
void useCache ( [cache file] )
激活内建的输出缓存. 判断当前执行的脚本 (判断依据$_SERVER[REQUEST URI]) 是否在确定的时间内执行过. 如果执行过, useCache 将返回缓存的页面给浏览器并且中止运行.
如果没有一个有效的输出句柄可以使用,useCache将激活PHP输出缓存,并且返回数据到执行它的脚本. 下面的脚本执行时, useCache 捕获所有输出到浏览器的内容,并保存到缓存目录. 缓存的每一个文件名称是唯一的,他根据当前执行的脚本文件名称,GET参数(REQUEST_URI)以及可选得参数来自东设定.
如果脚本有一些重要的工作,例如记录日志等,那么应该在use_cache 之前调用你的代码.
例子:

<?php

require 'template.class.php';

$t = new template('hello_world.tpl.html');
$t->useCache(); 
//active output cache 


$title = 'Hello yubing! '.date('Y-m-d H:i:s');

include $t->compiledFile();

?>


例子6: 生成静态页面 (gen_htm.php)

template::useCache
void useCache ( [html file name] )
为了获得更快效率,有时候我们需要将页面生成静态页面,直接访问静态页面速度会快很多!一下实例在页面生成了一个index.html的文件。
例子:

<?php

require 'template.class.php';

$t = new template('hello_world.tpl.html');
$t->useCache('htm/index.html');


$title = 'This script show how to generate a html file! '.date('Y-m-d H:i:s');

include $t->compiledFile();

?>






例子7、条件判断
{if *} * {else} * {elseif} * {/if} 或者 {if *} * {/if},其中{if *}中的*就是此判断语句的条件表达式,符合php的表达式。


语法如下:
变量不为空

name not empty 

变量值判断

Your name is jack! 

(var 在 ENDIF 之后是可选的,但是最好加上)

<?php

require_once "template.class.php";
$page = new template("if.html");

$username= 'jack' ;
$usergroup='ADMIN';
$picture= '' ;

include $page->compiledFile();

?> 

if.php使用的模板文件如下:

Welcome, {$username} 


[img]http://www.dophp.net/{$picture}[/img] 




[url=http://www.dophp.net/admin.php]ADMIN Login[/url]





if.php执行的效果如下:
输出: (查看)

Welcome, jack 




[url=http://www.dophp.net/admin.php]ADMIN Login[/url]


例子7、包含子模版
{include 文件名 } 

if.php使用的模板文件如下:
include.html:

{include file="header.html"}
{$body}
{include file="footer.html"}

include.php执行的效果如下



Hello World!
body
copy right

例子8、直接包含php代码
{include 文件名 } 

php.php使用的模板文件如下:
php.html:



<?php 
echo strtoupper($title);
?>




php.php执行的效果如下



<?php require_once "template.class.php";
$page = new Template("php.html");
$title ='Hello World!';
include $page->compiledFile();

?>



例子9、美工也可以看到的图片
{include 文件名 } 

8_image.php使用的模板文件如下:
image.html:



[img]http://www.dophp.net/{$images}/beijing.jpg[/img] 




8_image.php执行的效果如下



<?php 
require_once "template.class.php";
$page = new Template("image.html");
$images ='templates/{$images}/';
include $page->compiledFile();

?>



例子10 为多语言而生

8_image.php使用的模板文件如下:
image.html:

{%title}



8_image.php执行的效果如下



<?php

require_once "template.class.php";
$page = new Template("lang.html");

$lang['title'] ='模版显示中文';
include $page->compiledFile();

?> 





例子11、McTemplate VS smarty 


8_image.php使用的模板文件如下:
hello.html:












{$title}





mctemplate 代码 



<?php
require_once "template.class.php";
$page = new Template("hello.html");

$title = 'Hello World!';
include $page->compiledFile();

?>





smarty 代码 



<?php

include_once("Smarty.class.php");
class SmartyTpl extends Smarty{
function SmartyTpl(){
$this->Smarty();
$this->template_dir = "./templates/";
$this->compile_dir = '/tmp';

$this->compile_check = true;
$this->cache_lifetime=0;

}

}
$title = 'Hello World!';
$smarty->assign('title',$title);
$smarty->display('hello.html');

?>







8_image.php使用的模板文件如下:
include.html:

{include file="header.html"}
{$body}
{includE file="footer.html"}



mctemplate 代码 



<?php
require_once "template.class.php";
$page = new Template("include.html");


$title = 'Hello World!';
$body = 'body';
$footer ='copy right';
include $page->compiledFile();

?>





smarty 代码 



<?php

include_once("Smarty.class.php");
class SmartyTpl extends Smarty{
function SmartyTpl(){
$this->Smarty();
$this->template_dir = "./templates/";
$this->compile_dir = '/tmp';

$this->compile_check = true;
$this->cache_lifetime=0;

}

}
$title = 'Hello World!';
$body = 'body';
$footer ='copy right';

$smarty = new SmartyTpl();
$smarty->assign('body',$body);
$smarty->assign('title',$title);
$smarty->assign('footer',$footer);
$smarty->display('include.html');

?>
交流QQ群2:16142493
智能手机软件下载
PHP新手不可错过一帖
PHP新手如何获得积分
论坛需要你,我们大家需要你!

TOP

狂顶!!!!!。。。。。。。。。

TOP

顶......好东西

TOP

有沒詳細的說明? 這個上面應該有很多的功能沒有寫出來

TOP

酷酷

TOP

发新话题