O'Reilly logo

Learning PHP, MySQL, and JavaScript by Robin Nixon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Scripts

You are now ready to create some Smarty scripts. Type in the code in Example 12-1 and save it as smarty.php.

Example 12-1. The smarty.php program
<?php // smarty.php
$path = $_SERVER['DOCUMENT_ROOT'];
require "$path/Smarty/Smarty.class.php";

$smarty = new Smarty();
$smarty->template_dir = "$path/temp/smarty/templates";
$smarty->compile_dir  = "$path/temp/smarty/templates_c";
$smarty->cache_dir    = "$path/temp/smarty/cache";
$smarty->config_dir   = "$path/temp/smarty/configs";

$smarty->assign('title', 'Test Web Page');
$smarty->display("index.tpl");
?>

This program tells Smarty where it can find the Smarty.class.php file, and where your Smarty templates are located. Because we will be using both .php and .tpl template files in this chapter, I have included everything you need in each file.

Example 12-1 looks up the document root and sets the variable $path to that value. It then uses $path as a prefix for fetching the Smarty class files and the template files from the temp folder. This saves you the maintenance hassle of hard-wiring full path names into the code.

Note the penultimate $smarty->assign command. This creates a Smarty variable called title and assigns it the string value “Test Web Page”. You’ll see why shortly.

Once you have typed the program in, save it using the filename smarty.php into the temp directory you created earlier.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required