Bounded generic parameters in Play Framework template

Itchy

How can I use bounded generic parameters in a Scala template within a Play Framework 2.3 Java project?

I currently have something like:

@(entities: List[_ <: Entity], currentEntity: Entity)

<ul>
    @for(entity <- entities) {
        @if(currentEntity.equals(entity)) {
            <li><strong>@entity</strong></li>
        } else {
            <li>@entity</li>
        }
    }
</ul>

However, I can call this with different types of entities in entities and currentEntity - which is not nice. I would like to do something like:

@[T <: Entity](entities: List[T], currentEntity: T)
...

But this gives me Invalid '@' symbol as a compile error.

millhouse

As @m-z points out, it's not supported (yet). But you might be able to get the type-safety you desire (at the cost of another family of classes) by marshalling your arguments into a View object first:

case class HighlightedListView[E <: Entity](entities:List[E], currentEntity:E)

Now in your controller, load up a new HighlightedListView instance instead of feeding the parameters straight to the template:

 def foo = Action {
  ...
  // Assuming some SubEntity exists, the compiler will enforce the typing:
  val hlv = HighlightedListView[SubEntity](entities, currentEntity)


  Ok(html.mytemplate(hlv))

}

As the comment states, the compiler will barf if your types don't line up. Then the template can be pretty loose with types, because we know we're safe:

@(hlv:HighlightedListView[_])

<ul>
    @for(entity <- hlv.entities) {
        @if(hlv.currentEntity.equals(entity)) {
            <li><strong>@entity</strong></li>
        } else {
            <li>@entity</li>
        }
    }
</ul>

You can even take advantage of your new View object to add helper methods, which can make for easier-to-read templates, and facilitate unit testing:

case class HighlightedListView[E <: Entity](entities:List[E], currentEntity:E) {
   def shouldHighlight(e:Any):Boolean = currentEntity.equals(e)
}

leading to:

 @if(hlv.shouldHighlight(entity)) {
     <li><strong>@entity</strong></li>
 } else {
     <li>@entity</li>
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Play framework 2.1.3 function that will render scala template with given parameters

From Dev

Play Framework Scala Template

From Dev

Play Writes with generic type parameters

From Dev

Generic lambda functions with template parameters

From Dev

Error passing parameters in play framework

From Dev

Play Framework: POST request with parameters

From Dev

Can not access parameters in play framework

From Dev

Play framework dynamic template include

From Dev

Play framework dynamic template include

From Dev

Java bounded generic covariance

From Dev

Using class level like generic bounded type parameters for method's formal arguments

From Dev

How to validate optional query parameters in Play Framework?

From Dev

Using POST routes parameters in Play Framework

From Dev

Play framework parsing semicolons in query parameters

From Dev

Reverse routing with optional GET parameters in Play Framework

From Dev

Using POST routes parameters in Play Framework

From Dev

Have a List in Play framework web service parameters

From Dev

How to inject Application in Play Framework 2.5 template

From Dev

Play Framework template that is actually a JS file

From Dev

Display Dynamic Image in Play! Framework Template

From Dev

Get scala template in a variable in Play Framework

From Dev

play framework passing sub class to scala template

From Dev

Play Framework template, iterate backwards over List

From Dev

using bootstrap with play framework scala template

From Dev

Play2 Framework Float Format in Template

From Dev

Play framework scala template with case statement on enumeration

From Dev

Get Javascript variable in Template play framework 1.2.6

From Dev

play framework passing sub class to scala template

From Dev

using bootstrap with play framework scala template

Related Related

  1. 1

    Play framework 2.1.3 function that will render scala template with given parameters

  2. 2

    Play Framework Scala Template

  3. 3

    Play Writes with generic type parameters

  4. 4

    Generic lambda functions with template parameters

  5. 5

    Error passing parameters in play framework

  6. 6

    Play Framework: POST request with parameters

  7. 7

    Can not access parameters in play framework

  8. 8

    Play framework dynamic template include

  9. 9

    Play framework dynamic template include

  10. 10

    Java bounded generic covariance

  11. 11

    Using class level like generic bounded type parameters for method's formal arguments

  12. 12

    How to validate optional query parameters in Play Framework?

  13. 13

    Using POST routes parameters in Play Framework

  14. 14

    Play framework parsing semicolons in query parameters

  15. 15

    Reverse routing with optional GET parameters in Play Framework

  16. 16

    Using POST routes parameters in Play Framework

  17. 17

    Have a List in Play framework web service parameters

  18. 18

    How to inject Application in Play Framework 2.5 template

  19. 19

    Play Framework template that is actually a JS file

  20. 20

    Display Dynamic Image in Play! Framework Template

  21. 21

    Get scala template in a variable in Play Framework

  22. 22

    play framework passing sub class to scala template

  23. 23

    Play Framework template, iterate backwards over List

  24. 24

    using bootstrap with play framework scala template

  25. 25

    Play2 Framework Float Format in Template

  26. 26

    Play framework scala template with case statement on enumeration

  27. 27

    Get Javascript variable in Template play framework 1.2.6

  28. 28

    play framework passing sub class to scala template

  29. 29

    using bootstrap with play framework scala template

HotTag

Archive