OakWeb--之Smarty的dynamic section
<?php
// This just initializes Smarty with the locations of the
// directories it will use
$full_path = dirname(__FILE__)."/";
define ("SMARTY_DIR",$full_path."libs/Smarty/");
define ("TEMPLATES_PATH",$full_path . "templates");
define ("TEMPLATES_C_PATH",$full_path . "templates_c");
define ("CONFIGS_PATH",$full_path . "configs");
define ("CACHE_PATH",$full_path . "cache");
require_once SMARTY_DIR."Config_File.class.php";
require_once SMARTY_DIR."Smarty.class.php";
// Initialize a new smarty object
$smarty = new Smarty ();
// Assign some variables in our templates file
$smarty->assign ("title", "Smarty Introduction");
$smarty->assign ("body", "Hello There.");
// Thanks goes out to the authors of Smarty
// Note we are appending here and filling in a dynamic section
$smarty->append ("first", "Monte");
$smarty->append ("last", "Ohrt");
$smarty->append ("first", "Andrei");
$smarty->append ("last", "Zmievski");
// Show the template
$smarty->display ("index.tpl");
?>
index.tpl
examples/smarty/templates/index.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>{$title}</title></head><body><h1>{$title}</h1><p>{$body}</p><h3>I'd like to thank these people for making this possible:</h3><ul>{* This is a Smarty comment *}{section name="thank" loop=$first} <li>{$first[thank]} {$last[thank]}</li>{sectionelse} <li>No names were found.</li>{/section}</ul></body></html>