How to know programmatically if a view or a layout file exists in grails

前端 未结 3 358
予麋鹿
予麋鹿 2021-01-12 22:53

I want to know programmatically if a view or a layout exists in grails.

I am thinking in obtain the absolutepath and ask for File.exists but I don\'t know how to obt

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-12 23:42

    Additionally to what amra said, you can also use grailsAttributes(see docs for GrailsApplicationAttributes). Quick example:

    private templateExists(String name) {
        def template = grailsAttributes.getTemplateUri(name, request)
        def resource = grailsAttributes.pagesTemplateEngine
                                       .getResourceForUri(template)
        return resource && resource.file && resource.exists()
    }
    

    This example is of course for templates but as you can see from the docs, similar method exists for views too.

提交回复
热议问题