Search code examples
docxopentbstinybutstrong

Conditional show text block in .docx using OpenTBS based on variable value


In my project I use TinyButStrong / OpenTBS to generate Word documents from templates. First I load a variable like myuser in the document.

[onload;tplvars;myuser=user]

The document is provided with the required variables, so I can add tags like this where the name is placed on the variable:

Hello [myuser.firstName]!

Also conditional values work fine:

[onshow;block=tbs:p;when [myuser.firstName]='John'] This is a paragraph just for John.

But when I have multiple paragraphs that I want to show/hide, it's not easy to select the correct paragraphs (or change the template and add a paragraph). Is it possible to create blocks that have a 'begin' and 'end' that are shown based on a value? Something like this (pseudocode):

Paragraph 1
[if [myuser.firstName] = 'John']
    (insert random number of paragraphs and maybe tables/other stuff here)
[end if]
Paragraph n

I could not find any of this in the TBS/OpenTBS docs. Does someone know a method to do this?

Thanks!

Niels


Solution

  • TBS has a block syntax [onshow;block=being]...[onshow;block=end] but it's a bad practice with OpenTBS because in fact in the inner XML those two tags are placed in a two separated <w:p> entities. So those entities will be split in two and it's a luck if they match with the newly merged contents.

    One solution is to protect the begin/end tags with a paragraph entity.

    [onshow;block=begin;enlarge=tbs:p;when ....] This paragraph will be deleted
    Some text here.
    Some text there.
    [onshow;block=end;enlarge=tbs:p] This paragraph will be deleted
    

    Another solution is to define a block on several paragraphs: This block is defined on 3 paragraphs:

    [onshow;block=tbs:p+tbs:p+tbs:p;when ...] Paragraph 1 - inside the block
    Paragraph 2 - inside  the block
    Paragraph 3 - inside  the block
    Paragraph 4 - outside the block
    

    The other solution is