SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Orlando, FL
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating/using subtemplates in Smarty

    Does anyone know if it's possible to use subtemplates with Smarty. If so, could you please provide a simple example. Thanks.

  2. #2
    SitePoint Zealot sike's Avatar
    Join Date
    Oct 2002
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    this is really not an advanced topic... but anyway : http://smarty.php.net/manual/en/lang...on.include.php

    Sike

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Orlando, FL
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the post. It's actually not what I was looking for though. I know how to include templates. I was more interested in knowing if there was a technique to use a "master template", and have areas that get rendered as sub-templates (this isn't the same thing as "include header", "include nav", "include footer"). It's something that's possible with Mojavi.
    Here's an example of the thinkiing.
    Have 1 template file that has the overall layout, header, nav, footer, etc. (some of which may be dynamic using includes)
    Have a template file for each "page" that represents the body content of the page.
    Load the master template.
    Load the body template, apply any variables to the body content template.
    Apply the body template to the body section ({body} placeholder) of the master template
    Render the master template.

    Sorry if you thought this was the wrong forum.

    Thanks,

    Jeff

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Orlando, FL
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,
    I finally figured this one out.
    The trick is to create 2 (or more) smarty objects, and use "fetch()" to render the subtemplates (instead of display) because fetch() returns a value, whereas display() prints the content (display() simply calls fetch passing in a parameter of $display=true).

    Here's some example code:
    Code:
     <?php
    // the includes class instantiates both smarty objects
    require_once("includes/mysmarty.php");
    
    // ... get some data, apply  to the body template variables here
    
    // now assign the body to the {body} variable in the master template
    $smarty->assign('body', $smartyBody->fetch('unique_body.tpl'));
    
    // then render the master template
    $smarty->display('index.tpl');
    ?>
    I personally think this is a much better solution than using "includes".
    Does anyone have any opinions on this?

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Orlando, FL
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One last note here. Instead of creating 2 smarty objects, you could create 1, and re-use it for each of the subtemplates. Each time you assign variables to a subtemplate, you could store the return value of calling $smarty->fetch() into a variable, and then call $smarty->clear_all_assign() before re-using for the next subtemplate (or for the main template). This way you don't have the overhead of using multiple smarty objects.

    So, for instance, the above code would be changed to:
    Code:
     <?php
    // the includes class instantiates 1 smarty object
    require_once("includes/mysmarty.php");
    
    // ... get some data, apply it to the body template variables here
     $smarty->assign('some_body_var','Some Content');
    
    // now render the body template to a local variable
    $body = $smarty->fetch('unique_body.tpl')
    
    $smarty->clear_all_assign();
    
    $smarty->assign('body', $body);
    
    // then render the master template
    $smarty->display('index.tpl');
    ?>
    d


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •