企业级php开发,我们需要考虑:
[list:6ec4bae001]• 质量 (程序必须拥有一致性和稳定性)
• 功能 (程序必须满足付费用户的所有功能需求)
• 性能 (程序应该在一个可接受的严格规定的时间内处理用户请求)
• 扩展性 (倘若硬件支持,程序使用用户必须有数量级的提升)
• 持续发展 (不能超过被期望的开发预算)
• 准时性 (程序必须在规定的时间交付)
• 维护/支持费用 ()
• 可用性
• ......
• 等等[/list:u:6ec4bae001]
简短说明:
主要翻译http://www.smartphp.net/的smarttemplate部分的内容,随着认识的逐次加深,我将适当调整翻译的结果。
如果你有任何建议,请告诉我,谢谢!
为什么推荐他:
[list:6ec4bae001]
• 轻巧
• 简单
• 功能确实不错
[/list:u:6ec4bae001]
现在下载:
[list:6ec4bae001]
• [url=http://www.smartphp.net/download.php/smarttemplate_1_0_2.zip]smarttemplate_1_0_2.zip[/url](内含演示程序)
• [url=http://www.smartphp.net/download.php/smarttemplate_extension_examples.zip]smarttemplate_extension_examples.zip[/url](扩展演示)
[/list:u:6ec4bae001]
基本方法:
[list:6ec4bae001] • assign
• append
• output
• result
• use_cache
• debug[/list:u:6ec4bae001]
模板流程控制:
[list:6ec4bae001] • if
• else
• elseif
• begin ... end(块结构)
[/list:u:6ec4bae001]
| 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]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



