|
|||||||
| HTML, CSS, and Design Help Need to ask some questions about your sites html, css, or design? This is the place. |
|
|
Thread Tools | Display Modes |
|
#1
|
||||
|
||||
|
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:
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......
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.
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.
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.
__________________
Eric Barnes 68 Classifieds Developer "If a wood chuck could chug beer how much beer could a wood chuck chug?" Customer Area | Issue Tracker | Knowledge Base | User Manuals | Twittering Last edited by suzkaw; 03-29-2006 at 10:32 AM. |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|