Support Forums

Smarty Templates and You. Posted BY Lhotch

This is a discussion on Smarty Templates and You. Posted BY Lhotch within the Templates, HTML, CSS, and Design Help forums, part of the General category; Just so everyone is aware, this is my first encounter with Smarty templates and honestly I was a bit confused ...


Go Back   68 Classifieds Forums > General > Templates, HTML, CSS, and Design Help

Reply
 
Thread Tools Display Modes
Old 03-29-2006, 11:27 AM   #1
68 Classifieds Staff
 
Eric Barnes's Avatar
 
Join Date: Mar 2006
Location: Belmont, NC
Posts: 5,489
Rep Power: 136
Eric Barnes is just really nice Eric Barnes is just really nice
Thumbs up Smarty Templates and You. Posted BY Lhotch

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......
  1. Joe internet surfer points his browser to your domain which triggers the web server to parse index.php.
  2. 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.
  3. 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.
  1. Queries to the database are made to retrieve your categories.
  2. The template home.tpl.php is assigned to the variable called "body"
  3. 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.
  1. Queries to the database are made to retrieve the top listings.
  2. A template is assigned to the variable $body
  3. 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.
__________________
Eric Barnes
68 Classifieds Developer
Customer Area | Issue Tracker | Documentation | 68C Mods | 68 @ Twitter | My Modules

Last edited by Eric Barnes; 03-29-2006 at 11:32 AM.
Eric Barnes is offline   Reply With Quote
Old 08-11-2006, 02:46 PM   #2
Senior Member from OHIO!
 
dawyatt's Avatar
 
Join Date: Mar 2006
Location: Sandusky, OH
Posts: 224
Rep Power: 28
dawyatt is on a distinguished road
Default {debug} - tried but not working

I tried putting the {debug} at the top of the layout.tpl.php page and nothing happens.

I do have popup enabled , but still nothing...

I am using this in my /testsite area ...

I have tried putting at the very top... nothing
in the body... nothing

Can someone help me here...? I want to get a better handle on how 68 and smarty are working together...

thanks
__________________
The CEO & Boss!
_____________________________________
Current Version: V4.0.9 - Developer
________________________________
dawyatt is offline   Reply With Quote
Old 08-11-2006, 03:22 PM   #3
curmudgeon
 
Join Date: Mar 2006
Posts: 5,416
Rep Power: 139
Lhotch is a glorious beacon of light Lhotch is a glorious beacon of light Lhotch is a glorious beacon of light
Default

The only thing I can think of is that you have a popup blocker in place. Last resort you could try and clear the contents of /templates_c and see if that helps.
__________________
Larry

Knowledge learned is more valuable than knowledge given.
Lhotch is offline   Reply With Quote
Old 08-11-2006, 03:56 PM   #4
Senior Member from OHIO!
 
dawyatt's Avatar
 
Join Date: Mar 2006
Location: Sandusky, OH
Posts: 224
Rep Power: 28
dawyatt is on a distinguished road
Default

NO.. I have pop ups enabled meaning they are allowed....

I have tried a few things to no avail.. I guess I will plug away.... it really is the best way to learn so I'll see what I can figure out, but if anyone has suggestions than I am open to that too!

Thanks...
__________________
The CEO & Boss!
_____________________________________
Current Version: V4.0.9 - Developer
________________________________
dawyatt is offline   Reply With Quote
Old 08-11-2006, 04:59 PM   #5
Staff
 
Join Date: Mar 2006
Location: New Jersey
Posts: 2,180
Rep Power: 70
John Snyder is a jewel in the rough
Default

Just add:

somepage.php?debug=true
__________________
John
68C Staff

68C Downloads | Report a Bug | Knowledge Base
If you have a current support subscription, you can Submit a Support Ticket
John Snyder is offline   Reply With Quote
Old 05-27-2007, 12:44 AM   #6
Junior Member
 
Join Date: Apr 2007
Posts: 1
Rep Power: 0
dfoshe is on a distinguished road
Default

Quote:
Originally Posted by dawyatt
NO.. I have pop ups enabled meaning they are allowed....

I have tried a few things to no avail.. I guess I will plug away.... it really is the best way to learn so I'll see what I can figure out, but if anyone has suggestions than I am open to that too!

Thanks...
put the debug at the very top and the hold down your Ctrl key when you go through your site
dfoshe is offline   Reply With Quote
Old 05-30-2007, 04:56 AM   #7
Junior Member
 
Join Date: Apr 2007
Posts: 6
Rep Power: 0
taiwanted is on a distinguished road
Default Extending the templates

Thanks for that Larry. I am preparing to redesign our site and the info in your post was invaluable.

I do have a couple of questions. I have had a look through the code and can see that php is disabled from being used in the template files.

But, I would like the front page to display differently from the rest of the pages, to display an extra column (for ads and other content from our other sites) on the right. Therefore, I would like to put a check in layout.tpl.php along the lines of:

Code:
if (this is the front page) {
<td valign="top" class="padding">
<!-- // Right Column // -->
echo "the right column stuff";
<!-- // End Right Column // -->
</td>
}
If this is not possible, are there other options? Would it be possible to make a layout.tpl.php file for the front page, calling it front_layout.tpl.php for example and then calling that from index.php instead?

That would get round the need for the check as above, but it would still not let me use php in the right hand col.

Would "including" a file in index.php to create front_layout.tpl.php work? It seems a bit cumbersome though.

Any words of wisdom from the experts would be appreciated.

If this is better in a different thread please move.

Last edited by taiwanted; 05-30-2007 at 06:04 AM. Reason: I have thought of some questions
taiwanted is offline   Reply With Quote
Old 05-30-2007, 10:55 AM   #8
curmudgeon
 
Join Date: Mar 2006
Posts: 5,416
Rep Power: 139
Lhotch is a glorious beacon of light Lhotch is a glorious beacon of light Lhotch is a glorious beacon of light
Default

Smarty does employ some basic logic so you dont need php to do it, you can use smarty.

By using the debug console you can see that when you view the index.php file it displays the home.tpl.php template, that value is passed from index.php to layout.tpl.php as a smarty variable called $body.

With the above in mind, you can use smarty to check the value of $body and if it equals home.tpl.php then display your extra html.

PHP Code:
{if $body=="home.tpl.php"}
<
td valign="top" class="padding">
<!-- 
// Right Column // -->
echo "the right column stuff";
<!-- 
// End Right Column // -->
</td>
{/if} 
__________________
Larry

Knowledge learned is more valuable than knowledge given.
Lhotch is offline   Reply With Quote
Old 05-30-2007, 11:27 AM   #9
Junior Member
 
Join Date: Apr 2007
Posts: 6
Rep Power: 0
taiwanted is on a distinguished road
Default

Thanks for the reply.

Actually I can't get php to work in the templates at all. But, creating a new template file has worked to solve the front page problem.

I do need to use php in the templates though, as I want to call some dynamic features for the right hand column.

It seems like you are saying that I can use php. Am I doing something wrong? I tried using this a a test in layout.tpl.php but it didn't work:

<?php echo "hello world"; ?>
taiwanted is offline   Reply With Quote
Old 05-30-2007, 11:40 AM   #10
68 Classifieds Staff
 
Eric Barnes's Avatar
 
Join Date: Mar 2006
Location: Belmont, NC
Posts: 5,489
Rep Power: 136
Eric Barnes is just really nice Eric Barnes is just really nice
Default

You can include php but you would need to use smarty's function:
http://smarty.php.net/manual/en/lang...nction.php.php
__________________
Eric Barnes
68 Classifieds Developer
Customer Area | Issue Tracker | Documentation | 68C Mods | 68 @ Twitter | My Modules
Eric Barnes is offline   Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 02:09 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2011, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0