smarty模板 php 突击教程
smarty模板 php 突击教程
突击教程
模板基础知识:向 分配内容,然后显示模板文件。
index.php
include('Smarty.class.php'); // create object $smarty = new Smarty; // assign some content. This would typically come from // a database or other source, but we'll use static // values for the purpose of this example. $smarty->assign('name', 'george smith'); $smarty->assign('address', '45th & Harris'); // display it $smarty->display('index.tpl');
然后模板文件包含表现形式的输出,其间插入 {标签}, 用动态内容替换这些标签。
index.tpl
Info User Information: Name: {$name} Address: {$address}输出
Info User Information: Name: george smith Address: 45th & Harris如你所见,彻底地将你的展示(HTML/CSS)与应用(PHP)代码分离了。然而smarty模板 php 突击教程,它仍然包含一些自身的逻辑。关于 的设计原则,模板中的逻辑应该仅用于展示。例如,遍历表格行颜色或从另一个模板包含一个模板会被视为展示逻辑。通过这种方式,应用程序代码只需要提供内容,模板可以负责展示。其想法是将模板设计人员的角色(前端开发人员)和应用程序编程人员的角色(后端开发人员)区分开来。你应该能够彻底重新设计展示(内容)而无需改变应用程序代码。你也应该能够重新构建应用程序代码并使用相同的模板,只需将内容分配到相同的变量。这种设计极为有利于维护和稳定性。
有时需要对分配的内容进行更改以用于展示。这可以通过变量修饰符实现。这些用于改变模板中已分配变量的输出。在我们的示例中,我们想要以大写字母显示 的名字smarty模板 phpsmarty模板 php 突击教程,并且希望正确转义地址中的符号(&)。我们还展示如何以自定义格式显示当前日期。
index.tpl
Info User Information: Name: {$name|capitalize} Addr: {$address|escape} Date: {$smarty.now|date_format:"%b %e, %Y"}输出
Info User Information: Name: George Smith Addr: 45th & Harris Date: Jul 16, 2010通过使用修饰符,我们刚刚将已分配内容的格式控制权交给模板。其想法是将未修改的内容分配给模板,将展示逻辑保留在应用程序代码之外。
你可以在一个变量上链接任意数量的修饰符,这使得此功能极具灵活性。 附带了许多其他 修饰符,或者你也可以使用易于使用的 插件 架构自己制作修饰符。将你的新修饰符放入插件目录,然后在模板中提到它!
您还可以使用或创建模板函数。模板函数在模板中执行任务。例如,您可以通过{}函数包含其他模板,可以通过{}函数生成一组选择选项,或者可以通过{cycle}函数循环一组值。假设您有很多具有相同页眉和页脚信息的模板。您可以将它们作为单独的模板进行管理并包含它们。
.tpl
{$title|default:"no title"} .tpl
index.tpl
{include file="header.tpl" title="Info"} User Information:Name: {$name|capitalize}
Address: {$address|escape}
{include file="footer.tpl"}输出
User Information:Info Name: George Smith
Address: 45th & Harris
{}函数的一项功能是局部作用域变量。请注意,$title 是一个模板变量,它没有直接分配给模板,而是通过将其作为属性传递给{}函数来分配的。这样,$title变量只在页眉模板的范围内可用,并且可以在每次包含.tpl文件时动态更改。此外,请注意我们对$title使用了修饰符,所以如果$title为空,那么文本 “no title” 将显示而不是不显示任何内容。
管理模板层次结构的另一种方法是模板继承。一个模板可以继承另一个模板smarty模板 php,并更改该模板内的特定内容块。例如
.tpl
{block name=title}Default Title{/block} {block name=body}Default Body{/block}child.tpl
{extends file="parent.tpl"} {block name=title}My Title{/block} {block name=body}My Body{/block}输出
My Title My Body有一些很好的函数可以自动执行诸如html下拉框之类的任务。便是其中之一。您将数据数组分配给模板,然后此函数将执行所有工作来生成 HTML 输出。
index.php
include('Smarty.class.php'); // create object $smarty = new Smarty; // assign options arrays $smarty->assign('id', array(1,2,3,4,5)); $smarty->assign('names', array('bob','jim','joe','jerry','fred')); // display it $smarty->display('index.tpl');index.tpl
{html_options values=$id output=$names selected="5"}输出
<option label="bob" value="1">bob <option label="jim" value="2">jim <option label="joe" value="3">joe <option label="jerry" value="4">jerry <option label="fred" value="5" selected="selected">fred
您可以使用函数快速循环处理数据数组。下面是一个示例,而且我们还通过{cycle}函数交替显示行颜色,并使用{strip}函数去除额外的空格。
index.php
include('Smarty.class.php'); // create object $smarty = new Smarty; // assign an array of data $smarty->assign('names', array('bob','jim','joe','jerry','fred')); // assign an associative array of data $smarty->assign('users', array( array('name' => 'bob', 'phone' => '555-3425'), array('name' => 'jim', 'phone' => '555-4364'), array('name' => 'joe', 'phone' => '555-3422'), array('name' => 'jerry', 'phone' => '555-4973'), array('name' => 'fred', 'phone' => '555-3235') )); // display it $smarty->display('index.tpl');index.tpl
| {$name} | {/strip} {/foreach}
| {$user.name} | {$user.phone} | {/strip} {/foreach}
输出
| bob | <tr bgcolor="#dddddd">jim | <tr bgcolor="#eeeeee">joe | <tr bgcolor="#dddddd">jerry | <tr bgcolor="#eeeeee">fred |
| bob | 555-3425 | <tr bgcolor="#bbbbbb">jim | 555-4364 | <tr bgcolor="#aaaaaa">joe | 555-3422 | <tr bgcolor="#bbbbbb">jerry | 555-4973 | <tr bgcolor="#aaaaaa">fred | 555-3235 |
还具有内置缓存功能,有助于加快页面渲染速度。模板输出的副本存储在文本文件中,然后在后续请求调用时显示该副本,而不是每次动态呈现页面。这可以大大加快页面渲染速度,尤其是在创建页面时涉及大量处理时,例如数据库调用和变量分配。您还可以通过将某些部分标记为 而不使其动态。
index.php
include('Smarty.class.php'); // create object $smarty = new Smarty; $smarty->setCaching(true); // see if the page is already cached if(!$smarty->isCached('index.tpl')) { // not cached, so you might do some database queries here, // then assign the returned content. We just use static // values for this example. $smarty->assign('name', 'george smith'); $smarty->assign('address', '45th & Harris'); } // display it $smarty->display('index.tpl');
通过向() 函数传递唯一缓存 IDsmarty模板 php,还可以拥有单个页面的多个缓存版本。有关详细信息,请参阅文档。
还有很多其他内置和自定义函数附带,或者您可以自己制作,再次使用易于使用的插件架构。
关于的还有很多东西需要了解,现在就去文档中看看吧!
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299
资源库 - 资源分享下载网 » smarty模板 php 突击教程
常见问题FAQ
- 关于资源售价和售后服务的说明?
- 代码有没有售后服务和技术支持?
- 有没有搭建服务?
- 链接地址失效了怎么办?
- 关于解压密码



