TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

Run dynamic operation after cache ?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: Milop
Date: 2012-12-05
Time: 12:28

Run dynamic operation after cache ?

Hello,

Here is my code:

<?php

include_once('include/tbs_class.php');
if (isset($this)) {
    $TBS =& $this;
  $TBS->NoErr = true;
} else {
    $TBS = new clsTinyButStrong;
  $TBS->NoErr = true;
}

$id = isset($_GET['id'])?(int)$_GET['id']:1;

$cacheid  = 'single-'.$id.".cache";
include_once('include/tbs_plugin_cache.php');
if(!$TBS->PlugIn(TBS_CACHE,$cacheid, 604800,'./cache') ) { // 604800 = 7 days
$TBS->LoadTemplate('theme/single.php',false);
$index_post= $TBS->MergeBlock(some merge here);
$TBS->Show(TBS_OUTPUT);
}

?>


Evrything is working fine but I want to add some extra PHP code in that file (not in template) that should run always after all code. Something like this:

<?php

include_once('include/tbs_class.php');
if (isset($this)) {
    $TBS =& $this;
  $TBS->NoErr = true;
} else {
    $TBS = new clsTinyButStrong;
  $TBS->NoErr = true;
}

$id = isset($_GET['id'])?(int)$_GET['id']:1;

$cacheid  = 'single-'.$id.".cache";
include_once('include/tbs_plugin_cache.php');
if(!$TBS->PlugIn(TBS_CACHE,$cacheid, 604800,'./cache') ) { // 604800 = 7 days
$TBS->LoadTemplate('theme/single.php',false);
$index_post= $TBS->MergeBlock(some merge here);
$TBS->Show(TBS_OUTPUT);
}

echo time();

?>


Imagine that is not just echo time()
but some more complex code that run curl and call remote server.

Issue is that once cache is create it doesn't continue further then TBS->PlugIn(TBS_CACHE.. part and never execute my code on the end.

Is there any way I can do this ?





By: Milop
Date: 2012-12-07
Time: 11:58

Re: Run dynamic operation after cache ?

Is this possible at all ?
By: Skrol29
Date: 2012-12-07
Time: 15:15

Re: Run dynamic operation after cache ?

The thing is that in sub-template mode, the sub-script errors are not displayed.
This is because TBS activates ob_start() and thus output is diverted to a buffer.
So the script may have encounter an error and display nothing.

There is no debug feature for this case, but I can give a trick if you're lost.
By: Milop
Date: 2012-12-07
Time: 15:57

Re: Run dynamic operation after cache ?

Yes, please help me.

No I don't want to debug script, time() function was just example that I want to add some function on the end of script that should run even if page is cashed.
By: Skrol29
Date: 2012-12-10
Time: 00:13

Re: Run dynamic operation after cache ?

Hi,

Since there is no debug mode for TBS Sub-templates yet, you can temporary avoid the output diversion by canceling the following line of code :
Go into method meth_Misc_ChangeMode(), and turn into comment the 3 lines with ob_start(), ob_get_contents() and ob_end_clean().
Doing this, the output of the sub-template will be display directly instead of inside the main template.

Then run again you code, taking care before that to add some code like you "echo time();" that will prove that the code is executed.
With your full CURL code, error messages should be displayed.
By: Milop
Date: 2012-12-10
Time: 09:59

Re: Run dynamic operation after cache ?

Hm. It doesn't work.
When I applied what you told me what happened is that subtemplate that I have doesn't display on 2nd page load, when it should be loaded from cache.

But my code is not placed in subtabmplate but in main index.php file
By: Milop
Date: 2012-12-10
Time: 10:22

Re: Run dynamic operation after cache ?

So, after line

if(!$TBS->PlugIn(TBS_CACHE,$cacheid, 604800,'./cache') )

script is not running anymore as it looks like Cache plugin exit() running after it display content from cache.

Am I right ?
So, we just need to find some way to continue PHP execution after that.


By: Skrol29
Date: 2012-12-10
Time: 23:07

Re: Run dynamic operation after cache ?

Hi,

> But my code is not placed in subtabmplate but in main index.php file

Then the part with "if (isset($this))" is useless. This piece of code comes from sub-template examples.

In sub-template mode the Render option can only be TBS_OUTPUT.
In normal mode it is TBS_OUTPUT+TBS_EXIT.

> script is not running anymore as it looks like Cache plugin exit() running after it display content from cache.

The command $TBS->PlugIn(TBS_CACHE,...) performs a Show() with the default Render option when the cache file is available.
So if you want the code to continue then you have to change the Render option.

http://www.tinybutstrong.com/manual.php#php_setoption_render
By: Milop
Date: 2012-12-11
Time: 09:46

Re: Run dynamic operation after cache ?

Thank you!