Categories > TinyButStrong general >

How to cache subtemplates??

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Tom
Date: 2005-11-08
Time: 14:02

How to cache subtemplates??

Finally got subtemplates working... now on to cacheing only the subtpl result(changes seldom)... not caching main page content (changes frequently)

BUT - caching in subtpl shows subtpl result twice in result page when page is reloaded.

Can anyone see what's wrong with my approach???

In main.php
$header_file = "header.php";
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate('main.html') ;
$TBS->MergeBlock('blk1',$cnx_id, $sqlMAIN);

$TBS->Show() ;

function m_event_blk1( ..... ){    <!-- this is working fine -->
....... }


In main.html
<table>

<tr><td>[var.header_file;script=[val];subtpl]</td></tr>

<tr><td>[blk1.id;block=tr;ondata=m_event_blk1] blk1.fullname]</td>
<td> [blk1.phone;.]</td></tr>
</table>

then in
header.php
$TBS = new clsTinyButStrong ;
$this->LoadTemplate('header.html');
$this->MergeBlock('blk4',$GLOBALS['cnx_id'],$sqlHDR);
$this->CacheAction('headercache',12) ;
$this->Show();

and in
header.html
[blk4.lettername]

Any advice or hints/guesses would be appreciated.

[[Thanks for TBS]]
By: Tom
Date: 2005-11-08
Time: 14:26

Re: How to cache subtemplates??

Oops - forgot to show you the source code for the final result page output
<tr>
<td colspan=2 align="center">
<!-- ============  TEMPLATE CODE ============== -->
<table align=center bgcolor="#EBEBEB" border="1" cellpadding="2" cellspacing="0" summary="">
<tr>
<td> <a href="contacts_main.php?letterpage=T">T</a> </td><td> <a href="contacts_main.php?letterpage=S">S</a> </td><td> <a href="contacts_main.php?letterpage=D">D</a> </td><td> <a href="contacts_main.php?letterpage=J">J</a> </td>
</tr>
</table>
<!-- ============  TEMPLATE CODE ============== -->
<!-- ============  TEMPLATE CODE ============== -->
<table align=center bgcolor="#EBEBEB" border="1" cellpadding="2" cellspacing="0" summary="">
<tr>
<td> <a href="contacts_main.php?letterpage=T">T</a> </td><td> <a href="contacts_main.php?letterpage=S">S</a> </td><td> <a href="contacts_main.php?letterpage=D">D</a> </td><td> <a href="contacts_main.php?letterpage=J">J</a> </td>
</tr>
</table>
<!-- ============  TEMPLATE CODE ============== -->

</td>
</tr>


PS clarification:
the first time the main.php page is called - the header result is there just "one time"
Upon reloading (before cache time expires) the header result shows "twice" as shown above.

Thx
By: Skrol29
Date: 2005-11-08
Time: 16:18

Re: How to cache subtemplates??

Hello Tom,

There was a TBS bug about that.
  http://www.tinybutstrong.com/forum.php?msg_id=3803
Do you have TBS 2.05.3 ?
By: Tom
Date: 2005-11-08
Time: 17:34

Re: How to cache subtemplates??

Thanks for the reply

I had searched for prior posts - that's how I got subtemplates working at first - the TBS Forum is a great place for finding your and other's good advice and solutions ;-)

I saw that post and upgraded already to TBS 2.05.3 with Apache/1.3.33 (Win32) PHP/4.3.10 and MySQL 4.1.10

Is ther some other thing for me to try - ? ? ?
Do you see any potential probs with my poor attempts with TBS?

[[Thanks for TBS]]

By: Skrol29
Date: 2005-11-08
Time: 18:04

Re: How to cache subtemplates??

Hello Tom,

I've tested you snippets and it works almost correctly for me.
When the cache of subtemplate is loaded then the content is display twice because of you header.php script.

It should work as expected if you code:
header.php:
<?php
if (!$this->CacheAction('headercache',12)) {
  $this->LoadTemplate('header.html');
  $this->MergeBlock('blk4',$GLOBALS['cnx_id'],$sqlHDR);
  $this->Show();
}
?>
By: Tom
Date: 2005-11-08
Time: 19:14

Re: How to cache subtemplates??

Thank you for that snippet - it does work - brilliantly

But please tell me is that
if(!$CacheAction(...)){...}
a workaround for something?

Is that the way is 'should' be coded for all cases in future??
Does this aspect need to be in documentation??

I am always nervous to use something I do not yet understand - if you can take time to explain how things are working among the various files that requires the if() to be used - I would very much appreciate it.

[[Thanks for TBS]]
By: Skrol29
Date: 2005-11-08
Time: 19:26

Re: How to cache subtemplates??

No, it is the normal way of using the Cache System in automatic mode.
Automatic mode is when the cache file is automatically loaded when a valid one is found.
If it is loaded, then you dont" have to performe the MergeBlock() and Show() anymore => the result is already saved in the cache.

What the snippet does is :
  this->CacheAction('headercache',12) // => try to found a valid cache file
  if (!...) {  // => if no success, then doing the merge manually
    $this->LoadTemplate(...) // => doing the merge manually
    ... // => doing the merge manually
    $-this->Show // => doing the merge manually
  }
By: Tom
Date: 2005-11-08
Time: 20:01

Re: How to cache subtemplates??

Thanks for the explanation of that.

The more I try to use TBS to do the apps that I used to code manually before -- the more I run into my newbie status in understanding TBS.  But it seems already that it is worth the learning curve ;-)

BTW - Would there be another approach to code for "non-automatic" cache control (that would eliminate the if() checking?

Again, big  [[Thanks for TBS]] and for your patience with teaching us all.

FWIW -- would it be worthwhile for me to help out -- by code up example files to be added to the "TBS Examples" that you distribute with the package?
By: Tom
Date: 2005-11-08
Time: 20:31

Re: How to cache subtemplates??

Just discover a puzzling (to me ;-) aspect of calling subtemplate script.

Referring to same files above...
In main.php
//$header_file = "header.php"; // replace with ANY value
$header_file = "GaRbaGe";

And everything still works fine even though the block code
[var.header_file;script=header.php;subtpl] (from from main.html)
would seem to need a valid value.

-- why is that ??
-- and should it be that way ??

[[Thanks for TBS]]
By: Skrol29
Date: 2005-11-09
Time: 11:19

Re: How to cache subtemplates??

Hello,

> BTW - Would there be another approach to code
> for "non-automatic" cache control (that would eliminate the if() checking?

You can use other Cache System constants to do what the automatic mode does, and more. But the if() checking is quite inherent to the Cache managment. Somehow, you have to manage thinks like this :
* if cache existe and valid => take cache
* otherwise => create cache

> FWIW -- would it be worthwhile for me to help out -- by code up
> example files to be added to the "TBS Examples" that you*
> distribute with the package?

What do you mean ?
By: Skrol29
Date: 2005-11-09
Time: 11:42

Re: How to cache subtemplates??

> -- why is that ??

When you write:
  [var.header_file;script=header.php]
without parameter "subtpl" then the "header.php" script is executed and the TBS Field is normally replaced by the value $header_file.

But when you write:
  [var.header_file;script=header.php;subtpl]
then the parameter "subtpl" activates the subtemplate mode, and then the output of the "header.php" script replaces the value of the TBS Field.
$header_file is no longuer needed and the Field is the same as:
  [onshow;script=header.php;subtpl]

$header_file could be usefull when you write someting like this:
  [var.header_file;script=[val];subtpl]
Here, variable $header_file should contain the name of the script.