Optional path element

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Optional path element

Paul_Fairless
Hi,

Is it possible to have an optional part of a path e.g.

handlers {
  get("story/:storyId/?:slug") {
    if(!slug) slug ="something"
    get(TemplateRenderer).render "story.html", story: storyId, slug:slug
  }
}

I though i saw some early examples but can't find any reference of how to do it now?

Cheers
Paul
Reply | Threaded
Open this post in threaded view
|

Re: Optional path element

Luke Daley
Administrator
There isn't at the moment. I'd like to support this though.

Interested in contributing?

Would need to be implemented here: https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/org/ratpackframework/path/internal/TokenPathBinder.java#L29

Unit tests: https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/test/groovy/org/ratpackframework/path/internal/TokenPathBinderTest.groovy#L21

Functional tests: https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/test/groovy/org/ratpackframework/path/PathParamsSpec.groovy#L21

Should be just a matter of messing with the regex used to match the path.

One thing to keep in mind is that TokenPathBinder.bind() is on a hot path, so can't be slow.
Reply | Threaded
Open this post in threaded view
|

Re: Optional path element

Paul_Fairless
It was a bit harder than expected, but pull request sent for your review https://github.com/ratpack/ratpack/pull/68

Cheers
Paul
Reply | Threaded
Open this post in threaded view
|

Re: Optional path element

Luke Daley
Administrator
Awesome. Commented on the PR.