smarty template engine
Wednesday, August 20, 2008  
download | documentation | faq | forum | mailing lists | changelog | contribs 


search for in the  


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. The example given is a fairly simple yet complete mini-framework for building Smarty driven applications quickly and easily. 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, PEAR 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
/web/www.example.com/smarty/guestbook/libs/sql.lib.php

Lets go over each one:

/web/www.example.com/docs/

The /docs/ directory is our web server document root.

/web/www.example.com/docs/guestbook/

/guestbook/ is the subdirectory where our application is accessed by the browser.

/web/www.example.com/docs/guestbook/index.php

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.

/web/www.example.com/smarty/guestbook/

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 web server means to stop direct access to application files under doc root.

/web/www.example.com/smarty/guestbook/templates/

This where we will put our Smarty template files.

/web/www.example.com/smarty/guestbook/templates_c/

This is where Smarty places its compiled template files. If you installed this correctly, the web server user running PHP has write access here. For most intents and purposes you can just ignore this directory.

/web/www.example.com/smarty/guestbook/configs/

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.

/web/www.example.com/smarty/guestbook/cache/

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. Much the same as the compile directory, it can be ignored.

/web/www.example.com/smarty/guestbook/libs/

/libs/ is the directory we'll keep our main application files.

/web/www.example.com/smarty/guestbook/libs/guestbook_setup.php

guestbook_setup.php is where we'll keep some basic initialization information for our application.

/web/www.example.com/smarty/guestbook/libs/guestbook.lib.php

guestbook.lib.php is where we'll keep the bulk of our application logic.

/web/www.example.com/smarty/guestbook/libs/sql.lib.php

sql.lib.php is where we keep our database access logic.

[Page 1] [Page 2] [Page 3] [Page 4] [Page 5]


 

credits 

Smarty Copyright © 2002-2008
New Digital Group, Inc.

All rights reserved.