Installing Smarty

Installing Smarty is fairly straightforward, there are a few things to be aware of. Smarty creates PHP scripts from the templates. This usually means allowing user "nobody" (or whomever the web server runs as) to have permission to write the files. Each installation of a Smarty application minimally needs a templates directory and a compiled templates directory. If you use configuration files you will also need a directory for those. By default these are named "templates", "templates_c" and "configs" respectively. If you plan on using caching, you will need to create a "cache" directory, also with permission to write files.

Technical Note: You can get around the need to allow the web server user write access to compile templates. Smarty needs to compile the templates only once. This can be done from the command line, using the CGI version of PHP. example: "php -q index.php". Once the templates are compiled, they should run fine from the web environment. If you change a template, you must recompile from the command line again. If you do not have the CGI version of PHP available and you are concerned about world-writable directory access, you can chmod 777 the compile_dir, let the templates compile once as the web server user, then change the directory mode to 755. If you are using the caching feature of Smarty, the cache directory must always have write access for the web server user.

Technical Note: If you do not have access to the php.ini file, you can change non-server settings (such as your include_path) with the ini_set() command (available in PHP 4.0.4 or later.) example: ini_set("include_path",".:/usr/local/lib/php");

Copy the Smarty.class.php, Smarty.addons.php and Config_File.class.php scripts to a directory that is in your PHP include_path. NOTE: PHP will try to create a directory alongside the executing script called "templates_c". Be sure that directory permissions allow this to happen. You will see PHP error messages if this fails. You can also create the directory yourself before hand, and change the file ownership accordingly. See below.

Technical Note: If you don't want to use include_path to find the Smarty files, you can set the SMARTY_DIR constant to the full path to your Smarty library files. Be sure the path ends with a slash!

Example 2-1. Example of installing Smarty

# be sure you are in the web server document tree
# this assumes your web server runs as user "nobody"
# and you are in a un*x environment
gtar -zxvf Smarty-[version].tar.gz
mkdir templates_c
chown nobody:nobody templates_c
chmod 700 templates_c
# if you are using caching, do the following
mkdir cache
chown nobody:nobody cache
chmod 700 cache

Next, try running the index.php script from your web browser.