TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

Nested if and single quotes inside block

By: Andries
Date: 2016-11-08
Time: 16:47

Nested if and single quotes inside block

Hi,

I am using OpenTBS for several years now. I have an issue when using nested if-then-else conditions, that contain single quotes as content, inside a block. The parsing is not done as suspected.

For this example, I have a block with 2 records inside a table (template created with LibreOffice).

Here is an example of the source code:
Test [testtab.headvar;block=tbs:row;magnet=tbs:row]
[testtab.checkvar1;if [val]=N;then 'Begin if blo''ck 1';else '']
[testtab.checkvar2;if [val]!=N;then 'Begin if blo''ck 2';else 'Begin else blo''ck 2:
[testtab.checkvar3;if [testtab.checkvar3]=[testtab.checkvar4];then 'Begin nested if blo''ck 3_1.';else 'Begin nested else blo''ck 2_1.']
Some te'xt after nesed block in block 2.
[testtab.checkvar1;if [val]=N;then 'Begin nested if blo''ck 2_2.';else '']']

Below, you can find the output. My concern is in "Test Block2". You can see that at a certain point the single quote of the condition is visible and the parsing goes wrong.

Test Block 1


[testtab.checkvar2;if [val]!=N;then 'Begin if blo''ck 2';else 'Begin else blo''ck 2:
Begin nested if blo'ck 3_1.
Some te'xt after nesed block in block 2.
']
Test Block2
Begin if blo'ck 1

'Begin else blo''ck 2:
Begin nested else blo'ck 2_1.
Some te'xt after nesed block in block 2.
Begin nested if blo'ck 2_2.'

How can I fix this issue? Is there a more proper way of implementing nested if-structures?

Thank you,
Andries
By: Skrol29
Date: 2016-11-10
Time: 03:25

Re: Nested if and single quotes inside block

Hi Andries,

In a then/else expression delimited with single-quotes (') , all single-quotes inside should be escaped (that is doubled with two single-quotes), just like you did with « 'Begin if blo''ck 2' ».
In you snipped, it is not done with « ... then 'Begin nested ... » and after.

But in my opinion such complicate expressions for if/then/else should no be done in the template side.
This is not a Template Engine job to compute complicated if/then/else.
You'd better use parameter "ondata" with a custom PHP function in order to arrange the data, and then have a simple TBS field in the template.