Categories > TinyButStrong general >

CACHENOW & Render NOTHING inside a function

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: TomH
Date: 2010-09-20
Time: 18:01

CACHENOW & Render NOTHING inside a function

I have been trying some new things (for me) and I searched this forum for related coding as a way to see if I was on track.

Some code by Skrol29 & Pirjo Posio has inspired me to to take the next step and make a function to do the dirty work.

In this function I want to be able to interrupt the TBS processing for the current page and do the following:
* read in a template having TBS blocks
* populate the blocks with db content
* save (cache) the merged template file for later use
* resume TBS processing for the current page

I want this scheme to cache merged template(s) so that they can be used in all the other pages of the site without the other pages having to do ANY PHP/db processing.

Here's my attempt at a function to do  this -- it actually seems to be working - it does create the merged subtemplate. And it does expire and re-build the merged files as expected. I see no evidence "yet" that it is breaking the TBS for the current page. ((But this stuff really scares me!))

// ===========  function to do merging for cachenow ============
// Assumes TBS 3.6.0 RC &  TbsSQL 3.0.0 bete are running
function cachenow_merged($cacheid, $dir, $mask, $maxage, $subtpl, $sql, $blockname){
global $TBS, $Db;
$TBS->Render = TBS_NOTHING ;

$template = file_get_contents($subtpl); //read html table (with TBS code blocks)
$TBS->Source = $template; // load the template see Manual property Source
$TBS->LoadTemplate(null) ;

$TBS->Render = TBS_OUTPUT; // need this to be able to see in tbssql console
$res = $Db->GetRows($sql);
$TBS->Render = TBS_NOTHING ;

$TBS->MergeBlock($blockname,$res);
$TBS->PlugIn(TBS_INSTALL, TBS_CACHE, $dir, $mask);
$TBS->PlugIn(TBS_CACHE, $cacheid,  TBS_CACHENOW );


$TBS->Render = TBS_NOTHING  = EXIT;
return true;

}//FUNCTION
// =================================================

Here's the function as it's being used in the PHP page script
// some other code here
...
...

$mainpage = $TBS->Source; // insure TBS Source does not get contaminated

// set the vars needed for the cachenow_merged() function
$cacheid="letter_table";
$dir=".";
$mask="merged_*.html";
$maxage=2;
$subtpl = "subtpl_letter_table_func.html";
//$subtpl = "subtpl_letter_table.html"; // switching tpl does prove it is working !!!
$sql = "SELECT DISTINCT left(lname,1) AS fletter from mycontacts ORDER BY lname";
$blockname="blk_fl";

$filename = ABS_PATH."/merged_".$cacheid.".html"; // needed for evaluating whether to run cachenow_merged()

if((file_exists($filename)  && (time() - filectime($filename) > $maxage*60)) || !file_exists($filename)){
  cachenow_merged($cacheid, $dir, $mask, $maxage, $subtpl, $sql,$blockname);
}

$TBS->Source = $main_page; // recover previous TBS Source

// some other code here
...
...
$TBS->Show();

I would greatly appreciate a detailed critique of this approach -and- any warnings of weaknesses or outright errors.

You can see a prototype of this at
http://tomhenry.us/tbs3/cachenow_merged/contacts_merged_func.php
sometimes it's easier to understand code when the page can be seen.

Thanks very much for any guidance you can give.

And thanks for TBS every day,
TomH
By: TomH
Date: 2010-09-20
Time: 18:15

Re: CACHENOW & Render NOTHING inside a function

By: Skrol29
Date: 2010-09-21
Time: 21:49

Re: CACHENOW & Render NOTHING inside a function

Hi TomH,

1) Why don't you put the source swap into the function?
$mainpage = $TBS->Source; // insure TBS Source does not get contaminated
...
$TBS->Source = $main_page; // recover previous TBS Source
This swap is needed only in case of actual caching. And it would be also cleared to read.

2) In the following part, I'm surprised that you need to swap the Render:
$TBS->Render = TBS_OUTPUT; // need this to be able to see in tbssql console
$res = $Db->GetRows($sql);
$TBS->Render = TBS_NOTHING ;

Otherwise, it seems ok
By: TomH
Date: 2010-09-21
Time: 23:40

Re: CACHENOW & Render NOTHING inside a function

SKrol29,
Thanks for your comments.

I did make those changes you recommended and the function works fine (that did not surprise me except for the part of eliminating the Render swap!).

But that means there's something I don't understand about the Source property...

Here's the current function
function cachenow_merged($cacheid, $maxage, $subtpl, $sql, $blockname){
global $TBS;
global $Db;
$main_template = $TBS->Source;

$template = file_get_contents($subtpl); //read html table (with TBS code blocks)
$TBS->Source = $template; // load the template see Manual property Source
$TBS->LoadTemplate(null) ;

$res = $Db->GetRows($sql);
$TBS->MergeBlock($blockname,$res);
$TBS->PlugIn(TBS_CACHE, $cacheid,  TBS_CACHENOW );

$TBS->Source = $main_template; // recover previous TBS Source  //skrol        
return true;
}//FUNCTION

I can understand that $main_template = $TBS->Source;  saves the main Source -- but I don't see how then the Source only contains the source from $TBS->Source = $template; ready for the Merge ???

What is happening that makes Source empty before the $TBS->Source = $template;  statement?

Thank you for your patience, and thanks for TBS every day,
TomH




By: Skrol29
Date: 2010-09-23
Time: 09:29

Re: CACHENOW & Render NOTHING inside a function

Hi,

> What is happening that makes Source empty before the $TBS->Source = $template;  statement?

Source does not become empty. It is just swaped.