Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

problem using {include} tag in recursive templates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
ozone
Smarty n00b


Joined: 25 Aug 2008
Posts: 4

PostPosted: Mon Aug 25, 2008 6:59 pm    Post subject: problem using {include} tag in recursive templates Reply with quote

Hi everyone

I am new to smarty and have been trying to use the {include} tag in recursive templates without any success.

What I am trying to do:
I have a main template called index.tpl and want to display blocks on the sides (each blocks will be using the same shell) without the use of a smarty pluggin.
So first, I made a simple blocks class (which is called from index.php) to query some informations from the database , then include the required php files, then assign the values array to smarty.

part of the class code goes like this :
Code:

  //Query database for blocks matching user credentials
  if ($is_user) {
      $sql = ('SELECT name, position, title
                  FROM blocks
                  WHERE status = 1
                  ORDER BY position ASC');
  } else {
      $sql = ('SELECT name, position, title
                  FROM blocks
                  WHERE permission = 1
                  AND status = 1
                  ORDER BY position ASC');
  }
  $results = $db->sql_query($sql);
  $idx = 0;
  //Include blocks to be displayed
  while($row = $db->sql_fetchrow($results, SQL_ASSOC)) {
      include(PATH_BLOCKS.$row['name'].".php");
      //Build blocks array
      $this->blocks_list[$idx]['title'] = $row['title'];           
      $this->blocks_list[$idx]['file'] = $row['name'].$this->file_extention;
      $this->blocks_list[$idx]['template'] = PATH_TEMPLATES.str_replace('.', '_', $row['name']).$this->template_extention;
      $this->blocks_list[$idx]['position'] = $row['position'];
      $idx++;
  }
  $db->sql_freeresult($results);
  $smarty->assign('blocks', $this->blocks_list);


Then in my index.tpl, in the sidebars divs I perform a loop to include the block shell like this:
Code:

  {section name=id loop=$blocks}
    {include file="./block.tpl"}
  {/section}


And here goes the block shell template (block.tpl):
Code:

  <div class = "b-header"></div>
  <div class = "b-title">
      {$blocks[id].title}
      <hr />
  </div>
  <div class = "b-footer"></div>


At this point everything works fine .. the block.tpl template gets called for each blocks present in the list of blocks I passed to smarty earlier. So I can see all blocks with their respective titles.

Now what i want to do is include another template (that will control the blocks content layout) from the block.tpl template .. note that the included templates should be different for each blocks since each blocks content is different.
So I tryed to modify the block.tpl like this:
Code:

  <div class = "b-header"></div>
  <div class = "b-title">
      {$blocks[id].title}
      <hr />
  </div>
  <div class = "b-content">
      {include file=$blocks[id].template}
  </div>
  <div class = "b-footer"></div>


This is where my problem is .. when I add the {include file=$blocks[id].template}, I can only see one block with its associated content.
I checked the $blocks[id].template variables from the array and the files names and paths are correct; also, my template path and all asociated paths for smarty are correct. (note that I do not get any error message at all and that my smarty setup do not use the cache).

I also checked in the debug window and gets this when the {include file=$blocks[id].template} is present in the block.tpl template :

Code:

index.tpl (0.03363) (total)
    ./block.tpl (0.01225)
       C:\wamp\www\MyProject\templates\block_links.tpl (0.00561)


and this is what I get when the {include file=$blocks[id].template} is removed from the block.tpl template (I have 3 blocks in total):
Code:

index.tpl (0.00997) (total) 
    ./block.tpl (0.00200)
    ./block.tpl (0.00073)
    ./block.tpl (0.00073)


Any help would be greatly appreciated as I am struggling with this and can not seem to figure it out Confused
Thanks in advance for your help everyone Very Happy
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Aug 25, 2008 7:18 pm    Post subject: Reply with quote

May be there is an error in block_links.tpl.

Try to enable full error reporting. perhaps this provides more details about the root of the error.

ini_set('display_errors', 1);
error_reporting(E_ALL);

What is the content of block_links.tpl?
Back to top
View user's profile Send private message
ozone
Smarty n00b


Joined: 25 Aug 2008
Posts: 4

PostPosted: Tue Aug 26, 2008 10:45 pm    Post subject: Reply with quote

I think I found my problem ...

I was using the same name "id" for both loops ... my bad Embarassed .. lol
everything works fine now Very Happy

Code:

index.tpl (0.02033) (total)
(using a {section} loop to include block.tpl for each blocks)
    ./block.tpl (0.00450)
        C:\wamp\www\MyProject\templates\block_links.tpl (0.00209)
        (using a {section} loop to display a list of links)
    ./block.tpl (0.00325)
        C:\wamp\www\MyProject\templates\block_user.tpl (0.00239)
    ./block.tpl (0.00349)
        C:\wamp\www\MyProject\templates\block_rates.tpl (0.00261)


Thanks a lot for your answer U.Tews, this is greatly appreciated Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP