Just another WordPress.com site

Posts tagged “smarty templates

Smarty templates

As part of my database development and Drupal class assignments, I was asked to research something related to the course. I have been learning about Drupal and how content is added to a site, but I knew there were other ways of adding content other than just typing it directly into the CMS. For instance, if you have an e-commerce site with regular customers who have signed up for an account, they may have unique content that should be pulled into certain nodes within your site based on their specific login credentials.  I decided to look into Smarty templates and what they are, how they work, and why they are used.
Smarty is a template engine for PHP. It allows you to use the more simplified Smarty syntax to create templates which are compiled and transformed into PHP scripts and are cached for quick retrieval. The entire concept behind Smarty templates is to separate application logic from presentation logic. When you are trying to create a semantic HTML site and then attempt to add in PHP it gets messy and creates workflow issues when you have designers and developers working on a site simultaneously. When you have a large amount of content (especially dynamic content) and multiple pages within a site,you can use Smarty templates to separate the server-side logic from the display logic to keep coding cleaner and easier to manage on large sites.
To begin using Smarty you should visit the website at http://www.smarty.net and look around, view the link Crash Course and look through some of the sources to templates created by others in the FAQ(wiki) section of the website. After you have familiarized yourself with Smarty and how it works you can follow these steps:

  1. Download the Smarty library files from this link: http://www.smarty.net/download
  2. Unzip the files/open the Smarty folder/ copy the ‘libs’ folder into the root folder of your website
  3. Create the following folders in your libs folder: ‘templates’, ‘templates_c’, ‘configs’, ‘cache’
  4. Create an ‘index.tpl’ file in your templates folder
  5. Create an ‘index.php’ file in your htdocs folder (located in your site files)

(Note: this is only a brief overview of the steps for setup – for full documentation, see the Smarty website)

Now that your files are in place, you can open your index.php file in Notepad++ and add the following:

<?php
require_once( ‘smarty.class.php’);                  (this calls for the Smarty library/code)
$smarty = new Smarty();                  (this creates a new object – know as a smarty)
?>

This is the file where you will create objects from arrays and display them in your index.tpl file.

Here is an example of a couple simple/static smarty objects created to populate a welcome section on your homepage (refer to the Smarty website for info on using arrays):

<?php
require_once (‘smarty.class.php’);
$smarty = new Smarty();
$smarty->assign(‘name’, joe smith’);       (creates a Smarty object called $name,value = ‘joe smith’)
$smarty->assign(‘username’,’jsmith2345′);      (creates Smarty object called $username,value = ‘jsmith2345’)

$smarty->display(‘index.tpl’);  (states where these Smarty objects will be used/displayed)
?>

Now that you’ve created your Smarty objects you can write the template markup in your index.tpl file (example):

<html>
<head>
<title>Welcome Page</title>
</head>
<body>
{$smarty.now|date_format:%D}<p>
Hello, {$name|capitalize}<br>
username: {$username}<br>
</body>
</html>

The resulting output would be:

09/08/11

Hello, Joe Smith

username: jsmith2345

This is just a very basic example of using Smarty.  While using Smarty you can create an array (and arrays within an array and so on..), loop over an array, cycle through values, call functions, control caching and do many more things to make your Smarty objects and templates do what you need them to for your sites.  For more information, and to access the Smarty files visit http://www.smarty.net/.