scalatra icon indicating copy to clipboard operation
scalatra copied to clipboard

Add reverse route support for Twirl

Open takezoe opened this issue 7 years ago • 2 comments

This is a port of reverse routing support for Scalate in the scalatra-scalate module: https://github.com/scalatra/scalatra/blob/2.7.x/scalate/src/main/scala/org/scalatra/scalate/ScalateUrlGeneratorSupport.scala

First, controller must extend TwirlReverseRouteSupport.

class MyController extends ScalatraServlet with TwirlReverseRouteSupport {
  val login = get("/login"){
    ...
  }
  val article = get("/article/:id"){
    ...
  }
}

Then you can render URL of specified routes (specify by bound field names) in Twirl templates as follows:

@import org.scalatra.twirl.url._
@()(implicit request: javax.servlet.http.HttpServletRequest)
...
<!-- Without parameter -->
<a href="@url("login")">Login</a>
<!-- With parameter -->
<a href="@url("article", "id" -> "123")">Article</a>
...

takezoe avatar Nov 26 '17 04:11 takezoe

@takezoe

I think that it is necessary to merge this PR for the release of Scalatara 2.7. Are there any blocking factors at present?

magnolia-k avatar May 06 '19 12:05 magnolia-k

There is no blocker.

Why I haven't merged this pull request yet is that I'm not sure if this feature is actually necessary because I don't use the reverse routing even with Scalate.

Also I'm wondering if there is a better way to support it than url helper.

takezoe avatar May 06 '19 17:05 takezoe