Smarty installation
- If XAMPP is not already installed, download the current Windows XAMPP installer from its developer's website at: http://sourceforge.net/projects/xampp/. As of September 2009, the current XAMPP version was 1.7.2.
- Install the XAMPP package either at its default C:\xampp location or on a different partition, such as F:\xampp. The current default Windows installation configuration should suffice, unless you want some other configuration. The current default installation is fairly automatic--just follow the instructions.
- Create a new folder named smarty outside the (wwwroot) folder, which is the htdocs folder when running XAMPP, but still inside the xampp folder created by the XAMPP installation (C:\xampp\smarty).
- Download the latest zip file version of the Smarty Template Engine at: http://www.smarty.net/download.php and extract the zip file into any convenient temporary folder.
- Among the extracted files and folders, there should be two folders--demo and libs, plus around ten files. Copy those two folders and the files and paste them into the smarty folder you created in step 3. [You can delete the temporary folder afterwards, as you will no longer need it.]
- Also create two folders--with names templates_c and cache--and place them inside the smarty folder, but outside of the (wwwroot) folder--htdocs (C:\xampp\smarty\templates_c and C:\xampp\smarty\cache).
- Create another folder named smarty and place it inside the htdocs folder (C:\xampp\htdocs\smarty).
- Create two folders--named configs and templates--inside the smarty folder created in the previous step (C:\xampp\htdocs\smarty\configs and C:\xampp\htdocs\smarty\templates). Your templates will be placed inside the templates folder.
- Note: The next series of steps is not necessary, but it serves as a verification that you will be modifying the correct php.ini file. Earlier versions of XAMPP (before 1.7.1) had stored their php.ini files in a different location. So, this step shows the location of the active php.ini file so that you do not inadvertently modify an older, inactive php.ini file. The steps below also test the XAMPP installation before you modify anything, in the event that if something goes wrong afterwards, you will at least know that the original XAMPP configuration was once previously OK.
- Start the XAMPP Control Panel Application by double-clicking xampp-control.exe in the C:\xampp folder (i.e. C:\xampp\xampp-control.exe).
- Click the Start button next to Apache and wait until the Start button becomes a Stop button and a green Running label appears.
- Click the Start button next to MySql and wait until its green Running label appears.
- Click the Admin button next to the Stop button for Apache. A XAMPP for Windows page, whose URL is http://localhost/xampp/ will appear in your web browser. Select the language (e.g., English) you want to use if a language page appears the first time that the XAMPP for Windows page appears.
- Select the Status page from the navigation menu on the left side of the web page. If everything was set up and working OK, the top eight labels should be colored green and state that they were activated.
- Next select the phpinfo() page from the navigation menu. The value for the active Loaded Configuration File should be C:\xampp\php\php.ini if all went well.
You should always check the overall system whenever a change in configuration is made, so that troubleshooting and fixing any problems should be localized, and, thus, easier to correct. You can check out further other parameters in this file, as well as parameters in other XAMPP files of your setup, if you desire to explore around a bit.
- Start the XAMPP Control Panel Application by double-clicking xampp-control.exe in the C:\xampp folder (i.e. C:\xampp\xampp-control.exe).
- Inside the XAMPP installation, with a text editor open the active php.ini file whose path we just discovered. Search for the Paths and Directories section of this file, scroll down to the Windows subsection, and then edit the include library path by adding the location path of Smarty libraries. In other words, replace the line:
include_path = ".;C:\xampp\php\PEAR" [The line's path might differ very slightly from what is represented here.]
with the line:
include_path = ".;C:\xampp\php\pear\;C:\xampp\smarty\libs\". - Stop and restart Apache, using the Stop and Start buttons in the XAMPP Control Panel in order to set the path to Smarty.
- Set the security settings for the webserver to allow writing to these four folders.
Testing the Smarty setup
To test the Smarty installation, create two scripts--index.php and index.tpl. Place the index.php file into your webroot folder (htdocs, if running XAMPP) and place the index.tpl file inside of C:\xampp\htdocs\smarty\templates and copy-and-paste the following code into these two files.
index.php
- Code: Select all
<?php
// load Smarty library
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = 'C:/xampp/htdocs/smarty/templates';
$smarty->config_dir = 'C:/xampp/htdocs/smarty/configs';
$smarty->cache_dir = 'C:/xampp/smarty/cache';
$smarty->compile_dir = 'C:/xampp/smarty/templates_c';
$smarty->assign('name','badBadger!');
$smarty->display('index.tpl');
?>
index.tpl
- Code: Select all
<html>
<body>
Hello, {$name}!
</body>
</html>
Run the index.php file in your browser, and you should see the desired output.
-----------------------------------------------
Installing Smarty in Windows
This document assumes that your webserver and php5 is running.
Download Smarty - http://smarty.php.net
Installation - Windows, IIS/Apache, PHP5
Extract files, rename Smarty.x.x.x to smarty (suggest OUTSIDE of your www root!)
Example: d:\smarty
Run phpinfo.php to find out your php.ini location
Edit php.ini's include_path and add the location of the libs folder.
example: include_path = ".;d:\smarty\libs"
Restart IIS/Apache
Setup these two folders INSIDE your www root:
(wwwroot)/smarty/templates (this is where your templates will go)
(wwwroot)/smarty/configs
Setup these two folders OUTSIDE of your www root:
d:/smarty/templates_c
d:/smarty/cache
Setup security settings for the webserver to write to these four folders
In (wwwroot) create index.php and in (wwwroot)/smarty/templates/index.tpl with the following code:
index.php:
<?php
// load Smarty library
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = 'd:/inetpub/wwwroot/smarty/templates';
$smarty->config_dir = ' d:/inetpub/wwwroot/smarty/config';
$smarty->cache_dir = 'd:/smarty/smarty_cache';
$smarty->compile_dir = 'd:/smarty/smarty_templates_c';
$smarty->assign('name','fish boy!');
$smarty->display('index.tpl');
?>
index.tpl
<html>
<body>
Hello, {$name}!
</body>
</html>
Now open index.php in your web browser (requested from your webserver)
http://webserver/index.php
You can work this out to a referenced script/class:
smarty_connect.php:
<?php
// load Smarty library
require('Smarty.class.php');
class smarty_connect extends Smarty
{
function smarty_connect()
{
// Class Constructor.
// These automatically get set with each new instance.
$this->Smarty();
$this->template_dir = ' d:/inetpub/wwwroot/smarty/templates';
$this->config_dir = ' d:/inetpub/wwwroot/smarty/config';
$this->compile_dir = 'd:/smarty/templates_c';
$this->cache_dir = 'd:/smarty/cache';
$this->assign('app_name', 'Intranet');
}
}
?>
index.php:
<?php
require('smarty_connect.php');
$smarty = new smarty_connect;
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
?>
index.tpl:
<html>
<body>
Hello, {$name}!
</body>
</html>
If you are getting an error that Smarty.class.php isn't found chances are that your include_path isn't correct or you didn't edit the one that the webserver is using, check your phpinfo.php!
