2

I've been playing around with golang for a couple of weeks now, and recently started to look into the revel framework.
In other languages/frameworks that I've used, it's always been possible to check what the current route is from within the template. Something that made it easier for me to keep things like navigation in a separate template file, and just do this:

<!-- in header template -->
<ul class="nav">
    {{ if eq .req.action "App.Index" }}
        <li class="active"><a href="#">
    {{ else }}
        <li><a href="{{url "App.Index" }}">
    {{end}}
    Home</a></li>
    <!-- other links here -->
</ul>

This isn't the actual code I'm writing, but I hope this makes it clear what the idea is: Have a template for the navigation handy, and set classes/links according to which action is the current one.
After some time going through the source code, and a number of golang template examples, I couldn't quite see any way in which the current action or anything else is exposed to the template.

To get around this, I'm currently using a func interceptor which automatically sets a render argument for me:

func prependRoute(c *revel.Controller) revel.Result {
    c.RenderArgs["_current_route"] = c.Action
    return nil
}

This is working fine, but it feels a bit hacky. I think I'm probably missing something really obvious that would allow me to reserve func interceptors for when I really need them.

The question, then, is simple: What is the correct, and most reliable way to find out what the current action is in a revel template?

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • This looks about to be the best solution when using revel. Is there any reason you're using revel and not something like [mux](http://www.gorillatoolkit.org/pkg/mux)? Go's got a pretty solid `net/http` package. – Datsik Jan 03 '16 at 09:08
  • @Datsik: I've used the net/http package first. In principle, there's no real need for something like revel yet, but I thought I'd give it a spin because they seem to be looking for (or hopefully even working at) an integrated ORM, which would make it a full, easy to use package. In short: no specific reason for using revel yet, just me wanting to see if there are any promising frameworks being built – Elias Van Ootegem Jan 03 '16 at 15:20
  • fair enough. Revel is pretty solid. I've used it a couple times but I've found it much easier to build apps just using gorillas mux/session package combined with jinzus [gorm package](https://github.com/jinzhu/gorm) – Datsik Jan 03 '16 at 19:50

0 Answers0