Categories > TinyButStrong general >

caching of subtemplates

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: goldkante
Date: 2008-04-15
Time: 17:00

caching of subtemplates

hello!

i have one main-template where some othe scripts are included in "subtpl"-mode.

example:
<!-- PAGEHEADER -->
[onload;script=[var.tmpl_main_page_header_start];subtpl]
<!-- ENDE PAGEHEADER -->

<!-- MAINCONTAINER -->
<div id="main">
<!-- 3 SPALTEN SCHWERPUNKT & INTERVIEWS -->                
    <div class="s3Spalten content">
        <!-- PANORAMA UNTEN -->
        [onload;script=[var.tmpl_start_panorama_bottom];subtpl]
        <!-- ENDE PANOARMA UNTEN -->
    </div>
    <!-- ENDE 3 SPALTEN SCHWERPUNKT & INTERVIEWS --> 
               
    <!-- 2 SPALTEN NATIONAL & INTERNATIONAL -->                
    <div class="s2Spalten_same content">
        <!-- NATIONAL / INTERNATIONAL -->
        [onload;script=[var.tmpl_start_national_international];subtpl]
        <!-- ENDE NATIONAL / INTERNATIONAL -->
    </div>
    <!-- 2 SPALTEN NATIONAL & INTERNATIONAL -->
   
</div>
<!-- ENDE MAINCONTAINER -->
                             
<!--PAGE FOOTER -->
[onload;script=[var.tmpl_main_page_footer];subtpl]

<!-- ENDE PAGE FOOTER -->

everything's orking pretty good so far.

the included script's are lookin pretty much like this:
<?php
//      We are under the TBS Subtemplate Mode =>
//   variables are always local, not global,
//   and the TBS object is referenced by variable $this.
global $C,$DB_1,$_cache_secs;
$TBS =& $this;


if (!$TBS->PlugIn(TBS_CACHE,"content.start.topbox",$_cache_secs))    :
    echo "Frisch!";
       
    #angezeiget zurücksetzen
    $q_displays = "UPDATE article_settings SET is_displayed_on_startpage='0' WHERE is_displayed_on_startpage='1'";
    $DB_1->execute($q_displays);
   
    // TOP NEWS STARTSEITE
    $top_entries = 3;
    // query basteln
        $C->category('news');
    $C->order("article_settings.is_fix_news DESC, article_settings.datum DESC");
    $C->where("(article_settings.category != 8 AND article_settings.category!= 11 AND article_settings.category!= 17 AND article_settings.category!= 5 AND article_settings.category!= 20 AND article_settings.category!= 22 AND is_top_redaktion='1')");
    $C->start('0');
    $C->entries($top_entries);
    $q_news_top = $C->query();
       
    $res_top =& $DB_1->execute($q_news_top);
   
    while (!$res_top->EOF)    :
        // Zeit für cache-test
        $res_top->fields['now'] = date("H.i.s");
        $data_top[] = $res_top->fields;
        $shown_ids[] = $res_top->fields['id'];
        $res_top->moveNext();
    endwhile;
   
    # angezeigte updaten
    $displayed_articles = implode(",",$shown_ids);
    $q_displays = "UPDATE article_settings SET is_displayed_on_startpage='1' WHERE id IN (".$displayed_articles.")";
    $DB_1->execute($q_displays);

    $TBS->loadTemplate(TMPL."content.start.topbox.html");
    $TBS->mergeBlock('blk_news_top',"array",$data_top);

    $TBS->PlugIn(TBS_CACHE,"content.start.topbox",TBS_CACHEONSHOW);
    $TBS->Render = TBS_OUTPUT;
    $TBS->show();
endif;
?>

everythings's working fine, too!  output is generated and is looking fine.

if the cache-file of the subtemplate is deleted, the fresh content is generated and displayed properly.

my problem starts on the next refresh, when the content is loaded from cache: the cache-file of my

suddenly contains the content of the next included script. in this case the content

of .tmpl_start_national_international.

i don't have a clue whats wrong...

thanks in advance for your help!!

By: TomH
Date: 2008-04-15
Time: 19:36

Re: caching of subtemplates

Hello,

I think it's your IF statement syntax...

Here's how I do it

//if (!$TBS->CacheAction($cacheid, $cachesecs)) { // stops rendering subtemplate twice 

if( !$TBS->PlugIn(TBS_CACHE,$cacheid, $cachesecs) ) {

// do all the sql and merges here

$TBS->Show

} // end the if(cache) test
You can also see the approach for having multiple subtemplates cached separately at http://tomhenry.us/tbs3/  go th the Caching Model (3) link

Hope that helps,
TomH
By: goldkante
Date: 2008-04-15
Time: 19:58

Re: caching of subtemplates

hi tom!

problem stays the same. if _one_ of the subtemplate cachefiles is delete, the the output
of the next included script ist found in it, on the next refresh. if i  delete all cached subtemplate files, everythings fine. every cachefile is filled with the correct content.
maybe this is something about the $TBS->render property.
just guessing... actually i dont have a clue.

so far everthings so fine about tbs... i would be so sorry if the caching wouldnt work this way. 
By: TomH
Date: 2008-04-15
Time: 21:35

Re: caching of subtemplates

I may have been unclear... my caching (as shown above) is done in the main page NOT in the subscript

When I cache in the subscript I do a new instance of TBS and I do NO TBS->Render
$TBS = new clsTinyButStrong ;
// =======================================
$cache_id = basename($_SERVER["SCRIPT_NAME"], ".php");
// Skrol29 See:http://www.tinybutstrong.com/forum.php?msg_id=3847

$cachesecs=12;

if( !$this->PlugIn(TBS_CACHE,$cache_id, $cachesecs) ) {
    $this->LoadTemplate('menuscript.html') ;
    $sql="SELECT DISTINCT left(lname,1) AS fletter from mycontacts ORDER BY lname";
    $db->get_results($sql);
    $this->MergeBlock('blk4',$db);
    $this->Show();
}




By: goldkante
Date: 2008-04-15
Time: 22:03

Re: caching of subtemplates

hmmm... im confused :)
if i start a new instance of TBS inside a subscript, the only thing displayed is the
template loaded inside the subscript... no matter if its cached or fresh content.

i try to eyplain the whole scenario a little more, maybe the whole construction
is too complicated and you can give me a hint for building a better, simpler one.

in my index.php, which is executed first, i have loaded a template called
"main.container.startpage.html". in this template i include several scripts using
[onload;script=[var.tmpl_start_somescript];subtpl] for example.
inside those included scripts i load content fron the db, merge blocks and render
cachefiles, display them or just display them, depending on the "freshness" of
the content. in some cases, i  need to load an other script using  [onload;script=[var.tmpl_start_someotherscript];subtpl]... as you can see, in this situation im in a "sub-sub-script"-mode. maybe thats is the problem.

By: TomH
Date: 2008-04-15
Time: 22:26

Re: caching of subtemplates

Hmmm,

(1) read the full thread at http://www.tinybutstrong.com/forum.php?msg_id=3842 (slowly and carefully)

(2) then make sure your instances ($TBS->... and $this->...) mirror that architecture are used appropriately

If that doesn't help then post your COMPLETE main php and html and one set of subtpl php and html codes - as a debugging case. We'll have to work on the sub-sub- issue on the second pass ;)

By: goldkante
Date: 2008-04-15
Time: 23:37

Re: caching of subtemplates

erm... as i can see in http://www.tinybutstrong.com/forum.php?msg_id=3842,
everythings just like in my scenario... but i doesnt work.

lets give this debugging-attempt a try:

main.php
<?php
// CONTENT SPALTE TEMPLATE WEICHE

// GENERELLE
$tmpl_main_page_footer                    = ROOT.'content/main.page.footer.php';

// STARTSEITE
$tmpl_main_page_header_start            = ROOT.'content/main.page.header.start.php';
$tmpl_start_topbox                         = ROOT.'content/content.start.topbox.php';
$tmpl_start_panorama_top                = ROOT.'content/content.start.panorama.top.php';
$tmpl_start_news                         = ROOT.'content/content.start.newsblock.php';
$tmpl_start_newsticker                     = ROOT.'content/content.start.teaser.newsticker.php';
$tmpl_start_rectangle                     = ROOT.'content/content.start.rectangle.php';
$tmpl_start_teaserbuttons                = ROOT.'content/content.start.teaserbuttons.php';
$tmpl_start_panorama_bottom                = ROOT.'content/content.start.panorama.bottom.php';

$tmpl_start_national_international        = ROOT.'content/content.start.national_international.php';
$tmpl_start_geschichtsstunde            = ROOT.'content/content.start.geschichtsstunde.php';
$tmpl_start_community                    = ROOT.'content/content.start.community.php';
$tmpl_start_panorama_end                = ROOT.'content/content.start.panorama.end.php';
$tmpl_start_onlinelist                    = ROOT.'content/content.start.onlinelist.php';


//LANDINGPAGE
$tmpl_main_page_header_landingpage        = ROOT.'content/main.page.header.landingpage.php';
$tmpl_content_landingpage_articlelist    = ROOT.'content/content.landingpage.articlelist.php';

//ARTIKEL
$tmpl_content_article_standard            = ROOT.'content/content.article.standard.php';



    if ( !isset($_GET['inc']) || empty($_GET['inc']) )    :
        $TBS->LoadTemplate(TMPL.'main.container.start.html');
    else  :
        $TBS->LoadTemplate(TMPL.$_GET['inc'].'.html');
    endif;
   
    $TBS->Show();

?> 

this is the template:
<!-- PAGEHEADER -->
[onload;script=[var.tmpl_main_page_header_start];subtpl]
<!-- ENDE PAGEHEADER -->

<!-- MAINCONTAINER -->
<div id="main">
    <!-- TOPNEWS -->
    [onload;script=[var.tmpl_start_topbox];subtpl]
    <!-- ENDE TOPNEWS -->
               
    <!-- PANORAMA -->                
    [onload;script=[var.tmpl_start_panorama_top];subtpl]
    <!-- ENDE PANORAMA -->
               
    <!-- 2 SPALTEN OBEN -->              
    <div class="s2Spalten_ls content">
        <div class="col1">
        <!-- NEWSBLOCK -->
        [onload;script=[var.tmpl_start_news];subtpl]   
        <!-- ENDE NEWSBLOCK -->                                                   
        </div>
        <div class='col2'>
        <!-- NEWSTICKER -->
        [onload;script=[var.tmpl_start_newsticker];subtpl]
        <!-- ENDE NEWSTICKER -->
        </div>
    </div>
    <!-- ENDE 2 SPALTEN OBEN -->
               
    <!-- 3 SPALTEN SCHWERPUNKT & INTERVIEWS -->                
    <div class="s3Spalten content">
        <!-- PANORAMA UNTEN -->
        [onload;script=[var.tmpl_start_panorama_bottom];subtpl]
        <!-- ENDE PANOARMA UNTEN -->
    </div>
    <!-- ENDE 3 SPALTEN SCHWERPUNKT & INTERVIEWS --> 
               
    <!-- GESCHICHTSSTUNDE -->
    [onload;script=[var.tmpl_start_geschichtsstunde];subtpl]
    <!-- ENDE GESCHICHTSSTUNDE -->
   
    <!-- COMMUNITY -->
    [onload;script=[var.tmpl_start_community];subtpl]
    <!-- ENDE COMMUNITY -->

     <!-- PANORAMA END -->
    [onload;script=[var.tmpl_start_panorama_end];subtpl]
    <!-- ENDE PANORAMA END -->
</div>
<!-- ENDE MAINCONTAINER -->
                             
<!--PAGE FOOTER -->
[onload;script=[var.tmpl_main_page_footer];subtpl]
<!-- ENDE PAGE FOOTER -->


an finaly one of those nasty subscripts.
<?php
//      We are under the TBS Subtemplate Mode =>
//   variables are always local, not global,
//   and the TBS object is referenced by variable $this.
global $DB_1,$_cache_secs,$gastonline_zahl,$useronline_zahl;

$table = "t_users_online";

if (!$this->PlugIn(TBS_CACHE,"content.start.onlinelist",$_cache_secs))    {
    echo "FRISCH!";
   
    # We are now going to get the amount of visitors by
    # counting all entries in the table
    $q_guest_count = "SELECT COUNT(*) FROM ".$table." WHERE u_id='0'";
   
   
    $gastonline_zahl = $DB_1->getOne($q_guest_count);
   
    $q_useronline = "SELECT t_users.u_name,t_users_online.* FROM t_users_online LEFT JOIN t_users ON (t_users_online.u_id = t_users.u_id) WHERE t_users_online.u_id > 0 ORDER BY zeit DESC";
    $res =& $DB_1->execute($q_useronline);
   
    $useronline_zahl =  $res->_numOfRows;
   
   
    $counter = 1;
   
    while (!$res->EOF)    :
        $res->fields['image'] = (is_file(REMOTEROOT."img/user_jpg/".$res->fields['u_id']."_thumb.jpg")) ? "http://www.11freunde.de/img/user_jpg/".$res->fields['u_id']."_thumb.jpg" : "/img/previews/userbild.gif";
        $res->fields['tip_u_name'] = utf8_decode(str_replace(" ", "_", $res->fields['u_name']));
       
        if ($counter <= 12)    :
            $useronline_data[] = $res->fields;
        endif;
       
        $res->moveNext();
        $counter ++;
       
    endwhile;
   
    $this->loadTemplate(TMPL."content.start.onlinelist.html");
    $this->mergeBlock('blk_onlineuser',"array",$useronline_data);   
   
    $this->PlugIn(TBS_CACHE,"content.start.onlinelist",TBS_CACHENOW);
   
   
    $this->show();
   
}
?>

now you can see one example... all other subscripts have exactly the same structure...


and thank you so much for giving it a try! :)

By: goldkante
Date: 2008-04-15
Time: 23:48

Re: caching of subtemplates

one thing: if the cachetime is exceeded and all the cachefiles are refreshed at once /all have a global cache ttl), everythings working fine. if i delete just the cached output generated by the subcript i posted,
the content of this cachefile is replaced by the next _not_ cached output. in this case its
the footer navigation. as a result of this, insted of the "users online" list the footer navi ist displayed on this position. 
By: goldkante
Date: 2008-04-16
Time: 00:22

Re: caching of subtemplates

another thing i dont unserstand:
why is
$this->PlugIn(TBS_CACHE,"content.start.onlinelist",$_cache_secs)
working, while this
!$this->CacheAction(TBS_CACHE,"content.start.onlinelist",$_cache_secs)
is not working?
By: TomH
Date: 2008-04-16
Time: 00:31

Re: caching of subtemplates

Sorry to have miscommunicated...

I meant that you needed to construct a test case based on the model of that thread i.e. to have a new/separate instance of TBS for the subtpl script

I can't do that for you - as only you have the db's with which to plug the merges.

It's my hunch that the global instance being used in the subtpl  (from the main file) is the underlying source of the problem (and will rear it's ugly head again in the sub-subtpl case)
By: TomH
Date: 2008-04-16
Time: 00:32

Re: caching of subtemplates

What version of TBS are you running?
By: TomH
Date: 2008-04-16
Time: 00:33

Re: caching of subtemplates

I missed that "!" in the second method .
Maybe that's it?
By: goldkante
Date: 2008-04-16
Time: 00:41

Re: caching of subtemplates

im getting a bit tired now. forget about the (!)... i forgot to delete it.
why is one working while the other is not? all the examples  and tutorials are
working with CacheAction...

even if it doesnt semm to be my problem, i'd like to know.

btw. im working with 3.3.0...
By: goldkante
Date: 2008-04-16
Time: 01:14

Re: caching of subtemplates

i found the soulution:

just started a new instance in main.php and everythings working fine!!!

thanks for your help and thanks to skrol for this great engine!!
By: goldkante
Date: 2008-04-16
Time: 01:42

Re: caching of subtemplates

oh my... it doesnt work. the new instance just used the my rootdir for caching.
so teh che wasn't deleted properly...
i'm too tired right now. i'll give it another try tomorrow...

:(