Search code examples
thymeleaf

Thymeleaf remove based on a variable length and value


I have a variable with variable length eg. 01010011101010101 Not binary, but only 0s and 1s. My task is to switch on and off parts of an email regarding to the variable. So if its 1, the part of the text has to be visible, if 0 then not. I have the following code, but its not working properly:

<li th:remove="${#strings.length(VARIABLE)>=1 and #strings.substring(VARIABLE,0,1) == '1'}? none : all">A</li>
<li th:remove="${#strings.length(VARIABLE)>=2 and #strings.substring(VARIABLE,1,2) == '1'}? none : all">B</li>
<li th:remove="${#strings.length(VARIABLE)>=3 and #strings.substring(VARIABLE,2,3) == '1'}? none : all">C</li>
<li th:remove="${#strings.length(VARIABLE)>=4 and #strings.substring(VARIABLE,3,4) == '1'}? none : all">D</li>

[...]

DONT remove the li tag when the length of the variable is enough AND in the proper position there is a 1 instead of zero.

So for example i'll get 0101 from the variable, in this case I want to get back "BD".

I have several li-s, and I must check the length of the variable or the email will crash.

What is wrong whit this? How would you solve this problem?

Thanks!


Solution

  • Your approach is close - but needs some small changes:

    1. Instead of == '1', use == 1, as the result is interpreted as an integer not a character.

    2. Instead of none and all, use 'none' and 'all' so the results are interpreted as text instead of literal tokens (full disclosure: I was surprised this change is needed; I thought they would behave the same way).

    This gives you the following:

    <ul>
        <li th:remove="${#strings.length(VARIABLE)>= 1 and #strings.substring(VARIABLE,0,1) == 1} ? 'none' : 'all'">A</li>
        <li th:remove="${#strings.length(VARIABLE)>= 2 and #strings.substring(VARIABLE,1,2) == 1} ? 'none' : 'all'">B</li>
        <li th:remove="${#strings.length(VARIABLE)>= 3 and #strings.substring(VARIABLE,2,3) == 1} ? 'none' : 'all'">C</li>
        <li th:remove="${#strings.length(VARIABLE)>= 4 and #strings.substring(VARIABLE,3,4) == 1} ? 'none' : 'all'">D</li>
    </ul>
    

    This generates:

    <ul>
        <li>B</li>
        <li>D</li>
    </ul>
    

    There are probably various different approaches which could work, using Thymeleaf - but I think your approach is good (with the above changes).

    Depending on how your data A, B, C... is sourced you may want to consider using an iterator instead.

    One basic approach (just a starting point for you) is the following:

    <ul th:each="step : ${#numbers.sequence(0, #strings.length(VARIABLE)-1)}">
        <li th:text="${#strings.substring(VARIABLE, step, step+1)}"></li>
    </ul>
    

    This uses an array containing the integers from 0 to the length of your string 01010011101010101 minus one.

    It then steps through that array of integers to pull each value (0 and 1) from the string.

    You could use this as the basis for a different approach - depending on where and how you source your A, B, C data.