发新话题
打印

Smarty速成 - Smarty入门教学

Smarty速成 - Smarty入门教学

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.tpl                                    output
<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输出&字符
index.tpl                                         output
<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 & Harris<br>

</body>
</html>


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

There are many more [url=]href="http://smarty.php.net/manual/en/language.modifiers.php">modifiers[/url]
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它们.

[ 本帖最后由 Ajax_chou 于 2007-7-10 21:55 编辑 ]
努力为phpres做贡献
时刻准备着,当机会来临时你就成功了
打好基础,增加社会经验
资深技术工程师是我的梦想
承接各种团体网站外包服务和各种it技术培训
准备申请AJAX版大,希望大家支持~~

TOP

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.你可以给模板分配一个数组的数据,这个函数就完成要求一系列的输出工作.
努力为phpres做贡献
时刻准备着,当机会来临时你就成功了
打好基础,增加社会经验
资深技术工程师是我的梦想
承接各种团体网站外包服务和各种it技术培训
准备申请AJAX版大,希望大家支持~~

TOP

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函数换新的一行并留出空白.
努力为phpres做贡献
时刻准备着,当机会来临时你就成功了
打好基础,增加社会经验
资深技术工程师是我的梦想
承接各种团体网站外包服务和各种it技术培训
准备申请AJAX版大,希望大家支持~~

TOP

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


未完。。。
努力为phpres做贡献
时刻准备着,当机会来临时你就成功了
打好基础,增加社会经验
资深技术工程师是我的梦想
承接各种团体网站外包服务和各种it技术培训
准备申请AJAX版大,希望大家支持~~

TOP

一看到速成我就兴奋 呵呵

TOP

发新话题