Categories > OpenTBS general >

Ampersand in 'if' tests using OpenTBS

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Chris H
Date: 2013-09-19
Time: 13:32

Ampersand in 'if' tests using OpenTBS

I am at a bit of a loss with this.  I can see the problem but cannot work out a way around it, so any help would be gratefully received.

I have a Word document (docx) which I am merging using the OpenTBS plugin.  Everything works fine, except that I have a field to merge with an if statement that looks like this:

   [onshow; if [SolutionType]='Manage & Monitor'; then ' (see table below)';]

This test will never evaulate as true.  I think this is either because the SolutionType string is being converted from 'Manage & Monitor' to 'Manage & Monitor' (so that it will not break the XML), before the test is being evaluated or that the test is being read internally as if [SolutionType]='Manage & Monitor'; then...  If I replace the ampersand in the template with &amp then it still does not work, and if one examines the XML in the document it is now actually stored as &

I suspect i am looking for a custom onformat=function to make this work?
By: Sarah Kemp
Date: 2013-09-19
Time: 16:47

Re: Ampersand in 'if' tests using OpenTBS

Have you seen this topic? http://www.tinybutstrong.com/forum.php?thr=3198

Are you using htmlconv=no?
By: Chris H
Date: 2013-09-19
Time: 16:56

Re: Ampersand in 'if' tests using OpenTBS

I thought htmlconv was deprecated and should now be strconv?  I tried:

[onshow; if [SolutionType;strconv=no]='Manage & Monitor'; then ' (see table below)';]

but this still does not work.  It also does not break the XML, which a raw '&' would do, so I am thinking maybe the OpenTBS plugin is preventing insertion in raw format?

I have got around it at the moment by doing a bit of a hack and doing a string replace on the code side, replacing "&" with "*AMP*" and then matching this instead, i.e.

[onshow; if [SolutionType; onformat=preserve_amp]='Manage *AMP* Monitor'; then ' (see table below)';]

but this is a bit of an ugly workaround.
By: Sarah Kemp
Date: 2013-09-19
Time: 17:03

Re: Ampersand in 'if' tests using OpenTBS

You are right, I can never remember which is deprecated.

Is 'Manage & Monitor' really a string value hard coded into your tag or is it dynamic? If so, where is it coming from?
By: Chris H
Date: 2013-09-19
Time: 17:14

Re: Ampersand in 'if' tests using OpenTBS

No it is hard coded in the document template.  This is part of a simple web service that receives a bunch of post data and then merges it with a word template.  In this template we have different paragraphs which might apply depending on whether the 'SolutionType' input equals 'Monitor', 'Manage' or 'Monitor & Manage', so in the template we just have a sequence of 'if' statements with the 3 options hard coded. 

FYI, if I do a simple merge of [SolutionType] it will show up in the finished merged document correctly as 'Monitor & Manage'.

I originally assumed that it was actually merging 'Monitor & Manage' into the document, and that strconv=no would make it work, but as I say, it doesn't.   
By: Sarah Kemp
Date: 2013-09-19
Time: 17:41

Re: Ampersand in 'if' tests using OpenTBS

Another thought just occurred to me. When you say 'Manage & Monitor' is coded into your template, do you mean it is in the Word document that you are editing with Word or that it is in the deconstructed XML of a Word document? Because Word will certainly convert your '&' into '&' therefore strconv=no would be the opposite of what you want, as your goal is to compare 'Manage & Monitor' to 'Manage & Monitor'. Can you replace the code with:

[xonshow; if [SolutionType]='Manage & Monitor'; then ' (see table below)';]

And look at the debug XML to see the value of both sides of the if?
By: Chris H
Date: 2013-09-19
Time: 18:15

Re: Ampersand in 'if' tests using OpenTBS

That is the same as my first example, I think?

I have tried doing a debug XML and if I find the relevant bit, it is reading

<w:t>[onshow; if Manage &amp; Monitor='Manage &amp; Monitor'; then ' (see table below)';]</w:t>

so I am still confused as to why this isn't working!
By: Chris H
Date: 2013-09-19
Time: 18:21

Re: Ampersand in 'if' tests using OpenTBS

Actually - this might have pointed to a solution!

This appears to work:

[onshow; if '[SolutionType]'='Manage & Monitor'; then ' (see table below)';]

It seems that, whereas normally it wouldn't matter if one side of the comparison was in speechmarks and the other isn't it does if you include an ampersand?

Thank you very much for your help with this.
By: Sarah Kemp
Date: 2013-09-19
Time: 18:36

Re: Ampersand in 'if' tests using OpenTBS

My code was the same as your first example except for the x in front of onshow, meant to prevent the if from executing so we could see both side of the evaluation. It looks like you did not include this x, and yet your tag is still present. This shouldn't be happening. Are you getting your tag output in the body of the document when you allow it to be created?

Have you tried putting quotes around the first part of the expression? e.g. '[SolutionType]'?

Nevermind, I see you've fixed it.
By: Chris H
Date: 2013-09-19
Time: 19:21

Re: Ampersand in 'if' tests using OpenTBS

I misunderstood your previous post so I did an OPENTBS_DEBUG_XML_CURRENT render but got the same information.  Quoting the left side of the comparison seems to work fine. Thank you for your help with this.