Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

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:


 

templates

This directory should reside outside of the Web tree and is used to store the templates used by Smarty.

templates_c

This directory must reside inside of the Web tree and is used to store the compiled templates (PHP scripts) that are actually executed to display the Web page. This directory must be writeable by PHP and the Web server.

configs

This directory should reside outside of the Web tree and is used to store the configuration files used by templates created in Smarty (discussed later).

cache

This directory should reside outside of the Web tree and is used to store cached templates (discussed later). This directory must be writeable by PHP/the Web server.


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:

 

$template_dir

The path where Smarty will search for the templates it will use; it points to the directory with the same name you should have just created (Default: templates).

$compile_dir

The path where Smarty will store compiled versions of templates (Default: templates_c).

$plugins_dir

The path(s) where Smarty will look for plug-ins for the engine. This value is a PHP array of strings, each a path where plug-ins can be found (Default: array('plugins')).

$compile_check

Determines whether Smarty will check to see if a template needs to be recompiled. If this value is not set to true, Smarty will never update/recompile modified templates (Default: TRue).

 

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
<?php



require("Smarty.class.php");



$smarty = new Smarty;



$smarty->assign("testvar", 'PHP Unleashed');

$smarty->assign("testdata", range(1,10));

$smarty->display("test_template.tpl");
If 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.

 

 

Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Natural 34
Statistic


Last Post

 
Top! Top!