Search code examples
playframeworkplayframework-2.0playframework-2.2playframework-2.3

Play! Framework: Scala code inside play. scala.html template


I have a table in scala.html. Inside that table I need to compare if a certain string is equal to the yesterday's date. So I need to use a Scala code inside the template file.

    <table class="testResults zebra-striped" id="table1">
                <thead>
                    <tr>
                        @header("hssBuildNumber", "Build #")
                    </tr>
                </thead>
                <tbody>

                   @for(jobResult <- currentPage) {

                     <td>                
                        @{
                          var date = new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L);
                          var formatter = new SimpleDateFormat("yyyy-MM-dd");
                          var yesterday = formatter.format(date).toString();

                          @if(jobResult.hssBuildNumber.contains(yesterday)){
                             //do smth..
                          }
                       }
                     </td>
                    }

                </tbody>
    </table>

This doesn't work. Compilation error: not found: value yesterday. I will appreciate your help.

Thanks.


Solution

  • This is how you need to pass your value, i believe, http://www.playframework.com/documentation/2.0/ScalaTemplates#Declaring-reusable-values

    You may also consider normalizing your code to following one-liner:

    @if(jobResult.hssBuildNumber.contains(new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L)))){