Sample Application: Guestbook
This is an example of a full working PHP application using Smarty. The purpose of this example is to demonstrate how Smarty ties in with an application and how to separate your presentation. Once you understand the concepts of presentation separation, you should be able to apply them to any type of programming pattern. With that said, you may use the following code at your own discretion, and at your own risk.
You can download the source of this Sample Application here. This is not a guide on how to setup Apache or MySQL. Be sure you know these things or have handy references to them. If you are using alternatives, you will need to make appropriate adjustments in the code.
We will be building a guestbook where users can sign it or view it. There is no administration interface. We will be covering a few programming topics that involve Smarty such as form processing and database data retrieval and display.
This example extends the guestbook application setup given in the installation guide for Smarty, so we'll build on top of that. Here are the files we'll start with for our app:
guestbook app files/directories
/web/www.example.com/docs/ /web/www.example.com/docs/guestbook/ /web/www.example.com/docs/guestbook/index.php /web/www.example.com/smarty/guestbook/ /web/www.example.com/smarty/guestbook/templates/ /web/www.example.com/smarty/guestbook/templates_c/ /web/www.example.com/smarty/guestbook/configs/ /web/www.example.com/smarty/guestbook/cache/ /web/www.example.com/smarty/guestbook/libs/ /web/www.example.com/smarty/guestbook/libs/guestbook_setup.php /web/www.example.com/smarty/guestbook/libs/guestbook.lib.php
Lets go over each one:
The /docs/ directory is our web server document root.
/guestbook/ is the subdirectory where our application is accessed by the browser.
index.php will be the entry point of our application. The web browser will be accessing this script directly via http://www.example.com/guestbook/index.php.
This is the directory we will keep all files for our guestbook app that do not need to be under doc root. Whether you choose to keep files under doc root is up to you, but for this example we follow the practice of putting only files directly accessed by the browser there. You could also use Apache .htaccess or other dedicated server means to stop direct access to application files under doc root.
This where we will put our Smarty template files.
This is where Smarty places its compiled template files. If you installed this correctly, the web server user running PHP has write access here. You should not manipulate files in this directory, they are generated by Smarty.
This is where we keep config files for our application. Config files are a place to store information that you want accessible from either the templates or the application. These are not PHP scripts, they are text files parsed by the Smarty config file parser.
This is where Smarty puts its cache files. This directory is only used if Smarty caching features are enabled. If you installed this correctly, the web server user running PHP has write access here. You should not manipulate files in this directory, they are generated by Smarty.
/libs/ is the directory we'll keep our main application files.
guestbook_setup.php is where we'll keep some basic initialization information for our application.
guestbook.lib.php is where we'll keep the bulk of our application logic.