libs/-Verzeichnis des PHP include_path oder setzen der SMARTY_DIR Konstante auf das Smarty Verzeichnis.templates, configs und templates_c.smarty-test.php
<?php
require_once("path/to/Smarty.class.php");
$smarty = new Smarty();
if(isset($_SESSION['username'])) {
$smarty->assign("logged_in_as", "You are logged in as " . $_SESSION['username']);
}
if(isset($_POST['id'])) {
$results = database_query("SELECT * FROM userdata WHERE id = ?", Array($_POST['id']));
$smarty->assign("results", $results);
}
$smarty->display("smarty-page.tpl");
?>
smarty-page.tpl
{include file='header.php'}
<p>Welcome to the application</p>
{if isset $logged_in_as}
<p>{$logged_in_as}</p>
{/if}
{if sizeof($results > 0)}
<ul>
{foreach from=$results item=i}
<li>{$i}</li>
{/foreach}
</ul>
{/if}
<p>Please enter an ID to search for</p>
<form method='post'>
<input type='text' name='id' value=''>
<input type='submit'>
</form>
{include file='footer.php'}
Mit Smarty kann man safe-HTML-Code erzeugen, indem man durch $smarty→default_modifiers = array(’escape:”htmlall”‘) setzt, oder in PHP5 das Smarty Objekt mit $smarty→assign() beeinflußt.
Wenn Smarty-Infos erwünscht sind, so kann man den debug Modus einschalten, der die Ergebnisse in einem Popup-Fenster zeigt, während die Seite aufgebaut (gerendert) wird.
smarty-debugging
$smarty = new Smarty();
$smarty->debugging = true;
....
$smarty->display("mytemplate.tpl");