企业级PHP开发,我们需要考虑:
[list:6ec4bae001]• 质量 (程序必须拥有一致性和稳定性)
• 功能 (程序必须满足付费用户的所有功能需求)
• 性能 (程序应该在一个可接受的严格规定的时间内处理用户请求)
• 扩展性 (倘若硬件支持,程序使用用户必须有数量级的提升)
• 持续发展 (不能超过被期望的开发预算)
• 准时性 (程序必须在规定的时间交付)
• 维护/支持费用 ()
• 可用性
• ......
• 等等[/list:u:6ec4bae001]
简短说明:
主要翻译http://www.smartphp.net/的SmartTemplate部分的内容,随着认识的逐次加深,我将适当调整翻译的结果。
如果你有任何建议,请告诉我,谢谢!
为什么推荐他:
[list:6ec4bae001]
• 轻巧
• 简单
• 功能确实不错
[/list:u:6ec4bae001]
现在下载:
[list:6ec4bae001]
• smarttemplate_1_0_2.zip(内含演示程序)
• smarttemplate_extension_examples.zip(扩展演示)
[/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 don't 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]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
