Maik Klein
Maik Klein

Reputation: 16148

Java Play 2 - Function as a parameter in scala

I have problems to write reusable code in scala.

If i have something like

@helper.form(action = routes.Guides.addComment(category,title)) {

Is there a way to replace it with a variable?

pseudo code

@(func : Function)
@helper.form(action = func) {

Edit:

Oh.... now it's kinda obvious. The function itself should return a string , so I guess i can just say something like this

@(func :String)
..

.

return ok (form.render(routes.Guides.test()))

Testing it now

Upvotes: 1

Views: 415

Answers (2)

Marius Soutier
Marius Soutier

Reputation: 11274

May I suggest an alternative? Use Call directly.

@(route: Call)

@helper.form(action = route) {
  ...
}

In Scala, you could even pass only a part of the route and fill the rest from the controller (very useful when you're using pagination).

Upvotes: 2

Maik Klein
Maik Klein

Reputation: 16148

figured it out.

with

routes.Guides.test().url

you get the url and then you can use it as a parameter

for example

@guidesComment((routes.Guides.addComment(ug.category,ug.title)).url)

guidesComment looks like this

@(func: String)

Then use it like this

<form action="@func" method="POST">

Upvotes: 2

Related Questions