views:

175

answers:

1

I would like to implement the following:

import org.springframework.web.servlet.support.RequestContextUtils as RCU

class HomeController {
    def home = {
        def locale = RCU.getLocale(request)
        render view: viewExists("home_$locale") ? "home_$locale": "home"
    }
}

What is the code of boolean viewExists(String viewPath) that returns true if the input argument points to a valid GSP or Template file?

+2  A: 

You could try this:

def uri = "test123.gsp"        
def resource = grailsAttributes.pagesTemplateEngine.getResourceForUri(uri)

if ( resource && resource.file && resource.exists() ) {
    // exists
}

(modified from this post) but note the caveat that getResourceForUri() is private (see here). The code works for me but it doesn't give me a warm/fuzzy feeling.

I think the question is to frame your question in terms of the larger goal/problem and try to solve that. Grails probably has an answer.

Michael Easter