SMARTY QUICKSTART GUIDE ----------------------- Welcome to the Smarty QUICKSTART guide. This guide is to help you immediately get your feet wet in Smarty, get a feel for how it works and if it will work for you. We make a few assumptions here, such as you already have PHP installed on your web server, you know the basics of unix file permissions, the basics of PHP and how it works, etc. If you are using Smarty in a non-unix environment, you should understand the difference in your OS regarding file permissions. INSTALLATION ------------ Unpack the Smarty tarball. You will see four class files: Smarty.class.php, Smarty.addons.php, Smarty_Compiler.class.php and Config_File.class.php. You will need to have all four of these files somewhere in your PHP include path, so when you call require("Smarty.class.php") from within your application, it can find the class. Alternatively, you can set the SMARTY_DIR constant in your application, and Smarty will use that directory as the path to the Smarty class files. Be sure the path ends with a slash! Smarty uses the PEAR libraries for some of its error handling routines. PEAR libraries come with the distribution of PHP. Be sure that the path to these libraries is included in your php include_path. Unix users check /usr/local/lib/php. Windows users check C:/php/pear. Now change directories somewhere inside of your web server document root. For this guide, we'll create a directory under the document root named "Smarty", and put all of our work here. $> cd /home/htdocs $> mkdir Smarty $> cd Smarty now, we need to create a few directories for Smarty to use: $> mkdir templates $> mkdir templates_c $> mkdir configs Smarty needs to be able to write to the templates_c directory. It is recommended that you change the ownership and write permissions such that the web server user (usually nobody) has write access to this directory. You can chmod 777 the directory, but be aware of security concerns on multi-user systems. See the documentation for more info on this. $> chown nobody:nobody templates_c $> chmod 700 templates_c $> ls -l drwxrwxr-x 2 user group 512 Jan 18 14:18 configs/ drwxrwxr-x 2 user group 512 Jan 18 14:18 templates/ drwx------ 2 nobody nobody 512 Jan 18 14:18 templates_c/ Or alternately (less secure): $> chmod 777 templates_c $> ls -l drwxrwxr-x 2 user group 512 Jan 18 14:18 configs/ drwxrwxr-x 2 user group 512 Jan 18 14:18 templates/ drwxrwxrwx 2 user group 512 Jan 18 14:18 templates_c/ Make sure the current directory and the path to PEAR.php are in your include_path. Example php.ini entries: (unix) include_path=".:/usr/local/lib/php" (windows) include_path=".;\php\pear" Also, windows users may have to change the filename from PEAR.php.in to PEAR.php (named like this in some distributions?) Now we need to create two files, index.php and templates/index.tpl. index.php is the file that we will be calling from our web browser. index.tpl is the file that Smarty will use as its template file. (Smarty template files are never accessed directly from the browser, only the Smarty class accesses them.) --------- index.php -------- assign("Name","Ned"); $smarty->display("index.tpl"); ?> --------- templates/index.tpl --------
    There were {$smarty.section.people.loop} names in this list.
{include file="footer.tpl"}
Here we are introducing the {$smarty} variable, which is used to reference
values internal to the template. Notice that when printing variables inside of
the section, the section name must be referenced in the name of the variable
being displayed. This lets Smarty understand that you want to print the value
in the array postion indexed by the current loop value. There are also internal
template variables available within the section that display the loop iteration
and the total number of times the section is looped. Also note the
{sectionelse}. This would have been displayed if looped array $FirstName was
empty.
You can access keys of arrays with this syntax:
--------- index.php --------
assign(array("ContactInfo" => array(
				array("FirstName" => "Ned","LastName" => "Flanders"),
				array("FirstName" => "Monty","LastName" => "Burns")
				)
			));
$smarty->display("index.tpl");
?>
--------- templates/index.tpl --------
{include file="header.tpl" title="Home Page"}
    {section name=people loop=$ContactInfo}
        {$ContactInfo[people].FirstName}
		{$ContactInfo[people].LastName}
    {sectionelse}
        There are no values to loop through.
    {/section}
    
    There were {$smarty.section.people.loop} names in this list.
{include file="footer.tpl"}
You can also do complex nested sections, like so:
--------- index.php --------
assign("FirstName",array("Ned","Bart","Montgomery"));
$smarty->assign("LastName",array("Flanders","Simpson","Burns"));
$smarty->assign("ContactNames",array(
                               array("email","home","cell"),
                               array("email","home"),
                               array("email","home","fax")
                                ));
$smarty->assign("ContactVals",array(
                              array("ned@simpsons.com","555-666-7777","555-444-3333"),
                              array("bart@simpsons.com","555-111-2222"),
                              array("monty@simpsons.com","555-888-9999","555-234-5678"),
                                ));
$smarty->display("index.tpl");
?>
--------- templates/index.tpl --------
{include file="header.tpl" title="Home Page"}
    {section name=people loop=$FirstName}
        {$smarty.section.people.rownum} {$FirstName[people]} {$LastName[people]}
        {section name=contacts loop=$ContactNames[people]}
            {* for fun, lets bold every other row *}
            {if $smarty.section.contacts.rownum is even}{/if}
                {$ContactNames[people][contacts]}: {$ContactVals[people][contacts]}
            {if $smarty.section.contacts.rownum is even}{/if}
        {/section}
        
    {sectionelse}
        There are no values to loop through.
    {/section}
    
There were {$smarty.section.people.loop} names in this list. {include file="footer.tpl"} FOREACH ------- There is also an alternative way to loop through associative arrays that may be a bit easier for you, especially if you only need to loop over one variable. Here's an example that works with the default index.php that comes with Smarty: {foreach name=outer item=contact from=$contacts} {foreach key=key item=item from=$contact} {$smarty.foreach.outer.iteration}. contact {$key}: {$item} {/foreach} {foreachelse} no contacts {/foreach} Possible attributes: from: the array you are looping through item: the name of the variable that is the current element key: the name of the variable that is the current key (optional) name: the name of the of foreach (optional) The 'name' has to be used if you want to refer to any of the foreach properties. Properties (if 'name' is used): name: the name of foreach iteration: current iteration total: total number of iterations first: if it's first iteration last: it it's last iteration show: if foreach was shown at all Here's another neat example, dumps all properties of foreach: {foreach name=outer item=contact from=$contacts} {foreach key=key item=item from=$smarty.foreach.outer} {$key}: {$item} {/foreach} {/foreach} This should be enough to get your feet wet. Also, check out config file variables, built-in functions, custom functions, variable modifiers, all sorts of good stuff. Now go read the documentation, join the mailing list and have fun!