Search code examples
htmljspjstl

Hide <hr> if div empty


I have the following piece of code (jsp)

        ...
        <hr>
        <div class="actions">
            <div class="btn-toolbar">
                <c:if test="${condition1}">
                    <button type="button" class="btn" >
                        btn1
                    </button>
                </c:if>
                <c:if test="${condition2}">
                    <button type="button" class="btn">
                        btn2
                    </button>
                </c:if>
                <c:if test="${condition3}">
                    <button type="button" class="btn">
                        btn3
                    </button>
                </c:if>
            </div>
        </div>
        <hr> 
        ...

Depending on the conditions, div may be empty, and then we get a double hr-tag. Is there an elegant way to display first hr-tag only if div is not empty?


Solution

  • Save c:if test results to variables?

    <div class="actions">
        <div class="btn-toolbar">
            <c:if test="${condition1}" var="rt1">
                <button type="button" class="btn" >
                    btn1
                </button>
            </c:if>
            <c:if test="${condition2}" var="rt2">
                <button type="button" class="btn">
                    btn2
                </button>
            </c:if>
            <c:if test="${condition3}" var="rt3">
                <button type="button" class="btn">
                    btn3
                </button>
            </c:if>
        </div>
    </div>
    <c:if test="${rt1 || rt2 || rt3}">
    <hr> 
    </c:if>