Smarty Templates and You. Posted BY Lhotch

Discussion in 'Templates, HTML, CSS, and Design Help' started by Eric Barnes, Mar 29, 2006.

  1. Eric Barnes Guest

    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:
    [LEFT]User Information:
    Name: {$name}
    Address: {$address}[/LEFT]
    
    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.
  2. dawyatt Senior Member from OHIO!

    {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
  3. Lhotch curmudgeon

    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.
  4. dawyatt Senior Member from OHIO!

    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...
  5. John Snyder Staff

    Just add:

    somepage.php?debug=true
  6. dfoshe Customer

    put the debug at the very top and the hold down your Ctrl key when you go through your site
  7. taiwanted Customer

    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.
  8. Lhotch curmudgeon

    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:

    {if $body=="home.tpl.php"}
    <
    td valign="top" class="padding">
    <!-- 
    // Right Column // -->
    echo "the right column stuff";
    <!-- 
    // End Right Column // -->
    </td>
    {/if}
  9. taiwanted Customer

    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. Eric Barnes Guest

  11. Lhotch curmudgeon

    Smarty has its own program like functions, what I did above is smarty code not PHP. For php the "if" would be outside the braces and they wouldnt be curly brackets either.

    To use php inside of a template you need to escape the code so the smarty compiler doesnt try to treat it as smarty or html. To do this you simply surround your php with tags like so...

    {php} {/php}
  12. taiwanted Customer

    Excellent, many thanks for the help.
  13. castus Customer

    Is there anyway to pass variables to the smarty functions?

    I have the category id, and would like to pass it a new function I have made, so I can show the links with this category id as it's parent.
  14. castus Customer

    It's ok - I have found it!
  15. Wes New Member

    I am hoping someone can help me if they have some time.

    My goal:
    When browsing categories some of them have sub-categories. For example: Main category is "Parts" and includes subs: "Suspension" "Lights" and more. I want users to be able to browse all ads, as well as narrow down to viewing all the ads at once in the "parts" section. So in other words "Parts" and "Lighting" ads would all be displayed as well as the ads from all the other subs in that category. Of course if they want to view only "Suspension" they can just click on that category.

    In order for users to browse all ads I have gone to the category.php file and changed "$showlistings="N" to "$showlistings="Y"; which let users browse all categories but it made "Y's" show up in the browse category box and it shows all ads on the website in every category. Here is a link so you can better understand what I am trying to explain: http://selloffroad.com/category.php?cat=parts

    If anyone has any insight please help. Thank you!
  16. freeze2 Super Moderator

    I dug back into some old notes which I used to to the same...give this a go, it might work:

    *********************************************

    in category.php:

    Find:

    Code:
    $options['section']=$sec;
    Replace with:

    Code:
    $arr_childs = array($sec);
    $Categories->get_ids($arr_childs);
    $options['cats'] = $arr_childs;
  17. Wes New Member

    Thank you for your response freeze2,
    I replaced the code you advised and unfortunately now instead of showing ALL the ads all the time it always says "We are sorry no items were found"

    You can check the link in my first post to see.
  18. Mike-N-Tosh Owner

    The reason that you have a bunch of "Y"'s showing up in your browse categories is because you put "{$showlistings = 'Y'}" in your categories/browse.tpl file. If you remove that, your "Y"'s will disappear.

    PLEASE ENSURE THAT YOU WORK FROM A BACKUP COPY OF ANY FILE THAT YOU ARE WORKING ON!

    Now, as I thought I mentioned in a different post, you need to set the $showlistings = 'Y' to show the listings. This is in the category.php file in the root directory around line #28. just change the 'N' to 'Y'.

    What Freeze2 posted should work except the additional code posted should actually be under the first line (NOT replacing, because everything in the mySQL query is based on having the original $sec (the category you're browsing). I haven't tested any of this, but knowing the code as I do, it should work as you have requested.

    In the category.php file look for the $options['section'] = $sec; around line 98.
    PHP:
    $options['section']=$sec// this should already be there
    $arr_childs = array($sec);
    $Categories->get_ids($arr_childs);
    $options['cats'] = $arr_childs;
  19. Wes New Member

    Thank you for the response,

    The extra "Y" problem has been fixed

    Now I went and made sure the original $options['section'] = $sec; is in the code and added the 3 lines under it. It still says "We are sorry, no items could be found"
    When I take the 3 lines away and leave the original, all the ads on the website show up again for browsing, but obviously not the ones only pertaining to the category I'm in.
  20. Mike-N-Tosh Owner

    For this to be getting ALL the site listings, there must have been some other change(s) made. Please restore your category.php file from backup. Do one thing at a time and test. Start with simply changing the $showlistings = 'Y';

    In looking at the code that Freeze2 provided again, I see how you wouldn't get results. I believe it should actually be:
    PHP:
    $options['section']=$sec// this should already be there
    $arr_childs $Categories->get_ids($sec); // get the subcategory id's in an array
    $options['cats'] = $arr_childs// set options for getting listings in subcat's

Share This Page