| PHP Tutorial | Installing Smarty |
After you have downloaded and extracted Smarty, a number of directories and files are created. Of all of this, only a few things are a part of the Smarty engine itselfthree classes (Smarty.class.php, Smarty_Compile.class.php, and Config_File.class.php) and the plugins directory. All three files must be copied into the include path of your PHP installation. If you do not have the capability to copy these files into a directory that is a part of the include path (for example, if you do not have access to the php.ini file or .htaccess files) you have two options:
Option 1: You can copy these files into a directory and then set the include_path at runtime by using the ini_set() and ini_get() PHP functions:
<?php ini_set("include_path",
ini_get("include_path").";/path/to/smarty/files/"); ?>
Option 2: You can copy these files into a directory and define the SMARTY_DIR constant to the appropriate path prior to using the Smarty engine:
<?php define('SMARTY_DIR', '/path/to/smarty/files/'); ?>
The next step in the installation process is to create at least three (perhaps four) directories for Smarty to use. When you create these directories, it is important to be mindful of possible security consequences and behave accordingly. Following is a listing of the directories that need to be created for use with Smarty:
Each of these directories must have the appropriate permissions to be accessed by PHP (configs and templates can be read-only; the remainder require write permissions). For those not familiar with the terminology, the phrase "outside of the Web tree" means a directory that cannot be accessed through the Web server via a browser.
After you have copied the appropriate files and created the necessary directories, the next step is to configure the Smarty engine. You do this by opening the Smarty.class.php file and modifying the appropriate member variables (the ones at the top of the class). Although the class itself briefly describes each variable, following is a reference of some of the important configuration variables available to the Smarty engine:
After you have completed adjusting the configuration variables, you should test Smarty to see if things are working properly.
Test Template for Smarty
The value below should be 'PHP Unleashed':<BR>
{$testvar}<BR><BR>
The below should be a table with the numbers 1 through 10 in it:
<TABLE CELLPADDING=3 BORDER=1>
<TR>
{section name=testsection loop=$testdata}
<TD>{$testdata[testsection]}</TD>
{/section}
</TR>
</TABLE>
<BR>
There are {$testvar|count_characters} characters in the phrase '{$testvar}'.
Listing 7.8. Test Script for Smarty
<?phpIf the test script causes an error or otherwise fails to function properly, the first thing you should do is double-check to ensure that you have created the necessary directories properly and with the proper permissions. Also, be sure to double-check within your Smarty.class.php file to make sure the appropriate configuration variables relating to these directories have been properly set. If all else fails, check the Smarty documentation for more information. On the other hand, if the test script successfully executedcongratulations, Smarty is now installed on your server and you are ready to move on to how it works.
require("Smarty.class.php");
$smarty = new Smarty;
$smarty->assign("testvar", 'PHP Unleashed');
$smarty->assign("testdata", range(1,10));
$smarty->display("test_template.tpl");
| Users' Comments (0) |
|
No comment posted









