views:

608

answers:

2

Hi all,

I've looked around but could not find a way of simply including or rendering *.html files in Grails. My application needs to g.render or <g:render> templates which are delivered as html files. For this, as we know, html files have to be converted to _foo.gsp files in order to get rendered. I am totally surprised as to why isn't there a direct support for html or is there one??

Thanks!

+5  A: 

One obvious option is to simply rename your HTML files from foo.html to _foo.gsp and then use <render template="foo">. However this is so obvious that I'm sure you've already thought of it.

If you simply want to render a HTML file from within a controller you can use the text parameter of the render controller method

def htmlContent = new File('/bar/foo.html').text
render text: htmlContent, contentType:"text/html", encoding:"UTF-8"

If you want to do the same thing from within a .gsp, you could write a tag. Something like the following (untested) should work:

import org.springframework.web.context.request.RequestContextHolder

class HtmlTagLib {

  static namespace = 'html'

  def render = {attrs ->

    def filePath = attrs.file

    if (!file) {
      throwTagError("'file' attribute must be provided")
    }

    def htmlContent = new File(filePath).text
    out << htmlContent
  }
}

You can call this tag from a GSP using

<html:render file="/bar/foo.html"/>
Don
Thanks Don! This looks like it (the second part ie.). I'll give this a try and update my results here.
sector7
+2  A: 

What is it you are trying to accomplish?

  1. Render html from a controller? In that case all you should have to do is redirect the user to file from your control.

    redirect(uri:"/html/my.html")

  2. Use html-files instead of gsp template-files? Thing is, Grails is a "Convention over Configuration"-platform and that means you will have to do some things "the Grails way". The files needs the _ and the .gsp but the name can be whatever you like even if it's easier when you use the same name as the controller. What you gain from doing that is the knowledge that every developer that knows grails and comes into your project will understand how things are tied together and that will help them get started quickly.

xlson
In a real world scenario, might I say, things don't work according to rigid Conventions. A Framework should at least support working with its most fundamental elements - HTML in this case.Answering your question, we have a single GSP page wherein multiple Gauges are rendered. Each of these Gauges are pure _GSP_ templates BUT every once a while they need to be directly rendering HTML files which are dropped to a particular folder /some_folder/*.htmlIn the TAGLIB which I've created we need to toggle between rendering from GSP templates or HTML files.Apologies for not clearing this first.
sector7
Thanks for clearing that up. Don's answer seems like a good fit for you and honestly your need is specific enough to warrant it's own custom taglib.
xlson