Just so everyone is aware, this is my first encounter with Smarty templates and honestly I was a bit confused when I first dove into the scripts. My goal here is simply to share what I have learned in an effort to reduce the learning curve others may face.
Additionally, my intention is not to recreate the wheel and describe what Smarty templates are since the Smarty web page contains all of that ( smarty.php.net). My goal is to help bridge the gap between the info contained in the Smarty docs and the way Smarty templates fit into the 68 classifieds application. All references I make will be based on 68 Classifieds V3.0.5 Default templates but should be easy enough to apply to add on templates you may have purchased.
Looking at the crash course on the smarty web site we see this very simple example of how Smarty is used to separate the application code (ie PHP script) from the presentation (ie html, javascript etc). Its very straight forward and should be easy enough for people who dont even have PHP experience to grasp the concept of.
index.php (ie the application code)
Code:
include('Smarty.class.php');
// create object
$smarty = new Smarty;
// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');
// display it
$smarty->display('index.tpl');
index.tpl (ie the presentation template)
Code:
User Information:
Name: {$name}
Address: {$address}
output html sent to browser.
Code:
User Information:
Name: george smith
Address: 45th & Harris
The above simple example is very basic and of course in the real world we probably wouldnt go to the trouble of using PHP and templates if we were just going to assign a static value to a variable (ie
smarty->assign('name', 'george smith'). We would be using PHP in
conjunction with a database to retrieve records and assign data from the database to the variable in PHP and then use the Smarty templates to handle the presentation, which is exactly what is done in 68 classifieds.
Trying to take the above simple example and make sense of the 68 Classified layout at first can be a bit confusing. For starters the 68 Classifieds doesnt directly include the Smarty.class.php class but instead rely's on the init.php file which includes the Smarty class for us as well as doing a few other things. With that important little tidbit out of the way lets start at the top and breakdown the general flow of the 68 Classifieds layout. A typical scenario would look like this......
- Joe internet surfer points his browser to your domain which triggers the web server to parse index.php.
- index.php is parsed and the code within is executed which may include the script performing database lookups, math functions calling for templates to be displayed etc.
- The result of the parsed code is sent to the browser for Joe surfer to view and interact with.
The visual layout of 68 Classifieds is broken into 2 basic elements. The navigation framework and the body or content. The framework consists of the header image, the navigation menu and the footer. The body consists of various other things depending on which navigation link your visitor clicks on.
The navigation framework is handled by the file layout.tpl.php where as the various other templates are used in the body or content section of this framework.
Since the Smarty templates separate the logic from the presentation
everything that happens on the site will start at the logical level first
(ie php) and from there variables will be assigned to Smarty and templates will be called either via the Smarty {display} function to directly display a template or the name of the template will be assigned to a variable....or both.
To further illustrate lets look at the index.php file. When Joe Internet
Surfer points his browser at your domain the index.php file is parsed and in a nutshell the following ocures.
- Queries to the database are made to retrieve your categories.
- The template home.tpl.php is assigned to the variable called "body"
- The template layout.tpl.php is displayed.
As I mentioned above, layout.tpl.php is the main site framework which contains the header, navigation menus and the footer. As the layout.tpl.php file is handled it displays all of our typical HTML header info (ie meta tags etc), displays our navigation menu and then it calls upon the Smarty variable $body to be displayed as the page content. As I showed in the list above home.tpl.php was assigned to the variable $body so your page will in essence be comprised of 2 templates. layout.tpl.php for the framework and home.tpl.php as the body.
To further clarify lets take it a step farther and assume Joe Surfer just landed on your site (ie index.php) and the 1st thing he wants to do is check out the "Top Listings" by clicking on the link in the navigation menu. Just as with the above example with index.php we have a similar logical flow.
- Queries to the database are made to retrieve the top listings.
- A template is assigned to the variable $body
- The template layout.tpl.php is displayed.
Once again the layout.tpl.php template is displayed to the user with the typical header, navigation and footer but this time instead of displaying the home.tpl.php template for its body, the file toplistings.php (which is the file linked to from the "Top Listings" navigation menu item) assigns either showlistings.tpl.php or showlistings2.tpl.php (depending on your site settings)to the variable $body so it is in turn displayed in the body or content portion of your page.
The same general idea applies to all the links weather your clicking a link in the navigation menu or a link in the body portion of the site, such as drilling down into the various categories or viewing the specifics of an ad. The link will point to an *.php file which will assign a template to the body variable and then request the layout.tpl.php template be displayed....which in turn calls another template to display as its body.
Smarty gives people who are not familiar with PHP a set of tools that
are very PHP'ish (for lack of a better term) but much more limited in
scale and thus easier to learn and employ. As many of you may have noticed, many of the solutions various people have posted no longer even involve editing the actual PHP files but just require you to make a change in the template file (ie *.tpl.php).
Much of the data that people want to tap into or the logic people want to apply to the displaying of pages and such can be easily done at the presentation layer without knowing PHP.
For the adventurous, Smarty includes the ability for you to see what
variables are being passed from the PHP scripts to the templates. The
easiest way to do this is to open the layout.tpl.php file and insert
{debug} at the top of the file. Evertime the file layout.tpl.php file is
displayed a new window will pop-up showing you the variables and values of the data being passed from the *.php file to the *.tpl.php template file. Use with caution however since visitors to your site will also get this popup so its best used only momentarily to locate a variable/value or on a test installation for developing new template modifications.
Hope this has been of some help!
Larry.