TinyButStrong - the PHP Template Engine
Categories > [old] TbsOoo & TinyDoc >

How to show/hide part of document based on variable value?

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

By: Rene
Date: 2011-07-01
Time: 02:38

How to show/hide part of document based on variable value?

I have an OO template where I want to show or hide a part in some cases. Sounds like a simple task, but I have managed to hassle with it over 2 hours.

Here's an example I've come up with and can't figure out why doesn't it work:
[onload;block=begin;when [var.category]=1]
This line should be shown, but it isn't....
[onload;block=end]

When I print out the [var.category] variable it show's up as '1' like expected. I also have tried to do the following without sucess:
[onload;block=begin;when [var.category]='1']
[onload;block=begin;when [var.category]==1]
[onload;block=begin;when [var.category]=='1']

But if I do this then it shows the data:
[onload;block=begin;when [var.category]!=1]
This line shouldn't be shown, but it is....
[onload;block=end]

It seems like the category variable can't be found while doing the comparison, but why? What am I missing?

Regards,
Rene
By: Skrol29
Date: 2011-07-01
Time: 09:53

Re: How to show/hide part of document based on variable value?

> When I print out the [var.category] variable it show's up as '1' like expected.

Check it with [onload.category] instead of [var.category].
[var.category] is merged on Show() while [onload.category] is merged on LoadTemplate(), i.e. in the same time than [onload] blocks.
By: Rene
Date: 2011-07-02
Time: 01:43

Re: How to show/hide part of document based on variable value?

The [onload.category] tag is not being processed at all - it shows up in the output as "[onload.category]". I'm not actually using the Show() nor LoadTemplate() functions. It might be due to me using an older version of TBS or that these aren't acutally used in OpenOffice templates.

This is how I'm manipulating the template on PHP side:
    $OOo = new clsTinyButStrongOOo;
    $OOo->SetZipBinary('zip');
    $OOo->SetUnzipBinary('unzip');
    $OOo->SetProcessDir(<some direcotry>);
    $OOo->SetDataCharset('UTF8');
    $OOo->NewDocFromTpl($file->getFSPath());

    $OOo->LoadXmlFromDoc('content.xml');
    $OOo->MergeBlock('data', 'array', $new_data);
    $OOo->SaveXmlToDoc();

Just in case I chaecked that I'm using a fairly old version of TBS:
Version  : 2.05.1 for PHP >= 4.0.6
Date     : 2005-09-02

But I'm not to eager to change it because this TBS "installation" is part of a bigger project where others have used it too. So upgrading it could brake someone elses work...
By: Skrol29
Date: 2011-07-03
Time: 00:12

Re: How to show/hide part of document based on variable value?

Ok,

So try to check variable $category using
[onload;if 1=1; then [var.category];esle 'go']
instead of
[var.category]
By: Rene
Date: 2011-07-04
Time: 03:11

Re: How to show/hide part of document based on variable value?

First I just pasted your line to the template:
[onload;if 1=1; then [var.category];else 'go']
The output now indeed shows me "1" like expected.

But now I'm still stuck with the (maybe dumb) question how to use this knowlege to make a part of my template show/hide? I tried these just in case, but even I didn't expect those to work:
[onload;block=begin;when [onload;if 1=1; then [var.category];else 'go']=1]
This line should be shown, but it isn't....
[onload;block=end]

[onload;block=begin;when [onload;if 1=1; then [var.category];else 'go']]
This line should be shown, but it isn't....
[onload;block=end]

Just to be clear - the part of OO template I eventually want to show/hide also includes TSB blocks.

PS. You had a typo in your line "esle" -> "else".

By: Skrol29
Date: 2011-07-05
Time: 01:16

Re: How to show/hide part of document based on variable value?

Can you send your template to me, or a reduced template the reporduce the problem ?
By: Rene
Date: 2011-07-05
Time: 13:48

Re: How to show/hide part of document based on variable value?

I did a small example and sent it to your forum e-mail.
By: Rene
Date: 2011-07-09
Time: 02:17

Re: How to show/hide part of document based on variable value?

Well, it's now a bit awkward - it just works now without a reasonable explanation.... I can't realy imagine what has changed since... Even the server hasn't been rebooted in the meantime.

When doing the small example for you I did test it numerous times on my server and it did not work. The both example outputs where also generated from the same template. Somehow if I use the same template file now, it gives me the expected results.

Well what can I say for the end... I'm sorry to have wasted your time. I'm really frustrated not to be able to know what actually went wrong the first couple of tries.

To everyone else reading, the next piece of code works if you want to show/hide a part of your template:
[onload;block=begin;when [var.category]=1]
This line should be shown, AND IT IS.
[onload;block=end]