RejectedSoftware Forums

Sign up

diet-ng conditionallly included tag problem

Would like a clean way to include different table tags depending upon a condition. But there's a snag. I can work around it, but wondering if there's a clean solution.

    - if( condition)
        table(...)
    - else
        table(....)
            tr
                th .....

The indentation of tr is necessary for it to be inside the table, but that indentation stops the else branch from ending. So the table body is not included when the condition is true. How do I end the if keeping the indentation of tr in an elegant way?

Re: diet-ng conditionallly included tag problem

Am 14.03.2017 um 17:28 schrieb Carl Sturtivant:

Would like a clean way to include different table tags depending upon a condition. But there's a snag. I can work around it, but wondering if there's a clean solution.

    - if( condition)
        table(...)
    - else
        table(....)
            tr
                th .....

The indentation of tr is necessary for it to be inside the table, but that indentation stops the else branch from ending. So the table body is not included when the condition is true. How do I end the if keeping the indentation of tr in an elegant way?

The best you can probably do is to use a function or an include:

- void tableContent()
   tr
     th ...

- if (condition)
   table(...)
     - tableContent();
- else
   table(...)
     - tableContent();

Or using include table-content with a table-content.dt template.

Re: diet-ng conditionallly included tag problem

On Fri, 17 Mar 2017 20:59:28 +0100, Sönke Ludwig wrote:

The best you can probably do is to use a function or an include:

- void tableContent()
   tr
     th ...

- if (condition)
   table(...)
     - tableContent();
- else
   table(...)
     - tableContent();

Or using include table-content with a table-content.dt template.

Thank you. Apologies: in the end I didn't use such a table for other reasons.

However, it occurs to me that in a way, what is needed is endif in abstract, something that is at the same level of indentation as the if so it closes it, and yet renders to nothing. So maybe a comment will do!

    - if( condition)
        table(...)
    - else
        table(....)
    //- endif
        tr
            th .....

Haven't tried this, but if it works it might be a useful technique now and again with embedded control structures.

Re: diet-ng conditionallly included tag problem

Am 19.03.2017 um 00:08 schrieb Carl Sturtivant:

On Fri, 17 Mar 2017 20:59:28 +0100, Sönke Ludwig wrote:

The best you can probably do is to use a function or an include:

- void tableContent()
   tr
     th ...

- if (condition)
   table(...)
     - tableContent();
- else
   table(...)
     - tableContent();

Or using include table-content with a table-content.dt template.

Thank you. Apologies: in the end I didn't use such a table for other reasons.

However, it occurs to me that in a way, what is needed is endif in abstract, something that is at the same level of indentation as the if so it closes it, and yet renders to nothing. So maybe a comment will do!

    - if( condition)
        table(...)
    - else
        table(....)
    //- endif
        tr
            th .....

Haven't tried this, but if it works it might be a useful technique now and again with embedded control structures.

Unfortunately due to the way the Diet->D translation is done, this can't
be reasonably made to work within the parser (it would be possible to
write an AST transformation pass, but that would really be a rather
dirty solution).

Another possibility could be to introduce a way to conditionally exclude
certain attributes, for example by recognizing Nullable!T values.
However, if that gets in, the parser should also be extended to handle
attribute lists that span multiple lines, as otherwise this would get
too messy too easily.

Re: diet-ng conditionallly included tag problem

On Fri, 17 Mar 2017 20:59:28 +0100, Sönke Ludwig wrote:

Am 14.03.2017 um 17:28 schrieb Carl Sturtivant:

Would like a clean way to include different table tags depending upon a condition. But there's a snag. I can work around it, but wondering if there's a clean solution.

    - if( condition)
        table(...)
    - else
        table(....)
            tr
                th .....

The indentation of tr is necessary for it to be inside the table, but that indentation stops the else branch from ending. So the table body is not included when the condition is true. How do I end the if keeping the indentation of tr in an elegant way?

The best you can probably do is to use a function or an include:

- void tableContent()
   tr
     th ...

- if (condition)
   table(...)
     - tableContent();
- else
   table(...)
     - tableContent();

When I use this technique with pretty printed HTML I find that indentation goes wrong.

Specifically the HTML produced by the function call is not indented one more step and the first tag from that call is placed on the same line as the tag preceding it in the branch.

Re: diet-ng conditionallly included tag problem

On 8/28/19 1:18 PM, Carl Sturtivant wrote:

On Fri, 17 Mar 2017 20:59:28 +0100, Sönke Ludwig wrote:

Am 14.03.2017 um 17:28 schrieb Carl Sturtivant:

Would like a clean way to include different table tags depending upon a condition. But there's a snag. I can work around it, but wondering if there's a clean solution.

     - if( condition)
         table(...)
     - else
         table(....)
             tr
                 th .....

The indentation of tr is necessary for it to be inside the table, but that indentation stops the else branch from ending. So the table body is not included when the condition is true. How do I end the if keeping the indentation of tr in an elegant way?

The best you can probably do is to use a function or an include:

- void tableContent()
    tr
      th ...

- if (condition)
    table(...)
      - tableContent();
- else
    table(...)
      - tableContent();

When I use this technique with pretty printed HTML I find that indentation goes wrong.

Specifically the HTML produced by the function call is not indented one more step and the first tag from that call is placed on the same line as the tag preceding it in the branch.

I know this is old, but I'm assuming essentially you are changing what
is inside the table tag. Those kinds of things can be variable, but it's
hard to give good advice without more details.

For example, I've found it possible to change classes within the table
itself:

table(class='#{condition ? "class1" : "class2"}'
     tr
         th ...

Or if you have items that can be precomputed, you can initialize them
before doing the table, and put in the precomputed variables.

I've found that I sometimes have to work around the nested nature of
diet, and MOST of the time, it's not too bad. One case where I found it
really bad is I wanted to start a new row whenever two consecutive array
elements differed by a certain field. That was not too easy -- I created
a range that nested the array into a range of array slices to simulate
the structure. Probably could have done a while loop too.

-Steve