smarty速成,translated~[zhuan]

smarty速成,translated~



Crash Course[Smarty速成]


For those of you who have used PHP template engines, the basic concepts
of Smarty should look quite familiar.

In your PHP application you assign variables for use in the template,
then you display it.


如果你已经使用过php模板引擎,那么应该非常熟悉smarty的基本要领了.

在你的php程序里你把要用到的变量分配到模板中,然后将他们用于页面输出.











index.php
include('Smarty.class.php');

// create object[创建对象]
$smarty = new Smarty;

// assign some content[分配一些内容]
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');

// display it[输出模板]
$smarty->display('index.tpl');

The template file then contains the output interspersed with tags that
Smarty replaces with assigned content.


模板文件[tpl]包含了所要输出的内容的标签,Smarty将用你所分配的内容替换他们















index.tploutput
<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: {$name}<br>
Address: {$address}<br>

</body>
</html>
 
<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: george smith<br>
Address: 45th & Harris<br>

</body>
</html>

Smarty has a unique feature called variable modifiers that can
alter the content of assigned variables from within the template. In
our example, we would like to display George's name capitalized, and
we would like to properly HTML escape the amphersand (&) symbol
in the address.


Smarty有这样的一个特点(称之为变量调节器):可以在模板内转换所要分配的变量.

例如,我们希望让Geoge的名字首字母大写,而在地址里希望html输出&amp;字符















index.tploutput
<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: {$name|capitalize}<br>
Address: {$address|escape}<br>

</body>
</html>
 
<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: George Smith<br>
Address: 45th &amp; Harris<br>

</body>
</html>

Pretty easy! You can also chain modifiers together on one variable,
making this feature quite flexible.

There are many more
href="http://smarty.php.net/manual/en/language.modifiers.php">modifiers
that come with Smarty, or you can make your own with its easy to use
plugin architecture.

Drop your new modifier into the plugin directory, then mention it in
the template!


干得漂亮!(ft...)你同样可以将调节器链接到一个变量上面,让它更灵活.

Smarty自带了很多调节器,或者你可以使用插件机制diy一个.

把你自己做的调节器放到插件目录里,就会在模板里起作用!


Smarty also has template functions that can carry out tasks.

For example, you can include other templates from within a template
with the include function.

Let's say you have many templates with the same header and footer information.
You can manage these as separate templates and include them.


Smarty同样可以用模板函数来完成任务.

例如,你可以在模板里使用include函数来包含其他模板.

比如说你有很多模板都带有相同的头,尾信息,你可以为它们独立制作成模板,再include它们.

























header.tplfooter.tpl
<html>
<head>
<title>{$title|default:"no title"}</title>
</head>
<body>
 
</body>
</html>
index.tploutput
{include file="header.tpl" title="User Info"}

User Information:<p>

Name: {$name|capitalize}<br>
Address: {$address|escape}<br>

{include file="footer.tpl"}
 
          
          
<html>
<head>
<title> User Info</title>
</head>
<body>

User Information:<p>

Name: George Smith<br>
Address: 45th &amp; Harris<br>

</body>
</html>

Notice that $title is a template variable not assigned in the
PHP code, but assigned by passing it as an attribute to the include
function.

This way the $title variable is only available within the scope
of the header template, and can be dynamically changed any time the
header.tpl file is included.

Also, notice the default modifier is applied to $title
so in the case no title was supplied, then the text "no title"
will show up instead of nothing.


注意到$title是一个没有在php代码里被分配的模板变量,但是却被include函数传递了过来.

这种方法使得$title变量只在Header模板里有作用.在任何时候header.tpl被include的时候都可被动态替换.

同样,注意到default调节器应用到了$title,在没有任何标题的情况下,将显示"no title"


There are some nice functions that automate tasks such as html dropdown
boxes.

One is html_options. You assign the arrays of data to the template,
then this function does all the work to generate the HTML output for
it.


smarty同样也有很多很好的自动完成函数,比如html列表.

其中一个是 html_options.你可以给模板分配一个数组的数据,这个函数就完成要求一系列的输出工作.











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('name', array('bob','jim','joe','jerry','fred'));

// display it
$smarty->display('index.tpl');










index.tpl
<select name=user>
{html_options values=$id output=$names selected="5"}
</select>










output
<select name=user>
<option label="bob" value="1">bob</option>
<option label="jim" value="2">jim</option>
<option label="joe" value="3">joe</option>
<option label="jerry" value="4">jerry</option>
<option label="fred" value="5" selected>fred</option>

</select>


Smarty facilitates a convenient way to loop over arrays of data with
the section function.

Here's an example of that, and we also threw in alternating row colors
via the cycle function, and we use the strip function
to strip out newlines and white space.


Smarty可以使用section函数轻松的遍历数组.

这里有个例子,我们同时也用了cycle函数插入行交替颜色,使用strip函数换新的一行并留出空白.











index.php
include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign an array of data
$smarty->assign('name', 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
<table>
{section name=mysec loop=$name}
{strip}
<tr bgcolor="{cycle values="#eeeee,#dddddd"}">
<td>{$name[mysec]}</td>
</tr>
{/strip}
{/section}
</table>

<table>
{section name=mysec loop=$users}
{strip}
<tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
<td>{$users[mysec].name}</td>
<td>{$users[mysec].phone}</td>
</tr>
{/strip}
{/section}
</table>










output
<table>
<tr bgcolor="#eeeeee"><td>bob</td></tr>
<tr bgcolor="#dddddd"><td>jim</td></tr>
<tr bgcolor="#eeeeee"><td>joe</td></tr>
<tr bgcolor="#dddddd"><td>jerry</td></tr>
<tr bgcolor="#eeeeee"><td>fred</td></tr>
</table>

<table>
<tr bgcolor="#aaaaaa"><td>bob</td><td>555-3425</td></tr>
<tr bgcolor="#bbbbbb"><td>jim</td><td>555-4364</td></tr>
<tr bgcolor="#aaaaaa"><td>joe</td><td>555-3422</td></tr>
<tr bgcolor="#bbbbbb"><td>jerry</td><td>555-4973</td></tr>
<tr bgcolor="#aaaaaa"><td>fred</td><td>555-3235</td></tr>
</table>







bob
jim
joe
jerry
fred








bob555-3425
jim555-4364
joe555-3422
jerry555-4973
fred555-3235

There are many more
href="http://smarty.php.net/manual/en/language.builtin.functions.php">builtin
and
href="http://smarty.php.net/manual/en/language.custom.functions.php">custom
functions that come with Smarty, or you can make your own, again with
the easy to use
href="http://smarty.php.net/manual/en/plugins.php">plugin architecture.


Take note that all of the functionality of Smarty (modifiers, functions,
etc.) deal exclusively with the presentation of content.

This is an important aspect of separating your presentation layer.

All application/business logic should reside in your application, whereas
all presentation-related matters should be handled by the templates.


This is the paradigm Smarty was built upon.


Smarty有很多内建和自定义函数,你也可以使用插件机制自己做.


请注意smarty的所有专用于内容展示的功能(调节器,函数,等等).

这是分隔你的表示层的很重要的方面.

所有的应用/商业逻辑都应该保留在你的应用程序(后台)里 ,然而所有的有关页面输出,展示的冬冬都应该在模板里处理.

以上是smarty应用的范例.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值