Smarty

From DreamHost

Jump to: navigation, search

Contents

Overview

Smarty is a templating system for PHP. It allows you to easily create a consistant, efficient layout for use by many different pages. You should understand basic PHP before beginning use of Smarty.

Installation

Installation of Smarty is fairly straight forward. In cases when you need to create a file, you can do so from the command line using Vim (or another editor) or you can create the files and upload them via FTP. To use Vim, just do "vim filename.ext" and it will open that file or create it if it doesn't exist. Once in Vim, you use "i" to enter insert mode and "esc" to exit insert mode. To save and quit, do ":wq" (that's actually two commands ":w" to save and ":q" to quit). ":q!" will quit without saving.

Access the Code

The files are available at http://smarty.php.net/ so you can download the most recent version and upload it with FTP to your home directory ("/home/USERNAME/" or "~/"). Alternately, you can use wget, if you know the filename:

wget http://www.smarty.net/do_download.php?download_file=Smarty-2.6.20.tar.gz


To get the files out of the gzip, run this from the command line:

tar -zxvf Smarty-2.6.20.tar.gz 

Don't worry, this creates a folder and puts everything in there, so you won't have a huge number of files filling up your home directory. Feel free to browse the created directory, but you really just need the lib files, so do this:

mkdir smarty
cp -r Smarty-2.6.20/libs/* smarty/

Prepare and Test

Now that you have the code you need, you need to set it up so that you can test it out. Change into the smarty directory and create directories called "templates," "templates_c," "cache," and "configs."

cd smarty
mkdir templates
mkdir templates_c
mkdir cache
mkdir configs

Let's go ahead and create a Smarty file. First, change to your web directory (/home/USERNAME/DOMAIN/). Create a test file (I simply called mine "test.php") and fill it with the following:

<?php

// put full path to Smarty.class.php
require('/home/USERNAME/smarty/Smarty.class.php');
$smarty = new Smarty();

$smarty->template_dir = '/home/USERNAME/smarty/templates';
$smarty->compile_dir = '/home/USERNAME/smarty/templates_c';
$smarty->cache_dir = '/home/USERNAME/smarty/cache';
$smarty->config_dir = '/home/USERNAME/smarty/configs';

$smarty->assign('test_var', 'It works!');
$smarty->display('index.tpl');

?>

Make sure to change "USERNAME" above to your username.

That file calls on the Smarty template "index.tpl" to display the output. You better create that template!

Change to the templates directory ("/home/USERNAME/smarty/templates"). Create a file called "index.tpl" and fill it with the following:

<html>
<head>
<title>Smarty Test</title>
</head>
<body>
Guess what...  {$test_var}!
</body>
</html>

To make sure it works, visit your website and access the php file you created (such as "http://www.domain.com/test.php"). You should see:

Guess what... It works!!

If it works, you're ready to start making use of Smarty. If it didn't, you'd better figure out what you did wrong!

Personal tools