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.
<?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.
Get Learning PHP, MySQL, and JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.