This blog is about C++, CakePHP, Linux, PEAR, PHP, Pyrus, Apache, and MySQL and related topics.

Tuesday, October 27, 2009

How To Install Smarty Template Engine WampServer (Basic Installation)

This article will show how to install the Smarty Template Engine with WampServer.

This article is a modified version of the Basic Installation Instructions found on Smarty's website.

Once you understand the basic installation you can try the Extended Setup article which is a modified version of the Extended Installation Instructions found on Smarty's website.

This is my setup:
  • Windows XP SP3
  • WampServer 2.0i
    • -Apache 2.2.11
    • -MySQL 5.1.36
    • -PHP 5.3.0
  • Smarty 2.6.26
  • Notepad++

Table of Contents

Here is an ordered list of each step needed to complete our tutorial:
  1. Get WampServer
  2. Get Smarty
  3. Create Smarty/libs
  4. Modify php.ini
  5. Restart Apache
  6. Test
  7. Make application folder
  8. Make Smarty directories
  9. Make index.tpl
  10. Make index.php
  11. Test
Step One: Get WampServer:

This tutorial assumes that you have already installed WampServer.

Step Two: Get Smarty:

Download Smarty at http://www.smarty.net/download.php - Download the most recent .zip file.

Right click the .zip file select 'Extract All' the .zip file and you will have a folder that should have a name similar to Smarty-X.X.X, in my case it is named Smarty-2.6.26









Inside Smarty-2.6.26 there are two sub-folders called 'demo' and 'libs' as well as several files.




Step Three: Create Smarty 'libs' folder

Go to your PHP folder. (If you installed WampServer using the default configuration then your PHP folder should be at c:\wamp\bin\php\php5.3.0\ )

Create a folder called Smarty. Now copy Smarty's 'libs' folder and paste it into the Smarty folder we just created.

I could have placed the libs folder at c:\www\{application-name}\libs but "to avoid any security concerns, it is recommended (but not mandatory) to place these directories outside of the web server's document root"1 

The file structure should look like this:

c:\wamp\bin\php\php5.3.0\Smarty\libs\internals\      <this is a directory>
c:\wamp\bin\php\php5.3.0\Smarty\ libs\plugins\        <this is a directory>
c:\wamp\bin\php\php5.3.0\Smarty\libs\Config_File.class.php
c:\wamp\bin\php\php5.3.0\Smarty\ libs\debug.tpl
c:\wamp\bin\php\php5.3.0\Smarty\ libs\Smarty.class.php
c:\wamp\bin\php\php5.3.0\Smarty\ libs\Smarty_Compiler.class.php

Step Four: Modify php.ini

Next we need to edit include_path in php.ini. The php.ini file is located at c:\wamp\bin\apache2.2.11\bin\php.ini . Scroll down to the section called 'Paths and Directories'. Find the include_path. There are two include paths. One for UNIX and one for Windows. Notice how the include_path under UNIX starts with a semi-colon. The semi-colon at the beginning of a line will cause that line to be ignored. If you see a semi-colon at the beginning of the include_path for windows, remove it.  You can add multiple include_paths, seperated by semi-colons.

If this is the first and only path then the include_path directive should look something like this:

include_path = ".;c:\wamp\bin\php\php5.3.0\Smarty\libs".
If there already is a value then add a semi-colon at the end of the last path and then add the new path. Like this:
include_path = ".;c:\wamp\bin\php\php5.3.0\pear;c:\wamp\bin\php\php5.3.0\Smarty\libs"
Notice how the first value has a dot and a semi-colon preceding it and the second value is only preceded by a semi-colon.

Here is a screenshot of how my php.ini looks like after the change.





Step Five: Restart Apache

After you save the php.ini file. Restart Apache for the changes to take effect.



To verify the include_path has been updated open your browser to http://localhost/. You should be on the WampServer Configuration Page.



About a quarter page down click on phpinfo(). This brings you to the PHP configuration page. Do a 'Ctrl-F' and search for 'include_path'.



Do you see the path you just put in the php.ini file? If you don't see it then go back and check the following:

  • Did you edit the right php.ini file? Make sure. There's more than one php.ini file. (The php.ini located at c:\bin\php\php5.3.0\php.ini is not the right file!)
  • Did you save the php.ini file? Very easy to check. Open the file and look if your change is still there.
  • Did you enter the path correctly?  (right path? / correct spelling?)
Step Six: Test

Before going further we should test that php.ini is finding Smarty. So let's create a simple webpage that will call the Smarty.class.php and see what happens. Open a text editor. Notepad will do (Start>All Programs>Accessories>Notepad). I am using Notepad ++ .

Here is the code for the page:

<html>
<head>
<title>Smarty test</title>
</head>
<body>
<?php
// NOTE: Smarty has a capital 'S'
require_once('Smarty.class.php');
$smarty = new Smarty();
?>
<p>If this is all you see then the test passed. If you see an error saying Smarty.class.php could not be found then go to 'http://www.smarty.net/manual/en/installing.smarty.basic.php' and troubleshoot the problem.</p>
</body>
</html>

Just copy the code and paste it in a text file and save this text file as 'c:\wamp\www\smarty_test.php'.

Open your browser and type this address in the address window:  http:\\localhost\smarty_test.php.

If you see an error, go back and make sure you followed each step correctly.

Step Seven: Make Application Folder:

If the test passed then we move on to the next step which is to create a folder for the application. The name of the folder should be the same as the name of our application. The application folder should be located in the document root. The location of the document root can be verified by looking at our PHP configuration page which is accessed by going to http://localhost/ then clicking on phpinfo(). Then do a search for 'DOCUMENT_ROOT'.

The directory structure should look like this:
  • c:\wamp\www  (this is your document root)
  • c:\wamp\www\application-name
Step Eight: Make Smarty Directories:

After the application folder is set up it is time to create the Smarty directories:
  • c:\wamp\www\application-name\templates
  • c:\wamp\www\application-name\templates_c
  • c:\wamp\www\application-name\configs
  • c:\wamp\www\application-name\cache
Step Nine: Test


Let's do a test to make sure everything is working fine. We'll just grab the code of Smarty's page for this one.

Create a new file called index.tpl inside the 'templates' folder. Place the following code inside this file:
{* Smarty *}
Hello {$name}, welcome to Smarty!
Create a new file called index.php inside the 'application-name' folder. Place the following code inside this file:

<?php

require_once(SMARTY_DIR 'Smarty.class.php');

$smarty = new Smarty();

$smarty->template_dir 'templates';
$smarty->compile_dir  'templates_c';
$smarty->config_dir   'configs';
$smarty->cache_dir    'cache';

$smarty->assign('name','Jonathan');

//** un-comment the following line to show the debug console
//$smarty->debugging = true;

$smarty->display('index.tpl');

?>

If everything goes as planned you should see the following on the screen:

Hello Jonathan, welcome to Smarty!

VoilĂ ! You have successfully installed Smarty with WampServer




2 comments:

  1. Does anybody know how to contact Jon/John, he deserves a medal.

    ReplyDelete
  2. Hello Jon/John/Jonathan:
    Line:2 in the last bit of code for 'Step 9 Test'
    failed. Hacking at it I removed the SMARTY_DIR plus the concatination and everything worked fine.
    Could you please look into it and give me your opinion.... Kev

    ReplyDelete

Followers