This is a discussion on Smarty Templates and You. Posted BY Lhotch within the 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 ...
|
|||||||
![]() |
|
|
LinkBack | 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 Please do not send me a private message asking for support. Instead use these open forums or our ticket system. Customer Area | Issue Tracker | Documentation | 68C Mods | Submit a Ticket | Twitter Last edited by Eric Barnes; 03-29-2006 at 11:32 AM. |
|
#2
|
||||
|
||||
|
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
__________________
Current Version: V4.0.9 - Developer ________________________________ |
|
#3
|
||||
|
||||
|
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. (Please note: I am not a 68C employee. I am a customer and volunteer who helps with questions where I can and the forums spam free) Set your site apart from the competition with one of my modules...... Google Map Module | You Tube Module | Google Calendar Module 68 Classifieds Important Links Customer Area | Issue Tracker | Knowledge Base | User Manuals |
|
#4
|
||||
|
||||
|
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...
__________________
Current Version: V4.0.9 - Developer ________________________________ |
|
#5
|
||||
|
||||
|
Just add:
somepage.php?debug=true |
|
#6
|
|||
|
|||
|
put the debug at the very top and the hold down your Ctrl key when you go through your site
|
|
#7
|
|||
|
|||
|
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>
}
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 |
|
#8
|
||||
|
||||
|
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:
__________________
Larry. (Please note: I am not a 68C employee. I am a customer and volunteer who helps with questions where I can and the forums spam free) Set your site apart from the competition with one of my modules...... Google Map Module | You Tube Module | Google Calendar Module 68 Classifieds Important Links Customer Area | Issue Tracker | Knowledge Base | User Manuals |
|
#9
|
|||
|
|||
|
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"; ?> |
|
#10
|
||||
|
||||
|
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 Please do not send me a private message asking for support. Instead use these open forums or our ticket system. Customer Area | Issue Tracker | Documentation | 68C Mods | Submit a Ticket | Twitter |