Smarty php Crash Course

2008-01-25 23:15 来源: smarty 作者:phpma 网友评论 0 条 浏览次数 0

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="#eeeeee,#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>

Smarty also has built-in caching capabilities to help speed up the page rendering. A copy of the template output is stored in a text file, then that is displayed upon subsequent calls to the request instead of dynamically rendering the page each time. This can speedup page rendering substantially, especially if there is a lot of processing involved to create the page such as database calls and variable assignments.

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

// create object
$smarty = new Smarty;
$smarty->caching = true;

// see if the page is already cached
if(!$smarty->is_cached('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');

You can also have multiple cached versions of a single page by passing a unique cache id to the display() function, check out the docs on that one!

There are many more builtin and custom functions that come with Smarty, or you can make your own, again with the easy to use plugin architecture.

Again, we'll take note that all of the functionality of Smarty (modifiers, functions, etc.) deal exclusively with the presentation of content. This is the design principle Smarty was built upon.

[上一页1  2  3 
上一篇:Smarty Manual P..    下一篇:PHP&Smarty Why ..

相关主题:

网友评论