q99
q99

Reputation: 1011

Play framework template for cycle

I'm trying to print some html elements in a for cycle in a template.

Template:

    <ul>
     @for(index <- 0 until appointments.size){
    <li>
        @(utils.DateUtil.getDate(appointments(index).getStartDate())) - @appointments(index).getReason()
        <ul>

            @{val procedure = appointments(index).getProcedures()

                if(procedure.size() == 0)
                {
                    <b>žádna procedura nebyla vykonána</b>
                }
                else 
                {
                    <b>test</b>

                    for(proc <- procedure)
                    {
                    <b>for test</b>
                    }
                }

            }


        </ul>

    </li>
    }

</ul>

The problem lies in the else branch.

if I remove this code:

 for(proc <- procedure)
 {
  <b>for test</b>
 }

It prints out test

But if I leave it there nothing from the else branch prints out

I've tried encapsulating the code in curly braces, a different for cycle notation -> for(index2 <- 0 until procedure.size) but nothing worked.

Upvotes: 0

Views: 178

Answers (2)

biesior
biesior

Reputation: 55798

Remember that's a templating engine, although it's Scala based, some things just doesn't work - ie. variables declaration (as by definition it's job of controller). Play has a replacement for that (if really required) as:

@defining(appointments(index).getProcedures()) { procedure => 
   <b>Common HTML using defined @procedure</b>
}

Anyway if I'm not wrong, properly your code written in Play's template should look like below.

@(appointments: List[Appointment])

<ul>
    @for(appointment <- appointments) {
        <li>
            @(utils.DateUtil.getDate(appointment.getStartDate())) - @appointment.getReason()
            @if(appointment.getProcedures() != null && appointment.getProcedures().size()>0) {
                <ul>
                    @for(procedure <- appointment.getProcedures()){
                         <li>@Messages("procedure.execuded") @procedure.name</li>
                    }
                </ul>
            } else {
                <b>@Messages("no.execuded.procedures")</b>
            }
        </li>
    }
</ul>

Note: written from finger, it CAN contain errors

Upvotes: 2

Leo
Leo

Reputation: 2189

@for(proc <- procedure) { for test }

It is also considered a bad practice to define variables inside the view. The right way is to pass all the collections/data-classes from action and just iterate them in the view.

Upvotes: 0

Related Questions