TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

Struggling with conditional blocks

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

By: Ivor
Date: 2016-03-09
Time: 21:52

Struggling with conditional blocks

Hi, I am having some trouble with getting conditions blocks to work correctly. I am new to TBS but have created some nice shortcuts for some repetitive c++ coding of messages.
My data source is from sqlite. I use the @ symbol since '[' is used in c++

Can you please assist me in how to do this. I have read the documentation but still struggle. TBS is too strong for me.
In my template I have the following bought code (it does not work)
        @msg_data_struct;block=begin;p1=@grp_messages.msg_name@@
        @msg_data_struct;when msg_data_struct.msg_fieldtype == 'struct'@ // so if the filed type is a struct, I need to expand the struct for each value in order to initialise it to default values.

            @structfields;block=begin;p1=@msg_data_struct.msg_fieldname@; @ @structfields.struct_name@; //@structfields.struct_description@
            @structfields;block=end@

        @msg_data_struct;when @msg_data_struct.msg_fieldtype@ != 'struct'@ this is not a struct // if it not a struct, then just print this string

        @msg_data_struct;block=end@
By: Ivor
Date: 2016-03-10
Time: 17:24

Re: Struggling with conditional blocks

Scrol29, I sent an email to your gmail with much more info.
Regards
Ivor
By: Ivor
Date: 2016-03-11
Time: 21:19

Re: Struggling with conditional blocks

Please... anyone with some help on using conditional blocks using absolute syntax?
By: Skrol29
Date: 2016-03-11
Time: 21:54

Re: Struggling with conditional blocks

Hi Ivor,

TBS does not supports the same characters for beginning and ending tags.
I have tested, it doesn't work, but it is also logical: how can you solve a tag such as
@onshow;block=p;when @onshow.x@=29@
In fact TBS is considering the tag as
@onshow;block=p;when @

Since TBS does not supports this it is a bug that there is no alert.
This will be fixed in next version.
By: Ivor
Date: 2016-03-11
Time: 22:41

Re: Struggling with conditional blocks

Scroll29, thanks a million for replying. I was meaning to double check about the same start and end tag, but somehow I never got round to it.

So having changed to use the standard open and close chars, my conditional blocks look like this now as below, but they print all the text in between, i.e. as if always true
Is my syntax for using absolute blocks syntax wrong? Does the "when" only work with onshow?

       [msg_data_struct1;block=begin;p1=[grp_messages.msg_name];when [msg_data_struct1.msg_fieldtype]  == 'struct']
        Some block text to structs
        [msg_data_struct1;block=end]
        [msg_data_struct1;block=begin;p1=[grp_messages.msg_name];when [msg_data_struct1.msg_fieldtype]  != 'struct']
        Some block text to NON-structs
        [msg_data_struct1;block=end]
        [msg_data_struct1;block=begin;p1=[grp_messages.msg_name];default]
        Some Default TEXT
        [msg_data_struct1;block=end]

wit the result


       
        Some block text to structs
       
        Some block text to structs
       
       
        Some block text to NON-structs
       
By: Skrol29
Date: 2016-03-11
Time: 22:50

Re: Struggling with conditional blocks

Hi Ivor,

Parameter "p1" should be placed on the first section only.

According the the documentation :
« Each section of the block to be merged that contains a parameter p1 will be computed as a separate block for which the dynamic query is re-executed. The sections of the block that have no parameter p1 are combined with the previous section with a parameter p1.  »
http://www.tinybutstrong.com/manual.php#html_block_subblock

So you should try:
        [msg_data_struct1;block=begin;p1=[grp_messages.msg_name];when [msg_data_struct1.msg_fieldtype]  == 'struct']
           Some block text to structs
        [msg_data_struct1;block=end]

        [msg_data_struct1;block=begin;when [msg_data_struct1.msg_fieldtype]  != 'struct']
           Some block text to NON-structs
        [msg_data_struct1;block=end]

        [msg_data_struct1;block=begin;default]
           Some Default TEXT
        [msg_data_struct1;block=end]
By: Ivor
Date: 2016-03-11
Time: 23:14

Re: Struggling with conditional blocks

Thanks, ok now I understand that sentence in the manual ;>)

I have implemented your advice and it is working fantastic! See below.
You will notice the problem with the &#91 for the fields that contain a '['. I think I saw something on that in the manual...
How would you suggest that I recurse the struct. in the example I am using, the sCrestPoint is an array structs.

    //----------------------------------------------------------------------------
    //   Printing example of getting message fields
    //----------------------------------------------------------------------------
    [grp_messages.msg_name]_class(void)
    {
       [msg_data_struct1;block=begin;p1=[grp_messages.msg_name];when [msg_data_struct1.msg_fieldtype]  == 'struct']
       [structfields;block=begin;p1=[msg_data_struct1.msg_fieldname]] s[grp_messages.msg_name]_Data.[msg_data_struct1.msg_fieldname].[structfields.struct_name] = [structfields.struct_defaultvalue]; //[structfields.struct_description]
       [structfields;block=end]
       [msg_data_struct1;block=end]
       [msg_data_struct1;block=begin;default]
        s[grp_messages.msg_name]_Data.[msg_data_struct1.msg_fieldname] = [msg_data_struct1.msg_defaultvalue];
       [msg_data_struct1;block=end]
    }
with result
   //----------------------------------------------------------------------------
   //   Printing example of getting message fields
   //----------------------------------------------------------------------------
   Icc2Hmi_CrestPointList_class(void)
   {
     
       sIcc2Hmi_CrestPointList_Data.sCrestPointList.bNumberOfPoints = 0; //Number of points in the list
       sIcc2Hmi_CrestPointList_Data.sCrestPointList.bActivePointNumber = 0; //Current active point in the list
       sIcc2Hmi_CrestPointList_Data.sCrestPointList.sCrestPoint[MAX_NUM_CRESTPOINTS] = 0; //List of 20 crest points
     
     
       sIcc2Hmi_CrestPointList_Data.eLduMode = MODE_IDLE;
     
       sIcc2Hmi_CrestPointList_Data.sSomeOtherList.bNumberOfPoints = 0; //Number of points in the list
       sIcc2Hmi_CrestPointList_Data.sSomeOtherList.bActivePointNumber = 0; //Current active point in the list
       sIcc2Hmi_CrestPointList_Data.sSomeOtherList.sOtherPoint[2] = 0; //List of 2 other points
     
     
   }
By: Skrol29
Date: 2016-03-11
Time: 23:38

Re: Struggling with conditional blocks

> You will notice the problem with the &#91 for the fields that contain a '['. I think I saw something on that in the manual...

This is a protection against field injection.
You simply have to use parameter "protect=no".
[msg_data_struct1.msg_fieldname;protect=no]
By: Ivor
Date: 2016-03-11
Time: 23:40

Re: Struggling with conditional blocks

Thanks, you are amazing, there seems to be nothing that TBS cannot do.
By: Skrol29
Date: 2016-03-11
Time: 23:49

Re: Struggling with conditional blocks

:-)
By: Ivor
Date: 2016-03-12
Time: 00:11

Re: Struggling with conditional blocks

So this is now what I end up with. It looks really good.

The only thing I still need to sort out is to detect that the field is an array (probably with some regular expression) and then to insert some loop code. Unfortunately regular expressions are not my strong point.

But first, i must go get some sleep ;>)

    Icc2Hmi_CrestPointList_class(void)
    {
       sIcc2Hmi_CrestPointList_Data.sCrestPointList.bNumberOfPoints = 0; //Number of points in the list
       sIcc2Hmi_CrestPointList_Data.sCrestPointList.bActivePointNumber = 0; //Current active point in the list
                 sIcc2Hmi_CrestPointList_Data.sCrestPointList.sCrestPoint[MAX_NUM_CRESTPOINTS].bPointValid = 0; //Status of this points data
                 sIcc2Hmi_CrestPointList_Data.sCrestPointList.sCrestPoint[MAX_NUM_CRESTPOINTS].wPointElevation = 0; //Point Elevation
                 sIcc2Hmi_CrestPointList_Data.sCrestPointList.sCrestPoint[MAX_NUM_CRESTPOINTS].wPointBearing = 0; //Point Bearing
       sIcc2Hmi_CrestPointList_Data.eLduMode = MODE_IDLE;
    }
By: Ivor
Date: 2016-03-12
Time: 10:38

Re: Struggling with conditional blocks

Scroll, is it possible to globally enable the [protect = no]? Because the '[' is used in c++, I need to put it in almost every block.

Regards
Ivor
By: Ivor
Date: 2016-04-06
Time: 08:50

Re: Struggling with conditional blocks

Skrol, I am struggling with a piece of template that I am sure is right, but it does not work. I have stared at it for hours and tried all sorts of "tricks" to get it to work. Obviously doing something stupid again.

I would much appreciate it if you could have a quick look at it. Merci.

The problem I have is that the "Construct" message portion is not executed(inserted) even though the messages does exist with that condition.

The msg_grp_names25 field contains two groupings;
LDU Messages
DDU Messages

In each of these groupings, messages are contained with a name (msg_name) format as below:
Cdu2Ldu_xxxx
Ldu2Cdu_xxxx
Cdu2Ddu_xxxx
Ddu2Cdu_xxxx

where xxxx is a specific message's name.

[msg_grp_names25;block=begin]
1[msg_grp_names25.msg_group_name]
[grp_messages25;block=begin;p1=[msg_grp_names25.msg_group_name];when [grp_messages25.msg_name] ~= '/^Cdu2.*/';several]
void CduModule_class::Parse_[grp_messages25.msg_name]_Msg_( void )
{
}
void CduModule_class::Handle_[grp_messages25.msg_name]_Msg_(void)
{
}
[grp_messages25;block=end]
2 [msg_grp_names25.msg_group_name]
[grp_messages25;block=begin;when [grp_messages25.msg_name] ~= '/^2Cdu.*/']
3 [msg_grp_names25.msg_group_name]
void CduModule_class::Construct_[grp_messages25.msg_name]_Msg_(void)
{
}
[grp_messages25;block=end]
4 [msg_grp_names25.msg_group_name]
[msg_grp_names25;block=end]