honestqiao 回复于:2005-08-15 14:43:39
|
[color=darkblue:ba7ae02416][size=24:ba7ae02416]smarttemplate 简介[/size:ba7ae02416][/color:ba7ae02416] smarttemplate是一个支持大型web应用程序的模板引擎. smarttemplate有什么特色? 普通模板引擎工作方式: 你的php脚本指定一个html模板,指定动态内容并显示。模板分析器使用指派的内容替换模板内所有的占位符,然后显示给用户. 这意味着,每次你要输出一些内容,程序都要花上好多时间去进行字符串的处理和正则表达式的工作。 smarttemplate 的工作方式类似模板编译,他把模板转换为可执行的php脚本,并且保存起来以备以后重用。当一个新的模板在第一次被使用时,the first time a new template is processed, 模板内所有的占位符被替换为简单的可输出指定内容的php代码元素。据个例子,模板片断 [b:ba7ae02416]<h3>{title}</h3>[/b:ba7ae02416],将被转换为 [b:ba7ae02416]<h3><?php echo $title; ?></h3>[/b:ba7ae02416]. 如果你指定内容给正确的变量, 将再也不需要进行模板分析了. 程序要做得仅仅是自己包含并执行便以后的模板. 这通常会戏剧性的减少模板引擎的运行时间.
smarttemplate 支持: [list:ba7ae02416] • 普通变量替换 (字符串,等等) • 重复的内容块 (嵌套数组/ begin..end) • 基本的逻辑控制结构 (if..elseif..else) • 可定制的扩展模块 (输出过滤, 大小写转换, 格式输出, 等等.) • 模板编译 (html 模板被转换为可执行的php代码) • 输出缓存 (重用输出页面从而提速你的程序)[/list:u:ba7ae02416] 让我们用一些简单的例子开始,告诉你如何使用 smarttemplate:
通常的流程是先建立一个web页面,展示所需要的效果. 创建简单的页面版式可以使用一个网页编辑器,例如dreamweaver或者homesite. 为了获得更为友好的页面版式效果,可以使用一个专业的设计工具,例如photoshop、 paintshop pro或者gimp等等.
预览: [code:1:ba7ae02416]hello world![/code:1:ba7ae02416]
现在我们来看看达到我们期望的页面版式的html源代码:
html-源代码 (hello_world.html): [code:1:ba7ae02416]<html> <h3>hello world!</h3> </html>[/code:1:ba7ae02416]
然后开始令人激动的一步: 分离内容和设计. 我们把html源代码之中的内容元素替换为模板占位符. 模板占位符被分配一个唯一的名称并且使用一个特有的标签包含起来,这样我们在随后的程序之中可以识别他们. 标题 hello world! 被替换为一个叫做title 的占位符. 我们是用大括弧来标记占位符, 似的我们的模板引擎可以发现: {title}. 最后html模板如下:
html-模板 (hello_world.tpl.html): [code:1:ba7ae02416]<html> <h3>{title}</h3> </html>[/code:1:ba7ae02416]
使内容整合到模板,我们需要做如下工作: [list:ba7ae02416]• 调用 smarttemplate 类 • 创建一个 smarttemplate 分析器 对象 • 告诉 smarttemplate 要使用的html模板 • 指定内容给关联的占位符 • 处理模板 • 输出结果[/list:u:ba7ae02416] 以下的php程序完成所需的工作: hello_world.php: [code:1:ba7ae02416]<?php
require_once “class.smarttemplate.php”; $page = new smarttemplate(“hello_world.tpl.html”); $page->assign(title, hello world!); $page->output();
?>[/code:1:ba7ae02416]就这么简单 – 我们的 hello world 例程已经准备运行了.
|
honestqiao 回复于:2005-08-15 14:45:43
|
[color=darkblue:cf330659a8][size=24:cf330659a8]比smarty要快上8倍![/size:cf330659a8][/color:cf330659a8] smarttemplate 特别优化以提高速度.尽管 smarttemplate 支持多种功能和函数, 但是在你的程序运行期间,仅仅很少一部分时间用在模板处理上. 他将使得你使用最少开发费用开发php程序,同时拥有闪电般的运行数度!
以下的基准测试,使用我们众所周知得模板引擎作为对比,来测试他们得运行速度。 测试过程分别使用包含1、20、200个对象被使用得模板。 基准测试 i: (smarty vs. smarttemplate) [list:cf330659a8]• server: redhat linux, apache 1.3.27, php 4.3.1 • pentium iii – 1300 mhz cpu, 512 mb ram • apache 基准测试, 20 个并发请求[/list:u:cf330659a8] [img:cf330659a8]../uploadfile/200510/20051013111334944.gif[/img:cf330659a8] 基准测试 ii: (smarty vs. smarttemplate) [list:cf330659a8]• server: redhat linux, apache 1.3.27, php 4.3.1 + ioncube accelerator • pentium iii – 1300 mhz cpu, 512 mb ram • apache基准测试, 20 个并发请求[/list:u:cf330659a8] [img:cf330659a8]../uploadfile/200510/20051013111336722.gif[/img:cf330659a8]
|
honestqiao 回复于:2005-08-15 14:47:22
|
[color=darkblue:b51f16d69a][size=18:b51f16d69a]smarttemplate 配置[/size:b51f16d69a][/color:b51f16d69a]
[b:b51f16d69a]bool $reuse_code[/b:b51f16d69a] 设置是否保存编译后的模板代码,以备重用 如果设置否false,smarttemplate会在每次模板使用时都重新编译模板
[b:b51f16d69a]string $template_dir[/b:b51f16d69a] 模板文件目录,默认为当前目录 可使用全局变量$_config[template_dir]设置
[b:b51f16d69a]string $temp_dir[/b:b51f16d69a] 模板编译保存目录,默认为/tmp/ 可使用全局变量$_config[smarttemplate_compiled]设置 [b:b51f16d69a]注意:确保php对该目录可写[/b:b51f16d69a]
[b:b51f16d69a]string $cache_dir[/b:b51f16d69a] 输出缓存目录,默认为/tmp/ 可使用全局变量$_config[smarttemplate_ cache]设置 [b:b51f16d69a]注意:确保php对该目录可写[/b:b51f16d69a]
[b:b51f16d69a]int $cache_lifetime[/b:b51f16d69a] 默认输出缓存周期 可使用全局变量$_config[ cache_lifetime ]设置
|
honestqiao 回复于:2005-08-15 14:53:28
|
[color=darkblue:b920254608][size=24:b920254608]基本方法 smarttemplate::assign()[/size:b920254608][/color:b920254608] void [b:b920254608]assign [/b:b920254608]( string placeholder, mixed content ) or void [b:b920254608]assign [/b:b920254608]( array content ) 给模板占位符(placeholder)或者列表(content)赋值. 可以使用散列数组或者标量 [b:b920254608]例子1:标量赋值[/b:b920254608] [code:1:b920254608]<?php
$template = new smarttemplate(template.html);
$text = sample text; $template->assign( title, $text );
$template->output();
?>[/code:1:b920254608] 模板(template.html): [code:1:b920254608]<html> {title} </html>[/code:1:b920254608] 输出: [code:1:b920254608]<html> sample text </html>[/code:1:b920254608] [b:b920254608]例子2: 多个标量赋值[/b:b920254608] [code:1:b920254608]<?php
$template = new smarttemplate(user.html);
$template->assign( name, john doe ); $template->assign( group, admin ); $template->assign( age, 42 );
$template->output();
?>[/code:1:b920254608]
模板(user.html): [code:1:b920254608]name: {name} group: {group} age: {age} [/code:1:b920254608] 输出: [code:1:b920254608]name: john doe group: admin age: 42[/code:1:b920254608] [b:b920254608]例子3: 使用数组给多个标量赋值[/b:b920254608] [code:1:b920254608]<?php
$user = array( name => john doe, group => admin, age => 42, );
$template = new smarttemplate(user.html);
$template->assign( $user );
$template->output();
?>[/code:1:b920254608]
模板(user.html): [code:1:b920254608]name: {name} group: {group} age: {age}[/code:1:b920254608]
输出: [code:1:b920254608]name: john doe group: admin age: 42[/code:1:b920254608] [b:b920254608]例子4: 命名空间[/b:b920254608] [code:1:b920254608]<?php
$admin = array( name => john doe, age => 42, ); $guest = array( name => roger rabbit, age => 16, );
$template = new smarttemplate(users.html);
$template->assign( admin, $admin ); $template->assign( guest, $guest );
$template->output();
?>[/code:1:b920254608]
模板(user.html): 占位符(placeholder)对应数组,“.”对应数组“[]” [code:1:b920254608]admin name: {admin.name} admin age: {admin.age}
guest name: {guest.name} guest age: {guest.age}[/code:1:b920254608]
输出: [code:1:b920254608]admin name: john doe admin age: 42
guest name: roger rabbit guest age: 16[/code:1:b920254608] [b:b920254608]例子5: 使用数组命名空间[/b:b920254608] [code:1:b920254608]<?php
$users = array( admin => array( name => john doe, age => 42, ), guest => array( name => roger rabbit, age => 16, ), );
$template = new smarttemplate(users.html);
$template->assign( $users );
$template->output();
?>[/code:1:b920254608]
模板(user.html): 占位符(placeholder)对应数组,“.”对应数组“[]” [code:1:b920254608]admin name: {admin.name} admin age: {admin.age}
guest name: {guest.name} guest age: {guest.age}[/code:1:b920254608]
输出: [code:1:b920254608]admin name: john doe admin age: 42
guest name: roger rabbit guest age: 16[/code:1:b920254608] [b:b920254608]例子6: 命名空间, 3个部分[/b:b920254608] [code:1:b920254608]<?php
$template = new smarttemplate(template.html);
$content[world][europe][germany] = de;
$template->assign( top_level_domain, $content );
$template->output();
?>[/code:1:b920254608] 模板(template.html): 占位符(placeholder)对应数组,“.”对应数组“[]” [code:1:b920254608]<html> german tld: {top_level_domain.world.europe.germany} </html>[/code:1:b920254608] 输出: [code:1:b920254608]<html> german tld: de </html>[/code:1:b920254608] [b:b920254608]例子7: 列表赋值[/b:b920254608] [code:1:b920254608]<?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 smarttemplate(links.html); $template->assign( links, $links ); $template->output();
?>[/code:1:b920254608] 模板(links.html): 结构名称lnks对应数组 [code:1:b920254608]<html> <h3> sample links </h3>
<!– begin links –>
<a href=”{url}”> {title} </a>
<!– end links –>
</html>[/code:1:b920254608] 输出: [code:1:b920254608]<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>[/code:1:b920254608] [b:b920254608]example 8: 使用数组于多个命名空间[/b:b920254608] [code:1:b920254608]<?php
$title = sample links; // page title
$target = _blank; // the same target for all links
$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 smarttemplate(links.html);
$template->assign( title, $title ); $template->assign( target, $target ); $template->assign( links, $links );
$template->output();
?>[/code:1:b920254608] 注意: title 与 links..title 使用不同的命名空间! target 不是 links 数组的成员. 如果使用在 begin..end 块之内, 他必须被引用为 {parent.target} 或者 {top.target}. 其他可能的用法: {top.title}, {parent.parent.page_id}, {top.users.admin}, 等等.. 模板(links.html): [code:1:b920254608]<html> <h3> {title} </h3>
<!– begin links –>
<a target={parent.target} href=”{url}”> {title} </a>
<!– end links –>
</html>[/code:1:b920254608] 输出: [code:1:b920254608]<html> <h3> sample links </h3>
<a target=”_blank” href=”http://www.php.net/”> php </a>
<a target=”_blank” href=”http://www.apache.org/”> apache </a>
<a target=”_blank” href=”http://www.mysql.com/”> mysql </a>
</html>[/code:1:b920254608]
|
北京野狼 回复于:2005-08-15 14:53:57
|
有价值
|
honestqiao 回复于:2005-08-15 15:01:29
|
[color=darkblue:9f3a29cbdd][size=18:9f3a29cbdd]smarttemplate::append()[/size:9f3a29cbdd][/color:9f3a29cbdd] void [b:9f3a29cbdd]append [/b:9f3a29cbdd]( string placeholder, mixed content ) 追加内容给模板占位符. 可以使用散列数组或者标量. 例子1 (列表): [code:1:9f3a29cbdd]<?php
$page = new smarttemplate(links.html);
$page->append(links => array( title => php, url => http://www.php.net/ )); $page->append(links => array( title => apache, url => http://www.apache.org/ )); $page->append(links => array( title => mysql, url => http://www.mysql.com/ )); $page->output();
?>[/code:1:9f3a29cbdd] 模板(links.html): 列表追加为行 [code:1:9f3a29cbdd]<html> <h3> sample links </h3>
<!– begin links –>
<a href=”{url}”> {title} </a>
<!– end links –>
</html>[/code:1:9f3a29cbdd] 输出: [code:1:9f3a29cbdd]<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> [/code:1:9f3a29cbdd]
[b:9f3a29cbdd]例子2 (标量):[/b:9f3a29cbdd] [code:1:9f3a29cbdd]<?php
$page = new smarttemplate(template.html);
$page->append(title => hello ); $page->append(title => world ); $page->append(title => !);
$page->output();
?>[/code:1:9f3a29cbdd]
模板(template.html): 标量为内容的追加 [code:1:9f3a29cbdd]<html> {title} </html>[/code:1:9f3a29cbdd]
输出: [code:1:9f3a29cbdd]<html> hello world ! </html>[/code:1:9f3a29cbdd]
|
imbiss 回复于:2005-08-15 16:24:58
|
有意思。”他把html模板转换为php脚本以获得快速的运行速度” 问题 是否就是说,他需要往硬盘里写东西?权限如何处理?
期待尝试一次。
|
titan3 回复于:2005-08-15 16:40:09
|
[quote:d199ced60f=”imbiss”]有意思。”他把html模板转换为php脚本以获得快速的运行速度” 问题 是否就是说,他需要往硬盘里写东西?权限如何处理?
期待尝试一次。[/quote:d199ced60f]
你说的东东,对应这个选项,转换后的php脚本会保存在这个目录下:
string $temp_dir 模板编译保存目录,默认为/tmp/ 可使用全局变量$_config[smarttemplate_compiled]设置 [b:d199ced60f]注意:确保php对该目录可写 [/b:d199ced60f]
|
honestqiao 回复于:2005-08-15 17:03:24
|
[color=darkblue:9ca539ec2f][size=18:9ca539ec2f]smarttemplate::output()[/size:9ca539ec2f][/color:9ca539ec2f] void [b:9ca539ec2f]output [/b:9ca539ec2f]() 解析模板并输出结果. 例子: [code:1:9ca539ec2f]<?php
$page = new smarttemplate(template.html); $page->assign(title => sample title);
$page->output();
?>[/code:1:9ca539ec2f]
|
honestqiao 回复于:2005-08-15 17:13:42
|
[color=darkblue:05ce43da7f][size=18:05ce43da7f]smarttemplate::result()[/size:05ce43da7f][/color:05ce43da7f] string [b:05ce43da7f]result [/b:05ce43da7f]() 解析模板并返回结果. 例子: [code:1:05ce43da7f]<?php
$page = new smarttemplate(template.html); $page->assign(title => sample title);
$output = $page->result();
echo output page: . $output;
?>[/code:1:05ce43da7f]
|
honestqiao 回复于:2005-08-15 17:22:53
|
[color=darkblue:be07f8e2f7][size=18:be07f8e2f7]smarttemplate::use_cache[/size:be07f8e2f7][/color:be07f8e2f7] void [b:be07f8e2f7]use_cache [/b:be07f8e2f7]( [mixed key] ) 激活内建的输出缓存. 判断当前执行的脚本 (判断依据$_server[request uri]) 是否在确定的时间内执行过. 如果执行过, use_cache 将返回缓存的页面给浏览器并且中止运行. 如果没有一个有效的输出句柄可以使用,use_cache将激活php输出缓存,并且返回数据到执行它的脚本. 下面的脚本执行时, use_cache 捕获所有输出到浏览器的内容,并保存到缓存目录. 缓存的每一个文件名称是唯一的,他根据当前执行的脚本文件名称,get参数(request_uri)以及可选得参数来自东设定. 如果脚本有一些重要的工作,例如记录日志等,那么应该在use_cache 之前调用你的代码. 例子: [code:1:be07f8e2f7]<?php
$page = smarttemplate(template.html); $page->cache_dir = /tmp/; // where to store cache files $page->cache_lifetime = 120; // keep cache for 120 seconds $page->use_cache(); // activate ouput cache
// // assemble page content //
$page->output();
?>[/code:1:be07f8e2f7]
|
honestqiao 回复于:2005-08-15 17:48:34
|
[color=darkblue:94580b7a4a][size=18:94580b7a4a]smarttemplate::debug()[/size:94580b7a4a][/color:94580b7a4a] void [b:94580b7a4a]debug [/b:94580b7a4a]() 激活内建调试器. debug 能够代替或者内嵌在 output . 他列出了指定的变量及其内容的详细列表, 编译后的模板和模板的原来结构. debug 对于确定和排除模板中的错误非常有用.
|
honestqiao 回复于:2005-08-15 17:49:51
|
[color=darkblue:a8dfa966b1][size=18:a8dfa966b1]流程控制 smartphp 例子: if[/size:a8dfa966b1][/color:a8dfa966b1] [b:a8dfa966b1]if … endif 控制有条件的输出模板的部分.[/b:a8dfa966b1]
语法如下: 变量不为空 [code:1:a8dfa966b1]<!– if var –> var 不为空! <!– endif var –>[/code:1:a8dfa966b1] 变量值判断 [code:1:a8dfa966b1]<!– if name==”honestqiao” –> your name is honestqiao! <!– endif name –>[/code:1:a8dfa966b1] 变量值否定判断 [code:1:a8dfa966b1]<!– if name!=” honestqiao ” –> your name is not honestqiao! <!– endif name –> [/code:1:a8dfa966b1]
(var 在 endif 之后是可选的,但是最好加上) if.php: ( download) [code:1:a8dfa966b1]<?php
require_once “class.smarttemplate.php”; $page = new smarttemplate(“if.html”);
$page->assign( username, honestqiao ); $page->assign( usergroup, admin ); $page->assign( picture, );
$page->output();
?> [/code:1:a8dfa966b1]
if.php使用的模板文件如下: if.html: ( download) [code:1:a8dfa966b1]<!– if username –> <h3> welcome, {username} </h3> <!– endif –>
<!– if picture –> <img src=”{picture}”> <!– endif picture –>
<!– if usergroup=”admin” –>
<a href=”admin.php”> admin login </a><br>
<!– endif usergroup –>[/code:1:a8dfa966b1]
if.php执行的效果如下: 输出: ( 查看) [code:1:a8dfa966b1]<h3> welcome, honestqiao </h3>
<a href=”admin.php”> admin login </a><br>[/code:1:a8dfa966b1]
|
honestqiao 回复于:2005-08-15 17:51:01
|
[color=darkblue:3a4c2600a8][size=18:3a4c2600a8]smartphp 例子: else[/size:3a4c2600a8][/color:3a4c2600a8] [b:3a4c2600a8]else [/b:3a4c2600a8]控制作为 if 控制的扩展,当if 判断结果为 false 来输出模板的一部分. else.php: ( download) [code:1:3a4c2600a8]<?php
require_once “class.smarttemplate.php”; $page = new smarttemplate(“else.html”);
$page->assign( username, john doe ); $page->assign( usergroup, admin ); $page->assign( picture, );
$page->output();
?> [/code:1:3a4c2600a8]
else.php使用的模板文件如下: else.html: ( download) [code:1:3a4c2600a8]<!– if username –>
<h3> welcome, {username} </h3>
<!– endif –>
<!– if picture –>
<img src=”{picture}”>
<!– else –>
picture not available! <br>
<!– endif picture –>
<!– if usergroup=”admin” –>
<a href=”admin.php”> admin login </a><br>
<!– else –>
you are in guest mode!
<!– endif usergroup –>[/code:1:3a4c2600a8]
else.php执行的效果如下: 输出: ( 查看)
[code:1:3a4c2600a8]<h3> welcome, john doe </h3>
picture not available! <br>
<a href=”admin.php”> admin login </a><br>[/code:1:3a4c2600a8]
|
honestqiao 回复于:2005-08-15 17:51:45
|
[color=darkblue:4616b33ba0][size=18:4616b33ba0]smartphp 例子: elseif[/size:4616b33ba0][/color:4616b33ba0] [b:4616b33ba0]elseif [/b:4616b33ba0]控制是 else 与 if 的结合. elseif.php: ( 下载) [code:1:4616b33ba0]<?php
require_once “class.smarttemplate.php”; $page = new smarttemplate(“elseif.html”);
$page->assign( usergroup, internal );
$page->output();
?> [/code:1:4616b33ba0]
elseif.php使用的模板文件如下: elseif.html: ( download) [code:1:4616b33ba0]<!– if usergroup=”admin” –>
<a href=”admin.php”> admin staff login </a><br>
<!– elseif usergroup=”support” –>
<a href=”support.php”> support staff login </a><br>
<!– elseif usergroup –>
<a href=”other.php”> standard login </a><br>
<!– else –>
you dont even have a usergroup!
<!– endif –>[/code:1:4616b33ba0] elseif.php执行效果如下: 输出: ( 查看)
[code:1:4616b33ba0]<a href=”other.php”> standard login </a><br>[/code:1:4616b33ba0]
|
honestqiao 回复于:2005-08-15 17:53:47
|
未完待续
|
unicorn_angel 回复于:2005-08-16 08:17:57
|
honestqiao兄是否可以讲一下smarttemplate的安装, 我安装以后出现/tmp/配置问题,不知怎样解决, 环境,win+apache2+php4.3 再介绍一下linux环境下的安装
|
unicorn_angel 回复于:2005-08-16 08:52:59
|
问题解决了,呵
|
honestqiao 回复于:2005-08-16 09:06:04
|
[quote:4d489182c7=”unicorn_angel”]问题解决了,呵[/quote:4d489182c7]
其实很多时候,多花一分钟时间去看看说明,也许就会节省十分钟的摸索。
|
wing-qiang 回复于:2005-08-16 15:46:03
|
好象很不错似的哦!要试试!
|
unicorn_angel 回复于:2005-08-16 16:51:36
|
请教honestqiao兄, 关于smarttemplate模板嵌套,教程上是这样写的 有两个模板文件: 一、--table.tpl-- <html> <head> </head> <body>
<p align=”center”><strong><font color=”#ff0000″ size=”5″>{title}</font></strong></p> <p align=”center”>————————————————————– </p> <table width=”75%” border=”1″ align=”center”> {content} </table> </body> </html>
----------------------------
二、--table_content.tpl-- <tr> <td>{td1}</td> <td>{td2}</td> </tr> ---------------------------- 要实现的目的是将 table_content.tpl 中的模板内容嵌入到 table.tpl 模板中形成一个完整的表格,具体实现的程序如下: --start table.php -------------------- <? require_once “./class/class.smarttemplate.php”;
//对表格行列数据符值
$info = new smarttemplate( “./templates/table_content.tpl” );
$info->assign(td1,td1); $info->assign(td2,td2);
//$info->output();
$result=$info->result();
//对表格整体进行赋值
$index = new smarttemplate( “./templates/table.tpl” );
$index->assign (title,这是一个表格模板试验程序);
$index->assign(content,$result);
$index->output();
?>
我的测试, <?php require “./admin/class/class.smarttemplate.php”; $t = new smarttemplate(“link.htm”); $t->assign(“link”,$link); $link_data = $t->result(); $time = date(“y-m-d g:i a”,time()); $index = new smarttemplate(“index.htm”); $index->assign(“time”,$time); $index->output(); ?> 为什么index.htm中{time}无返回值呀,
|
yarco 回复于:2005-08-16 18:44:32
|
一直有个问题想问… 为什么模板技术不做成php_extension呢?
|
unixdotnet 回复于:2005-08-16 19:43:55
|
不做成标准的扩展库是因为php自己本身就是一个模版,为了速度、灵活应该用php自己作为模版,smarty大概一年半前就成熟应用,smarttemplate大概一年前也应用过,它们的缺点很明显——慢。如果不在特殊情况下例如由客户自己编辑模版文件(cms)的话而,系统是crm、ehr、oa等那就应该回php本身。
|
honestqiao 回复于:2005-08-16 21:08:39
|
[quote:8fe8a7f3cc=”unicorn_angel”]请教honestqiao兄, 关于smarttemplate模板嵌套,教程上是这样写的 有两个模板文件: 一、--table.tpl-- <html> <head> </head> <body>
<p align=”center”><strong&……….[/quote:8fe8a7f3cc]
呵呵,不知道你在什么地方看的这样子的教程? 而且你这样子的嵌套,实际上对于模板来说,非常不好。 因为你把表格的tr提取出来,这个可是很不好设计的。
而且,你没有告诉我们,你的{time}是怎么在页面占位的,实际上,你可以直接: <html> {tiem} </html>
标准的测试,应该为: 一、--table.tpl-- <html> <head> </head> <body>
<p align=”center”><strong><font color=”#ff0000″ size=”5″>{title}</font></strong></p> <p align=”center”>————————————————————– </p> <table width=”75%” border=”1″ align=”center”> <!– begin content –> <tr> <td>{td1}</td> <td>{td2}</td> </tr> <!– end content –> </table> </body> </html>
--start table.php -------------------- <? require_once “./class/class.smarttemplate.php”;
//对表格行列数据符值
$result[]=(td1=>td1,td2=>td2);
//对表格整体进行赋值
$index = new smarttemplate( “./templates/table.tpl” );
$index->assign (title,这是一个表格模板试验程序);
$index->assign(content,$result);
$index->output();
?>
|
unicorn_angel 回复于:2005-08-17 09:55:50
|
问题解决,呵,看来以后看在网站找资料先要验证一下准确性了,
开始喜欢上smarttemplate了,
|
honestqiao 回复于:2005-08-17 18:35:04
|
[quote:0ea176db3d=”unicorn_angel”]问题解决,呵,看来以后看在网站找资料先要验证一下准确性了,
开始喜欢上smarttemplate了,[/quote:0ea176db3d]
呵呵,后面还有一部分,这几天不忙的时候继续帖上来。
这个东西轻巧简洁,不错
|
honestqiao 回复于:2005-08-18 11:48:17
|
[color=darkblue:057f14a0b1][size=18:057f14a0b1]smartphp 例子: begin end[/size:057f14a0b1][/color:057f14a0b1]
[b:057f14a0b1]begin … end[/b:057f14a0b1] 结构提供了一种方法,使用数字索引数组来输出重复的相似的内容。数字索引数组的每一个元素,应该是一个散列数组,<!– begin –> and <!– end –> 标签类似一个小的模板,他分析内嵌的模板片断,并使用这个散列数组来生成内容。
每个散列数组可以使用以下的两个扩展参数:
rowcnt :当前元素的在父数组之中的实际位置. (0,1,2,3,…n) rowbit : 表示rowcnt的二进制字节的最后一位,也就是奇偶值. (0,1,0,1,0,1,…)
begin … end 块可以很容易的嵌套使用,他们会被自动的递归分析. begin_end.php: ( download) [code:1:057f14a0b1]<?php
require_once “class.smarttemplate.php”; $page = new smarttemplate(“begin_end.html”);
$users = array( array( name => john doe, group => admin ), array( name => jack doe, group => support ), array( name => james doe, group => guest ), array( name => jane doe, group => guest ), );
$page->assign( users, $users );
$page->output();
?> [/code:1:057f14a0b1]
begin_end.php使用的模板如下: begin_end.html: ( download) [code:1:057f14a0b1]<style type=”text/css”> .col0 { background-color: #d0d0d0; } .col1 { background-color: #f0f0f0; } </style>
<table border=”1″ cellpadding=”2″ cellspacing=”0″> <tr> <th> no </th> <th> username </th> <th> usergroup </th> </tr>
<!– begin users –>
<tr class=”col{rowbit}”> <td> {rowcnt} </td> <td> {name} </td> <td> {group} </td> </tr>
<!– end users –>
</table>[/code:1:057f14a0b1]
begin_end.php的运行效果如下: 输出: ( 查看) [code:1:057f14a0b1]<style type=”text/css”> .col0 { background-color: #d0d0d0; } .col1 { background-color: #f0f0f0; } </style>
<table border=”1″ cellpadding=”2″ cellspacing=”0″> <tr> <th> no </th> <th> username </th> <th> usergroup </th> </tr>
<tr class=”col0″> <td> 0 </td> <td> john doe </td> <td> admin </td> </tr>
<tr class=”col1″> <td> 1 </td> <td> jack doe </td> <td> support </td> </tr>
<tr class=”col0″> <td> 2 </td> <td> james doe </td> <td> guest </td> </tr>
<tr class=”col1″> <td> 3 </td> <td> jane doe </td> <td> guest </td> </tr>
</table>[/code:1:057f14a0b1]
|
yarco 回复于:2005-08-19 11:37:57
|
[quote:b730ee57c9=”unixdotnet”]不做成标准的扩展库是因为php自己本身就是一个模版,为了速度、灵活应该用php自己作为模版,smarty大概一年半前就成熟应用,smarttemplate大概一年前也应用过,它们的缺点很明显——慢。如果不在特殊情况下例如由客�………[/quote:b730ee57c9] 当然,我明白你的意思.但就象c也可以使用面向对象的技术一样. 毕竟用php写模版肯定不如smarty方便.这也是模板技术产生的原因.
楼主的这个smarttemplate不是也把模版转化成php模板提高速度吗? 换个角度来看,模版技术更象是一种转换工具. 而假如有php扩展来自动提供这种转换不是更好吗?
|
honestqiao 回复于:2005-08-21 13:11:47
|
一个简单的演示,大家可以看看,因为商业性质开发,所以暂时不便公开源代码,等二次整理之后可以公布: http://www.moment.com.cn/album.php?albumaction=menu
刚才用smarttemplate做了一个小东西,使用ftp直接上传图图库,然后在线浏览: http://www.moment.com.cn/templates/default/album/menu.htm 菜单模板 http://www.moment.com.cn/templates/default/album/album.htm 相册分页模板 http://www.moment.com.cn/templates/default/album/list.htm 图片列表模板
没有美化的,便于教学。
list部分代码 [code:1:e3b71d60ec] …… if($albumaction == “list”) { $list = array(); for($i=0;$i<count($album_list)/$album_td;$i++) { for($j=0;$j<$album_td;$j++) { if(empty($album_list[$i*$album_td+$j])) { } else { $list[$i][“listtd”][$j][“link”] = “$album_url/$albumdir/$albumpage/{$album_list[$i*$album_td+$j]}”; $list[$i][“listtd”][$j][“name”] = “[{$album_list[$i*$album_td+$j]}]”; $list[$i][“listtd”][$j][“objid”] = $i*$album_td+$j; } }
}
$tpl->assign(“list”,$list); $tpl->output(); exit; }
……. [/code:1:e3b71d60ec]
|
|
nbxmedia 回复于:2005-08-22 14:25:21
|
这个……用过,还行,不过比smarty功能差一些……
|
honestqiao 回复于:2005-08-22 14:51:08
|
[quote:ef0706586f=”nbxmedia”]这个……用过,还行,不过比smarty功能差一些……[/quote:ef0706586f]
呵呵,如果你的女友小巧玲珑,当然不会有太多的赘肉的啊。
|
mydevc 回复于:2005-08-22 15:20:13
|
功能上,总会有变通的方法来解决的,我就用这个,感觉还可以的
|
nbxmedia 回复于:2005-08-22 15:20:17
|
说赘肉就有些过了吧………. 这玩意有些年久失修了,要拿来用肯定要做大量的修正,考虑下smartylite和savant吧 http://www.paullockaby.com/projects/smarty-light/ http://www.phpsavant.com/yawiki/
|
honestqiao 回复于:2005-08-22 15:46:37
|
[quote:f9dff04c9a=”nbxmedia”]说赘肉就有些过了吧………. 这玩意有些年久失修了,要拿来用肯定要做大量的修正,考虑下smartylite和savant吧 http://www.paullockaby.com/projects/smarty-light/ http://www.phpsavant.com/yawiki/[/quote:f9dff04c9a]
呵呵,你说的用过,不过个人有一个看法,以下都是三者最基本的例子: 既然是为mvc而来得,那么把php的变量$方式,更确切的来说,用<>,引入到模板文件,是很不明智的; 仅仅来看看模板:
savant还没有达到这个层次,似乎,因为: [code:1:f9dff04c9a] <?php echo $this->variable1 ?> [/code:1:f9dff04c9a]
而smartylite则: [code:1:f9dff04c9a] <% $lala %> [/code:1:f9dff04c9a]
我们再看看smarttemplate: [code:1:f9dff04c9a] <h3>{title}</h3> [/code:1:f9dff04c9a]
显而易见,哪一个更适合作为模板. 当然,我用模板的目的,是为了美工和页面可以更好的承担工作. 从程序的角度来说,我也可以直接看美化的页面去确定我的变量,而不是看html源代码来确认变量,哪一个更便利那?
|
hongweig 回复于:2005-08-22 15:54:47
|
[quote:7c9e48eb76] 我们再看看smarttemplate: 代码:
<h3>{title}</h3> [/quote:7c9e48eb76]
这种说法不地道吧。
phplib template也是这么写的啊。 我都不知道有什么先进性啊。
|
hongweig 回复于:2005-08-22 15:55:38
|
[quote:afdd6e5276=”honestqiao”]
呵呵,如果你的女友小巧玲珑,当然不会有太多的赘肉的啊。[/quote:afdd6e5276]
晕,这是什么话。
|
honestqiao 回复于:2005-08-22 15:59:53
|
[quote:1f29b8ebaa=”hongweig”]
这种说法不地道吧。
phplib template也是这么写的啊。 我都不知道有什么先进性啊。[/quote:1f29b8ebaa]
不是是否有先进性.
如果你是处在一个分工合作而不是单打独斗的环境,你可以实际问问你的页面制作人员,她更喜欢哪一个作为模板的方式. 使用<>包含的方式,她没办法进行实际效果的预览的.
当然,如果仅仅是给php开发人员自己使用,那就不能这么认为了.
|
nbxmedia 回复于:2005-08-22 16:00:48
|
[quote:026e8f37cd=”honestqiao”] 显而易见,哪一个更适合作为模板. 当然,我用模板的目的,是为了美工和页面可以更好的承担工作. 从程序的角度来说,我也可以直接看美化的页面去确定我的变量,而不是看html源代码来确认变量,哪一个更便利那?[/quote:026e8f37cd]
美工的问题,在实际情况下是大括号更麻烦,为何?斑竹用过dreamweavermx/2004没有?要知道<??>和<%%>是原生的php/asp tag,如果美工用dw这样的所见即所得工具,反而是大括号{}更容易破坏版面设计,如果您说那样的美工是不合格,那我发现80%以上的美工都是低于60分了,找一个懂smarty的美工不容易啊
而且我觉得一个标签并不是评判这个engine是否优秀的标准
|
hongweig 回复于:2005-08-22 16:01:52
|
呵呵。 就只是因为一个$的问题?
呵呵。 太勉强。
|
honestqiao 回复于:2005-08-22 16:03:17
|
[quote:f8cd3cb2e2=”nbxmedia”]
美工的问题,在实际情况下是大括号更麻烦,为何?斑竹用过dreamweavermx/2004没有?要知道<??>和<%%>是原生的php/asp tag,如果美工用dw这样的所见即所得工具,反而是大括号{}更容易破坏版面设计,如果您?………[/quote:f8cd3cb2e2]
呵呵,看来大家的意见都很多的.
一开始,我并没有说哪一个更好,只是个人看法的一个方面的而已.
使用<>的模式,不知道你是如何在设计过程进行预览的,类似下面: [b:f8cd3cb2e2][color=red:f8cd3cb2e2][size=18:f8cd3cb2e2]{段落标题}[/size:f8cd3cb2e2][/color:f8cd3cb2e2][/b:f8cd3cb2e2]
吃冰淇淋和鲜澄多(不要眼红,我们就是这么开会的)开部门会议去了,回来再看.
|
hongweig 回复于:2005-08-22 16:10:07
|
hi. 版主,我想说两句,清不要见怪:
你说的这个东东。我刚才去看了一下,我觉的版主不是太地道啊。 这个东西的功能先不要去说, 现在已经是2005年了,download页面的user comments还是2003的。 是不是这个项目已经没有人维护了,我感觉做为版主这样是不地道的。 要么你觉得它有可取之处就把你的经验那出来,大家一起分享一下。 之是这样贴出来,还打了一个大大的标题,比较容易误导人啊。
|
nbxmedia 回复于:2005-08-22 16:12:10
|
如果网站只有一个{tittle}那我也不说事了,但是一旦东西多的时候在dwmx下就不太行了,特别是表格和section,foreach应用的时候,标签100%要被{var}之类的长长字符串给破坏,如果是<??>和<%%>,反倒只有一个很小的icon,美工大可放心的排板,而且dw默认设置是绝对不会改变php/asp tag里的内容,但是{}就很难说了……..smarty支持自定义标签,应该说是非常英明的举措.
ps:我比较喜欢统一的绿茶,不过还是更喜欢咖啡一些…….
|
honestqiao 回复于:2005-08-22 17:57:55
|
[quote:0225ec9b92=”hongweig”]hi. 版主,我想说两句,清不要见怪:
你说的这个东东。我刚才去看了一下,我觉的版主不是太地道啊。 这个东西的功能先不要去说, 现在已经是2005年了,download页面的user comments还是2003的。 是不是这个项……….[/quote:0225ec9b92]
就我个人而言,我认为,没有用的东西,我是不会给大家推荐的.
[quote:0225ec9b92=”nbxmedia”]如果网站只有一个{tittle}那我也不说事了,但是一旦东西多的时候在dwmx下就不太行了,特别是表格和section,foreach应用的时候,标签100%要被{var}之类的长长字符串给破坏,如果是<??>和<%%>,反倒只有一个很?………[/quote:0225ec9b92] 没有调查,没有发言权;就算要发言,也不能凭空意想. [code:1:0225ec9b92] <html> <table> <!– begin row –> <!– 这个标签表示表格循环的起始,和dw注释相同 –> <tr> <td>{title} </td> </tr> <!– end row –> <!– 这个标签表示表格循环的结束,和dw注释相同 –> </table> </html> [/code:1:0225ec9b92] 你需要预览效果的,你会看到,你不需要预览效果的,它只是dw的注释而已. 只有被显示的内容,才使用{}来标记. 而控制的部分,采用<!– 控制结构 –>来设置,绝对不会破坏版面的设计的. 不知道,你是如何猜测,他会按照你所想的进行破坏的. 不知道注释是否会破坏排版.
另外声明一下: 1,本帖介绍smarttemplate,在本帖之中,将不可避免的在观点上更倾向她 2,可以争鸣,但是不可以争吵
|
nbxmedia 回复于:2005-08-22 18:09:10
|
调查?在下实在没有精力和资金对广大美工专业人士做如此有深度的<smarttemplate美工反馈调查报告>,只是偶尔和美工吵过一下而已? smart我用过,而且我发誓,nnd我改了至少4处地方.
反正我那些美工同事看到了那些大括号就说是程序,你说怎么办,那我还不如给它看图标呢.感觉说templatesys有利美工,这个是不可能的,因为他们用的是dw,而不是文本编辑工具,倒是对开发者更有利罢了.除非你把变量也变成注释符,让其不可见
|
honestqiao 回复于:2005-08-22 18:12:08
|
[quote:83d689c2cf=”nbxmedia”]调查?在下实在没有精力和资金对广大美工专业人士做如此有深度的<smarttemplate美工反馈调查报告>,只是偶尔和美工吵过一下而已? smart我用过,而且我发誓,nnd我改了至少4处地方.
反正我那些美工同事看到了那……….[/quote:83d689c2cf]
看来分工不一样,不好类比了. 我们这里,美工出效果图并简单分割,然后页面制作成模板.
|
gydoesit 回复于:2005-08-23 10:58:14
|
如楼上所说,这玩意最后一次更新已经一两年前的事了.是不是太落后了呀.
倒是smarty还在不断更新哩.
|
honestqiao 回复于:2005-08-23 12:17:02
|
[quote:01b52d508b=”gydoesit”]如楼上所说,这玩意最后一次更新已经一两年前的事了.是不是太落后了呀.
倒是smarty还在不断更新哩.[/quote:01b52d508b]
好的东西,有些会余救弥新. 实际上,咱们国内写php的,直接echo的人,还是很多很多的哦.
|
nbxmedia 回复于:2005-08-23 13:44:25
|
不更新的原因是因为maintainer去苏黎世为sap打工了,而且是属于24×7的快速响应的tech support,当然就没时间管php了………
而且,我不大清楚一个templatesys是否适合企业开发的定义在哪里……..
|
peng1000cn 回复于:2005-08-23 14:52:18
|
我的网站用的就是smart,速度很快吗?吹的,
理论上说的话,用模板要多一个过程,就是将模板转成html或php
这过程的话是需要时间的。
如果作为大型的项目的话,还是可以用的,牺牲一点点性能。从整体来看的话是值得的。
|
hongfengyue 回复于:2005-08-23 15:04:35
|
谢谢版主工作。好文章!
如果大家觉得那个不错可以介绍给大家,学习版主的样子。share…… 至少对需要的人来说,这是很不错的文章!
如果要讨论那个模板更好我认为可以另开一贴。
|
honestqiao 回复于:2005-08-23 16:21:32
|
[quote:0d7f9d49ac=”peng1000cn”]我的网站用的就是smart,速度很快吗?吹的,
理论上说的话,用模板要多一个过程,就是将模板转成html或php
这过程的话是需要时间的。
如果作为大型的项目的话,还是可以用的,牺牲一点点性能。从整体来看的……….[/quote:0d7f9d49ac]
你用了编译和缓存么?
|
nbxmedia 回复于:2005-08-23 17:22:51
|
smarty默认就会cache,不需要了…..
|
numlock 回复于:2005-08-24 21:48:25
|
当年phpe推荐的时候,好像反映没有这么强烈!
个人意见:所有的template,用处不大! 1、php本身就是一个模板系统(大家都知道,但是少有理会);
2、你的html应该使用标准化设计,页面的外观的改变要靠css,而不是用你的dreamweaver把html搞成糨糊!
|
honestqiao 回复于:2005-08-25 09:38:23
|
[quote:f5e62a465f=”numlock”]当年phpe推荐的时候,好像反映没有这么强烈!
个人意见:所有的template,用处不大! 1、php本身就是一个模板系统(大家都知道,但是少有理会);
2、你的html应该使用标准化设计,页面的外观的改变要靠css,……….[/quote:f5e62a465f]
呵呵,有见解。 不过要问上句:用dw好像是用来堆砌页面的,而不是用来做标准化的页面的。 不知道你是怎么进行标准化设计的? css控制样式,但是首先要有东西,然后css才有作用,没有内容,css巧妇难为无米之炊。
|
powerpolly 回复于:2005-08-25 10:12:44
|
现在的html的tag都过时了,放弃html吧
|
numlock 回复于:2005-08-26 11:28:48
|
[quote:f781568f91=”honestqiao”]
呵呵,有见解。 不过要问上句:用dw好像是用来堆砌页面的,而不是用来做标准化的页面的。 不知道你是怎么进行标准化设计的? css控制样式,但是首先要有东西,然后css才有作用,?……….[/quote:f781568f91]
excuse me! 我可没说用dw来进行标准化设计,而且我也不用。(虽然最新的dw做了改进) 我最爱editplus,编辑html时可以预览效果,很爽喔!
如果能够做到“[b:f781568f91][i:f781568f91]用html来表达内容[/i:f781568f91][/b:f781568f91],[b:f781568f91]css来控制外观[/b:f781568f91]”的话, 那么实现一个主题系统时, 主题的改变,实际上对html的变动是极少的(甚至不改变),需要的只是重新链接一个css。
即使是到了非改html不可的地步,也不必一定要拿出template。。。。。。 (建议开个新贴讨论这个问题)
|
imbiss 回复于:2005-09-03 00:12:31
|
楼上的对模板的理解狭隘了。我认为template是很有必要的。模板可不仅仅指html模板。
目前可以做到css控制站点样式。(去看看css zengarten就知道了) php控制内容和流程 template可以控制输出的格式,并不局限于html模板。我的经验是,模板位于表现层,mvc结构的最终层。选择不同的模板产生xhtml+css,或者html+tablle或者xml格式的,比如rss。全部基于模板。我连ajax的请求都利用了xml模板。
所以模板无处不在。
|
numlock 回复于:2005-09-03 16:08:55
|
楼上的! 我们这里讨论的是模板引擎。不是template view。 我说不用模板引擎,可没说不用template view。
一个是工具,一个是模式。不要搞错,ok!
|
imbiss 回复于:2005-09-03 16:25:22
|
[quote:1915f33cb5=”numlock”]楼上的! 我们这里讨论的是模板引擎。不是template view。 我说不用模板引擎,可没说不用template view。
一个是工具,一个是模式。不要搞错,ok![/quote:1915f33cb5]
既然用模板,为何不用模板引擎?这个工具用在这个模式很合适,为何不用?
|
icelangker 回复于:2005-09-04 23:10:04
|
拿smartytemplate和phplib 来pk那个更加nb一点?? 顺便问下,phplib支持cache和编译吗?
|
imbiss 回复于:2005-09-05 01:19:02
|
[quote:59be45b1b2=”icelangker”]拿smartytemplate和phplib 来pk那个更加nb一点?? 顺便问下,phplib支持cache和编译吗?[/quote:59be45b1b2] smarty年轻,功能更强。(我只看过介绍,没有实战过) phplib历史悠久,小巧,但不支持cache和所谓的”编译”. (我常用)
|
honestqiao 回复于:2005-09-05 08:09:54
|
[quote:67d9af58ec=”imbiss”] smarty年轻,功能更强。(我只看过介绍,没有实战过) phplib历史悠久,小巧,但不支持cache和所谓的”编译”. (我常用)[/quote:67d9af58ec]
smarty与smarttemplate是不同的模板。 |
|
|