Categories > OpenTBS with DOCX >

Conditional Section and Template

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Sarah
Date: 2012-12-31
Time: 23:38

Conditional Section and Template

I am running into trouble trying to use conditional sections. I have moved my standard code block which contains references to my main block as well and some subblocks out of my subtemplate file because I was unable to use the main block references in the subtemplate when it was being chosen by the main block (e.g. LineItem.template contains a path to my template file which has references to LineItem.image and others) This caused the LineItem references in the subtemplate to not merge and instead have their brackets replaced with [

In order to avoid this, I have added this to my main template (document.xml). However, I do not always need the same information included in that area of the file, so I need to only include that code if LineItem.template == 'default'. If it has any other value, I need to instead include [LineItem.template].xml. These subtemplates do not have references to LineItem, only subblocks, so they should merge fine.

I am confused about when to use Conditional Sections and when to use If/then/else. The manual shows using if/then/else for inserting subtemplates like so:

[onload;file=[var.insert;if [val]=1;then 'header.html';else '']]

So I tried:

<w:r>[LineItem;block=w:r;file=[if [LineItem.template]!='default';then 'templates/[LineItem.template].xml';else '']]</w:r>
and
<w:r>[LineItem.template;block=w:r;file=[if [val]!='default';then 'templates/[val].xml';else '']]</w:r>

But neither of them ever include a template. If I don't use the conditional, it will include the templates.

For the other part (the large block of code removed from my subtemplate), I tried using Conditional Sections because those, as I understand it, are used with blocks. Here is my code:

<w:r>[LineItem;block=w:r;when[LineItem.template]='default']
      ...code extracted from the subtemplate....
</w:r>
<w:r>[LineItem;block=w:r;file=[if [LineItem.template]!='default';then 'templates/[LineItem.template].xml';else '']]</w:r>

The first <w:r>, which I only want to include when LineItem.template == 'default' is always included and also has an extra line at the top '[LineItem;block=w:r;whendefault='default']'

I don't know how to make the conditional section not display (everything between the <w:r> tags should be deleted), and I don't understand how to make the extra tag merge - I think maybe I should be using ondata or maybe onmerge instead of LineItem, but I really don't understand what is happening here.

Any help is appreciated, thank you for reading.
By: Skrol29
Date: 2013-01-02
Time: 17:35

Re: Conditional Section and Template

Hi Sarah,

Happy new year !

> This caused the LineItem references in the subtemplate to not merge and instead have their brackets replaced with &#91;

See http://www.tinybutstrong.com/support.php#faq_protect

> I am confused about when to use Conditional Sections and when to use If/then/else.

"If/then/else" are parameters processed in TBS fields, not TBS blocks.
Thus when you code [LineItem;block=....] that is just a tag for a block definition, without any field linked to it. "if/then/else" is ignored.
You can code something like [LineItem.my_column;block=....]. This is a tag that combines a field and a block definition.

When you are coding
[LineItem.template;block=w:r;file=[if [val]!='default';then 'templates/[val].xml';else '']]</w:r>
this is a mistake because the TBS "if" is not an operator in an expression, the TBS "if" is a parameter.
You have to code:
[LineItem.template;block=w:r;file=[val];if '[val]'=='default';then '';else 'templates/[val].xml']


By: Sarah
Date: 2013-01-03
Time: 01:11

Re: Conditional Section and Template

Happy new year to you!

Thank you (again) for your reply! As always you instantly clarify the issue. My condition include of the template files is working perfectly now that I structured the if/then/else correctly. I was confused by the example in the manual:

[onload;file=[var.insert;if [val]=1;then 'header.html';else '']]

I'm sure there is a reason this works and mine didn't, but I did try to follow the structure of it, which is how I ended up with the file=[if [val]='something';then 'something';else 'other thing'] layout. I assume the problem has to do with the var.insert part I edited out.

Protect=no had no effect, and I think it is because the conversion of the opening bracket to &#91; is not coming from outside variables. It is occurring in the rId or r:embed attribute of the image elements in the template files. I fixed it by adding a block definition to my changepic so it looks like this:

[LineItem_sub5;block=v:shape;p1]
[LineItem_sub5.fullname;ope=changepic;default=current;noerr;]

The problem I am having now is that my previously working [LineItem_sub1.fieldName] references in the subtemplates are no longer working. I have tried using $, putting known wrong field names (with noerr removed), and previously working field names and it just merges the tag away and gives me no errors. My subblock declaration looks like:
[LineItem;block=w:body;ondata=f_enhance_data;sub1=dimensions;sub2=hardware;sub3=desc;sub4=head_dimensions;sub5=images]

My conditional subtemplate include looks like:
[LineItem.template;block=w:r;file=[val];if '[val]'=='default';then '';else 'templates/[val].xml';noerr;]

And my problem tag looks like:
<w:t>[LineItem_sub1.inner]??<w:t>

(I added the ?? so I could tell where my tag should be)

LineItem_sub1 looks like:
inner => "3/4"
outer => "1-3/8"
thick => "3/8"

Adding 'p1' to the tag makes no difference, and adding a block declaration just causes it to remove the block as if the value is empty. But if the value is empty, I should get an error when 'noerr' is not present?

Do you have any ideas for this one? I don't know what other information about my setup is relevant... it is basically the same as it was when this tag worked.

Thank you very much for following up.
By: Skrol29
Date: 2013-01-07
Time: 15:27

Re: Conditional Section and Template

Hi Sarah,

You say you have a main template like this :
[LineItem.template;block=w:r;file=[val];if '[val]'=='default';then '';else 'templates/[val].xml';noerr;]
And the corresponding sub-template like this:
<w:t>[LineItem_sub1.inner]??<w:t>
But I didn't understood what is the problem. Can you remind me what is the corresponding data structure and what error you've got?
By: Sarah
Date: 2013-01-07
Time: 19:59

Re: Conditional Section and Template

My only error is that the tag is merging blank, so ([LineItem_sub1.inner;]) is showing in debug mode like (). TBS gives no errors, even if the key I use is fake or I add extra levels that don't exist like LineItem_sub1.thing.otherthing. This may be because there is no block= in my tag, but this was working before without it. If I add it, the block gets merged away and I will get errors for wrong tags (but still no errors and no data for right tags).

My main block declaration looks like: [LineItem;block=w:body;ondata=f_enhance_data;sub1=dimensions;sub2=hardware;sub3=desc;sub4=head_dimensions;sub5=images] (I am trying to print sub1 here)

f_enhance_data looks like:
function f_enhance_data($blockName, &$CurrRec){ //nested arrays needed to be flattened out to work with OpenTBS
    if($CurrRec->image->dimensions){
        $CurrRec->dimensions =& $CurrRec->image->dimensions;
    }
        $CurrRec->images[0] = $CurrRec->image; //move main image down a level so a subblock can be used to swap it in the accessory subtemplates
}

My data structure looks like:
LineItems =
array(6) {
  ["1-1"]=>
  object(LineItem)#7 (13) {
    ... //these use the 'default' template which is actually in the main template rather than a subtemplate and they work fine
  }
  ...
  ["4-1"]=>
  object(SeparateHardwareItem)#68 (10) {
    ["image"]=>
    object(Reference)#71 (9) {
      ["dimensions"]=>
      array(1) {
        ["stored"]=>
        array(6) {
          ["inner"]=>
          string(5) "1-5/8"
          ...
        }
      }
    }
    ["template"]=>
    string(15) "alternate_template_01"
  }
}

I've tried [LineItem_sub1.inner;block=w:r;] (with and without p1, inside the main template, I get errors when inner doesn't exists like I should but the w:r block always disappears), [LineItem_sub1.inner] (empty merge), [LineItem_sub1.clearlymadeup.extralevels] (empty merge), [LineItem_sub1.inner;p1;block=w:t] (inside the subtemplate, merges w:t and surrounding w:r away with no errors).

I feel this is probably a dumb mistake I am making, but I am at a loss as to what it could be.

When [LineItem_sub1.inner] worked, I didn't have the conditional block and everything was in a subtemplate that include another subtemplate (sub sub template?), where I used [LineItem.template;file=templates/[val].xml] for the sub sub template and  [LineItem_sub1.inner] for the fields inside it. The change I made to use the conditional block or a sub template has forced me to make changes like adding p1 to some fields since I am now using the same tags in multiple places, and to use extra merges for my images (like [onshow.myObject.items.[LineItem.$].images.0.fullname]). I don't know if this problem is related to that change.

Thank you for trying to help me wrap my head around this issue.
By: Skrol29
Date: 2013-01-08
Time: 18:35

Re: Conditional Section and Template

According to your data structure, 'inner' is not a key just under 'dimensions'.
You should try with
[LineItem_sub1.stored.inner]

By the way, it seems that block [LineItem_sub1] has no block definition, i.e. parameter "block=".

If you have still an unexpected result, you should try to add [LineItem_sub1.$] so it should display the key of the record currently merged. It's just a check to see if at least records are merged.
By: Sarah
Date: 2013-01-08
Time: 19:19

Re: Conditional Section and Template

I replaced the entire contents of my subtemplate with this:

<w:r>
    <w:t>[LineItem_sub1.$;block=w:t;]"</w:t>
</w:r>
<w:r>stufff</w:r>

And my output was:

<w:r>
      stufff
</w:r>

I also tried [LineItem_sub1.stored.inner;block=w:t;] with the same result. I get no errors in either case. It may be worth pointing out that the <w:r> surrounding the <w:t> block also disappears... I don't know if this is OpenTBS getting rid of empty elements or a side effect of this merge not working right.

Thank you for your reply, I hope you have another idea cause this one has me stumped.
By: Skrol29
Date: 2013-01-09
Time: 04:18

Re: Conditional Section and Template

Hi,

>  It may be worth pointing out that the <w:r> surrounding the <w:t> block also disappears.

OpenTBS has some automatic cleanup in order to avoid split tags because of spelling and grammar.
It is possible that the snippet:
<w:r>
<w:t>[LineItem_sub1.$;block=w:t;]"</w:t>
</w:r>
<w:r>stufff</w:r>
has been simplified into:
<w:r>
<w:t>[LineItem_sub1.$;block=w:t;]"</w:t>
stufff</w:r>
and if the sub-block has no data then the block is gone, and we have:
<w:r>
stufff</w:r>

So the question is: why is the sub-block empty for TBS ? Can you investigate your data?
By: Skrol29
Date: 2013-01-09
Time: 04:19

Re: Conditional Section and Template

by the way: there is a new bets version available on the web site. There is a command for changing a picture directly at the PHP side.
By: Sarah
Date: 2013-01-09
Time: 19:08

Re: Conditional Section and Template

I think I know what is causing this now, I figure I will leave the entire post in case it is helpful to anyone, but readers can skip to the *** at the bottom of the post if the process isn't important.

The only more thorough way I can think of to investigate my data is to put var_dumps in f_enhance_data(), which I think is the closest I get to the data OpenTBS is using. I changed the function to read:

function f_enhance_data($blockName, &$CurrRec){ //nested arrays needed to be flattened out to work with OpenTBS
    if($CurrRec->image->dimensions){
        $CurrRec->dimensions =& $CurrRec->image->dimensions;
    }
    if($CurrRec->image->top_view->dimensions){
        $CurrRec->head_dimensions =& $CurrRec->image->top_view->dimensions;
    }
        $CurrRec->images[0] = $CurrRec->image; //move main image down a level so a subblock can be used to swap it in the separate hardware subtemplates
    echo '<br><br>'.$blockName.'-'.$CurrRec->template.'<br>';
    var_dump($CurrRec->dimensions);
}

And my output (for the page using the template I'm working with) is:

LineItem-testing_template
array(1) { ["stored"]=> array(6) { ["diameter"]=> string(6) "1-1/2"" ["inner"]=> string(5) "1-5/8" ["outer"]=> string(1) "3" ["thickness_min"]=> string(5) "0.136" ["thickness_max"]=> string(5) "0.177" ["weight"]=> string(5) "0.200" } }

LineItem-testing_template
array(1) { ["stored"]=> array(6) { ["diameter"]=> string(2) "2"" ["inner"]=> string(5) "2-1/8" ["outer"]=> string(5) "3-3/4" ["thickness_min"]=> string(5) "0.178" ["thickness_max"]=> string(5) "0.280" ["weight"]=> string(5) "0.380" } }

I tried putting my [LineItem_sub1.$;block=w:p;] in the main template where LineItem_sub3 usually prints. It prints keys ('length', 'length_h', but not 'diameter') for data structured like:

["2-1"]=>
  object(LineItem)#37 (13) {
    ["info"]=>
    array(8) {
     ...
    }
    ["image"]=>
    object(Bent)#38 (19) {
      ["dimensions"]=>
      array(4) {
        ["diameter"]=>
        object(Dimension)#60 (6) {
          ...
        }
        ["length"]=>
        object(Dimension)#48 (5) {
          ...
        }
        ["length_h"]=>
        object(Dimension)#48 (5) {
          ...
        }
      }
    }
    ["template"]=>
    string(7) "default"
  }

And merges empty for data structured like:

["4-1"]=>
  object(AccessoryItem)#68 (10) {
    ["image"]=>
    object(Reference)#71 (9) {
      ["dimensions"]=>
      array(1) {
        ["stored"]=>
        array(6) {
          ["diameter"]=>
          string(6) "1-1/2""
          ["inner"]=>
          string(5) "1-5/8"
          ["outer"]=>
          string(1) "3"
          ["thickness_min"]=>
          string(5) "0.136"
          ["thickness_max"]=>
          string(5) "0.177"
          ["weight"]=>
          string(5) "0.200"
        }
      }
    }
    ["template"]=>
    string(15) "testing_template"
  }

I begin to think it is somehow skipping over the first element (stored) since it doesn't print 'diameter'? I don't think my f_enhance_data() is doing this since my var_dumps show that element, but perhaps there is some problem... (this is when I tested something that cleared up the issue)

***

Ok, I think I know what is happening. I moved my [LineItem_sub1;block=w:r;] up above the conditional block in my main template and all of my keys print. The conditional block contains references to LineItem_sub1, and then onshow it gets removed from the template, but the damage is already done. My conditional statement looks like this: [onshow;block=w:r;when '[LineItem.template]'='default';noerr]. Is there a way for me to check the LineItem.template in time to stop the merge of tags inside the conditional block when it is not going to be included in the final page? Onshow is clearly too late, unless there is a way to reset the LineItem_sub1 pointer after the conditional block and before the subtemplate inclusion?
By: Sarah
Date: 2013-01-09
Time: 19:16

Re: Conditional Section and Template

Oh, also, I did switch my OpenTBS from 1.7.5 to 1.8 Beta and it doesn't appear to have caused any problems, but I don't see anything in the changelog or manual about what you mean by "changing a picture directly at the PHP side"? Is this something that would help me go back to [LineItem.images.0.fullname;default=current;ope=changepic] from my new [onshow.quote.items.[LineItem.$].images.0.fullname;ope=changepic;default=current;noerr] (which I use to avoid messing up other merged tags inside the body of the image)? I know you said the [LineItem.$] in the longer tag would be a performance hit.
By: Skrol29
Date: 2013-01-09
Time: 23:46

Re: Conditional Section and Template

>  I don't see anything in the changelog or manual about what you mean by "changing a picture directly at the PHP side"?

See command OPENTBS_CHANGE_PICTURE
By: Sarah
Date: 2013-01-09
Time: 23:50

Re: Conditional Section and Template

Ah, I was looking at the online documentation. Didn't think of the zipped-in file, thanks!
By: Sarah
Date: 2013-01-14
Time: 18:15

Re: Conditional Section and Template

I ended up using the magical p1 parameter in all of my LineItem_sub1 references and now they are showing up. Thank you for helping me work through this!
By: Skrol29
Date: 2013-01-15
Time: 23:26

Re: Conditional Section and Template

:-)