Smarty Template system- Separate Logic from Output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Smarty Template system- Separate Logic from Output

Post by trukfixer »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Posted originally at D2 .. may be useful here as well.. FWIW.
Mods can move this to tutorials forums, if they feel it worthy.
Basically I wrote this up for another forum where I was trying to teach the development team how to use Smarty template system.. the end results: 

Within the code, Ive put quite a bit of commentary most of it self explanatory, showing various means by which output can be quickly and easily formatted in a smarty template.  Comments and questions welcome.. Ive used smarty in nearly every project Ive done now, and find that it is an excellent developmental tool during the development and fine tuning.. the only downside to smarty is it can cause quite a bit of memory usage/processing/server load under extremely high traffic situations...(unless you have it set up under load balanced, clustered, distributed networks.. Yahoo!, for example.. )  but for the vast majority of websites, smarty is an excellent choice.... just drop it in and you're good to go!

Code: Select all

<?php
// comment 1 : normally this will be in a configuration file

//path to smarty is /home/username/public_html/test_html/smarty
//path to templates_c is gonna be templates_c
//ABSOLUTE PATH TO SMARTY
$path = "/home/trukfixr/public_html/backends/smarty/";
//REQUIRE smarty, if it cant, its gonna blow up either way
require_once($path.'libs/Smarty.class.php');
$fixpath = dirname(__FILE__);//one way of setting path to templates, especially for multi-folder sites, etc.

  $smarty = new Smarty;//instantiate a new class
  
  $smarty->compile_dir = "$fixpath/templates_c";//set compile directory.
  $smarty->template_dir = "$fixpath/templates";//specify templates directory
  
  //END COMMENT 1
  
//OK, now we want to set up a little stuff to send to the smarty template, let's create a bit of output.

$title = "Smarty Template Test";

$empty_value = "empty";//to demonstrate how you can make smarty display or not display something in a template based on a flag

$true_flag = true;//also to demonstrate output switching

$username = "any old name here";

$array = array(0 =>array("one"=>'element1',"two"=>'element2'),1 =>array("one"=>'34567',"two"=>'end of arrays'));
//a multi-dimensional array, we could also assign key names...

$options = array('1','2','3','4','5');
$values = array('number one','number two','number three', 'number four','number five');
//the above arrays are to demonstrate how simple it is to make a drop down list

//Now we assign all the above values to smarty.

$smarty->assign('title',$title);//note, you can assign any literal name to smarty, which will become the variable you use in the template, as we'll show on the next one
$smarty->assign('empty',$empty_value);
$smarty->assign('true',$true_flag);
$smarty->assign('username',$username);
$smarty->assign('multi_array',$array);
$smarty->assign('options',$options);
$smarty->assign('values',$values);

//OK, we have assigned all variables to smarty, let's display it!

$smarty->display('index.tpl');
?>
and the Template file itself:

Code: Select all

<html>
<head>
<title>{$title}</title>
</head>
<body>
Smarty Tutorial display page: <br>
Below would be the results of the tutorial code, in order presented<br>

<br><br>
{if $empty neq "empty"}
<h3> If you can see this, your code is incorrect! </h3>
{else}
<h2> Your code is great so far!</h2>
{/if}
<br><br>

{if $true}

<h2> OK, we've proved truth, so we can display</h2>

{/if}
<br><br>

Hello, {$username} How are you today?<br>
<br><br>

<h3> OUR ARRAY ELEMENTS </h3><br>
{section name=keys loop=$multi_array}
{$multi_array[keys].one}  <---- Element 1 of array <br>
{$multi_array[keys].two}  <---- Element 2 of array <br><br>
{/section}
<br><Br>

<h2> OPTION DROP DOWN</h2>
Select an option: <select name="option">
{html_options values=$options output=$values}
</select>
<br><br>
Voila!, We're done!

</body>
</html>
and the final output should be like this:

http://www.crazybri.net/test_html/index.php

Further Reading:
http://smarty.php.net/crashcourse.php Smarty's crash course
http://smarty.php.net/docs.php Smarty documentation
http://smarty.incutio.com/ Smarty Wiki


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply