SEO-Board: Free, Fast and Search Engine Optimization Friendly Forum Script
»User: »Password:   Remember Me? 
SEO-Board Forum Software Community / General / Programming / Am a bit confused as to how templating works
Posted:  06 May 2005 14:24
Code:

function get_template($template_name)
{
  $tn = './skin/'.$template_name.'.htm';
  if (file_exists($tn))
  {
    $t = file_get_contents($tn);
    return "return <<<TMP\r\n<!-- START: $template_name -->\r\n{$t}\r\n<!-- END: $template_name -->\r\nTMP;\r\n";
  }
  else
    die("FATAL ERROR: File not found $template_name");
}


I understand this function and I understand this:

Code:

print eval(get_template('mainfooter'));



What I dont understand is when it comes to forumlisting or any list in a table for that matter.

For example in a file called listforums.php there would be the following:

Code:


print eval(get_template('forumbegin'));

while($r=mysql_fetch_array($result)){
     print eval(get_template('forumcell'));
}

print eval(get_template('forumend'));


In forumbegin it would begin the table. In forumcell it would have a <tr> section of the table for just one row and in forumend this would end tha table...


Is all that really necessary just to list the forums... Is there a way where one template could be used which would list the forums in the exact same way?
Posted:  06 May 2005 14:29
No. It can't because every forum cell has different information in it. So, for every cell you need to separately set all vars and then parse the forumcell template.

I don't use 2 templates for a start/end of forum(as in your forumbegin, forumend) example. I use only one template which has a var in it that represents all cells. I build that var by adding all forumcells together and finally parsing the bigger template. Makes templates easier to understand.

Also, another note about the code you've put. Your while loop will load the forumcell every time. It's better to fetch it once (get_template) and then for every added set of data to just parse it with eval.
Posted:  06 May 2005 14:41
Ok thanks for clearing that up...

So Im guessing that you would do something like this:

Have one template say called forummain which would ahve something like:

Code:

<table>
<tr>
<td>
{$forumcell}
</td>
</tr>
</table>


and then in the php file something like:

Code:

$forumcell = "";
while($r=mysql_fetch_array($result)){
.$forumcell = eval(get_template('forumcell'));
}
print eval(get_template('forummain'));



Or is that still wrong. By the way sorry for so many questions. Its just I really want to learn more about this.
Posted:  06 May 2005 17:54
I wouldn't do it like that. The $forumcell variable should open and close the table/div tags.

It would generally be sth like:
Code:

<table>
{$forumcells}
</table>


forumcells would contain all cells and the forumcell template will open and close tr and td tags.
Posted:  06 May 2005 19:48
Ahh I see. Yeah your right that is better

So how would I get the forums to list properly in {$forumcells}

Would it be this?

Code:


while($r=mysql_fetch_array($result)){
     .$forumcells = '<tr>'.$r[name].' - '.$r[description].'</tr>;
}
print eval(get_template('forumcells');


and then have forumcells template like this:
Code:

<table>
<td>
{$forumcells}
</td>
</table>


If not would you mind giving me an example on how its done.. Thanks
Posted:  06 May 2005 19:51
sorry.. i put <tr> and <td> tags in the wrong place there... swap them around
Posted:  06 May 2005 20:13
You add the forumcell straight from the sql results. I prefer to pull it from a forumcell template, to separate function/form. Just look at the source code to see how it works.
Posted:  06 May 2005 23:01
Thats what i done to begin with and it just lost me.

In board.php I found:

Code:

$forums_html = array();

$c_index = $f_lookup_by_parent[0];
$cell_iterator = 0;

$template_cat = get_template('forumcat');
$template_forum = get_template('forumcell');

while (isset($f_rows[$c_index]) && ($f_rows[$c_index][1] == 0))
{
  $c_id = $f_rows[$c_index][0];
  if (!forum_visible($user_id, $c_id))
  {
    ++$c_index;
    continue;
  }
  $forum_link = get_forum_link($c_id, $f_rows[$c_index][3]);
  $forum_desc = (strlen($f_rows[$c_index][4]) != 0) ? ' - '.$f_rows[$c_index][4] : null;
   
  array_push($forums_html, eval($template_cat));
  if (isset($f_lookup_by_parent[$c_id]))
  {
    $f_index = $f_lookup_by_parent[$c_id];
    while (isset($f_rows[$f_index]) && ($f_rows[$f_index][1] == $c_id))
    {
      list($f_id, , , $f_name, $forum_desc, $num_topics, $num_replies, $lastpost_time, $lastposter) = $f_rows[$f_index];
      if (!forum_visible($user_id, $f_id))
      {
        ++$f_index;
        continue;
      }
      $forum_link = get_forum_link($f_id, $f_name, 'forumlink');
      $subforums = '';
      $moderated_by = ($showmoderators == 0) ? null : get_forum_moderators($f_id);
      if ($num_topics == 0)
        $lastpost = $lang['no_posts_yet'];
      else
        $lastpost = $lastposter.'<br>'.format_datetime($lastpost_time, $user_timezone);
      $num_subforums = 0;
      if (isset($f_lookup_by_parent[$f_id]))
      {
        $subf_index = $f_lookup_by_parent[$f_id];
        $subforums = "<div class=subforums>&raquo;&nbsp;{$lang['sub_forums']}: ";
        while (true)
        {          
          $subf_id = $f_rows[$subf_index][0];
          if ($f_visible = forum_visible($user_id, $subf_id))
          {
            $subforums .= get_forum_link($subf_id, $f_rows[$subf_index][3], 'forumlink');
            ++$num_subforums;
          }          
          if (isset($f_rows[++$subf_index]) && ($f_rows[$subf_index][1] == $f_id))
          {
            if ($num_subforums > 0 && $f_visible)
              $subforums .= ', &nbsp';
          }
          else
            break;
        }       
      }
      if ($num_subforums == 0)
        $subforums = null;
      else
        $subforums .= '</div>';
      array_push($forums_html, eval($template_forum));
      ++$f_index;
      $cell_iterator = 1 - $cell_iterator;
    }
  }
  ++$c_index; //next main category
}
$forums_html = implode('',$forums_html);
print eval(get_template('mainforumtable'));
unset($template_cat);
unset($template_forum);
unset($forums_html);


I think that this is the code which is associated with lisiting the categories and forums but when looking through that it loses and confuses me...

Which is why im looking for a simpler method of doing it... and to learn the logic behind it and how it works...

Im able to do it fine in the way which was said a few posts away but dont understand how you are doing it...
Posted:  06 May 2005 23:50
you've picked the most complicated part of seo-board
Posted:  07 May 2005 03:12
oh...hmm....

So I take it Im not going to be able to get my answer here. Can you recommend someone or somewhere where I can?

Thanks
Posted:  07 May 2005 07:44
ughh, I am getting older and dumber. What is the question you seek an answer?
Posted:  08 May 2005 17:13
Ok.

What I want to do is display the list of forums in the same sort fo manner as it is done on the index.

My first attempt was to use a while statement and then during eachloop parse a template which is a html table containing the forum name, desc etc..
This worked fine!

However you said that this is not the best way to do it.. Then I asked what is the best way to do it.

And this is where Im at now.

You mentioned that you would not output the template every single time but instead you would create the list and store it in the variable and then output it.

So I done that...

You then said:
Quote:
You add the forumcell straight from the sql results


Which I didnt understand...

Also in the same post you said:
Quote:
Just look at the source code to see how it works.


Which I done (I copy and pasted the forum list function above). And I didnt understand it at all...

I dont understand how you have made it list the forums without using a while statement...

So my question is to basically explain the code which creates and outputs the forum list tables.
Posted:  08 May 2005 19:22
Quote:
You add the forumcell straight from the sql results
It means you generate the html for a forum cell directly in php. That works fine, but if you decide to change the HTML design later you'll have to change the php code. It is better to make a separate template a forum cell, load all vars (forum description, title etc.) and parse it in the while loop. Basically, you separate code/design. That makes future changes easier.
So instead of your
Code:

.$forumcells = '<tr>'.$r[name].' - '.$r[description].'</tr>;
you parse a template. Also in php string concatenation is slower than building an array of strings and then imploding it.


Quote:
Just look at the source code to see how it works.
The source code is very complicated. I'll need a couple of pages to explain it. The IS a while statement. In general it works like that:

while there are more forums
set all variables pertaining to the current forum (title, desc, subforums)
parse the html and add it to the forums_html array
end

in the end implode the forums_html array into $forums_html var and parse the mainforumtable which uses $forums_html

Posted:  09 May 2005 10:27
Ahh right.

Thanks for that. That answered my question perfectly.

Thanks for your help!