Java Play2 - Common Templates?

Can I create generic templates?

Pseudocode:

@(myForm: Form[T]) 

Therefore, I can use them like this:

 @inputText( myForm("title"), '_label -> "title" ) 
  • If so, how to do it?
+7
source share
3 answers

Use underscore for this:

 @(form: Form[_]) 

This is called an existential type in Scala, it roughly means "there is a type parameter, but I don't care what it is."

+9
source

I do not like the @(form: Form[_]) approach, as it gives out type safety from the window.

When passed to ok(form.render(userform)) you will not get any help from the compiler when you @form("lastNme") in your template, but you will get a runtime error if you did not catch it during testing.

What I am doing is providing an interface (or feature in game 2 scala) for general forms (for example, registering for membership and registering a conference, league statistics and team statistics, etc.); thus, typos with fat fingers, etc., are detected during compilation.

Yes, the more templates to solve, but based on the dynamic language side of the fence, the less I have to deal with runtime errors, the better ...

+4
source

Have you tried

in the app/views package create new flags: pseudo.scala.html

 @(someParam: String) <h1>This is my pseudo template</h1> <div>And there is some param: <b>@someParam</b></div> 

Then, in any other form, you can use it as follows:

 ... <div>@pseudo("param pam pam")</div> ... 

Of course, your parameters should not be String , so you can go there Form[T] , List[T] or something else.

+2
source

All Articles