Hoof
Hoof

Reputation: 1758

Grails relative path to controllers view folder

Having a Grails 2.1 application, where I have a taglibrary for rendering a summary for different controllers, I have an issue pointing at the correct view-folder.

Eg. TestAController and TestBController both have a controller specific view file called summary.gsp in their respective view folders. That is /testa/summary.gsp and /testb/summary.gsp.

How can I my taglib render the summary.gsp that is related to the controller currently in action - I need to set a path like "??/summary-gsp".

I don't want to implement any if/else logic as there could potentially be 10000 controllers using this taglib, all specifying their own summary.gsp.

Is this doable?

Upvotes: 0

Views: 596

Answers (2)

Dónal
Dónal

Reputation: 187529

The caller should pass in the path to the template as an argument to the tag. If this argument is omitted you could use a convention for locating the template, e.g.

class MyTagLib {

  def renderSummary = {attrs ->

    def defaultTemplatePath = "/${params.controller}/summary"
    def templatePath = attrs.template ?: defaultTemplatePath
    out << g.render(template: templatePath)
  }
}

Upvotes: 0

Dopele
Dopele

Reputation: 577

You can access the params object in your taglib so:

out << render(template: "/${params.controller}/summary")

Upvotes: 1

Related Questions