查看: 5064|回复: 0
打印 上一主题 下一主题

mctemplate大型WEB应用程序的模板引擎

[复制链接]
跳转到指定楼层
1#
发表于 2007-12-12 19:34:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
程序介绍:
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的值,其中的“name”由英文字母、数字和下划线组成首字母必须是英文字母或者下划线。

例子1:标量赋值

<?php
$template=new template('template.html');
define('TITLE,'Sample Text');;  
include $template->compiledFile();
?>
模板(template.html):
<html> {TITLE} </html>
输出:
<html> Sample Text </html>
例子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对应数组

<html>  
<h3> Sample Links </h3>

<!-- $links AS $v -->  

<a href="{$v['URL']}"> {$v['TITLE']} </a><br />

<!-- end -->
</html>

输出:

<html>
<h3> Sample Links </h3>

<a href="http://www.php.net/"> PHP </a>

<a href="http://www.apache.org/"> Apache </a>

<a href="http://www.mysql.com/"> MySQL </a>

</html>


例子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的表达式。
语法如下:
变量不为空

<!-- if($username)--> name not empty <!-- end -->

变量值判断

<!-- if($picture)--> Your name is jack! <!-- end -->

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

<?php

require_once "template.class.php";



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

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

include $page->compiledFile();

?>  




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

<!-- if($username)--> <H3> Welcome, {$username} </H3><!-- end -->


<!-- if($picture)--> <img src="{$picture}"> <!-- end -->


<!-- if($usergroup=="ADMIN") -->

<a href="admin.php"> ADMIN Login </a><br>
<!-- end -->
if.php执行的效果如下:
输出: (查看)
<H3> Welcome, jack </H3>  

<a href="admin.php"> ADMIN Login </a><br>

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

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

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

include.php执行的效果如下  
<h1>Hello World!</h1>
body<hr />
<h2>copy right</h2>

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

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

<h1>

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

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:

<h1>

<img src="{$images}/beijing.jpg" />  
</h1>  
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:

<h1>{%title}</h1>

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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" C>
<META NAME="Author" C>
<META NAME="Keywords" C>
<META NAME="Description" C>  
</HEAD>

<BODY>
{$title}
</BODY>
</HTML>
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');

?>

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表