Grails templating & Ajax
Published by peter August 24th, 2007 in ajax, grails.Grails' templating system is cool. There is no doubt about it. It's what I wanted JSP2.0 tagfiles to be. In essence they're not so different from tagfiles in practice their versatility is a fair bit higher.
Templates in Grails reside in the views folder of your project, and are plain gsp files prefixed with an underscore (i.e. /views/task/_task.gsp). The can contain anything a gsp template can.
So, say we have a simple template called _textile.gsp:
-
<div class="textileText">
-
<g:textile text="${text}"/>
-
</div>
It can be rendered in a view using the render tag like this:
-
<g:render template="textile" var="text" bean="${task.description}"/>
Or, using an iterator:
-
<g:render template="textile" var="text" collection="${task?.comments}" />
Verry nice, you'll probably say... but I can easily do that using a JSP tag as well. True. Enter the fact that you can call a template from your controller as well:
-
def textilePreview = {
-
render(template: "textile", var: "text", bean: params.text)
-
}
This is specifically useful when Ajax is put into the equation, since it creates a simple framework to create templates to be used for re-rendering parts of the page after the page has loaded:
-
<!-- snip -->
-
<g:remoteTextArea id="description" action="textilePreview" update="descPreview" paramName="text" name='description' cols="240" value="${task?.description}"/>
-
-
<div id="descPreview">
-
<g:render template="textile" var="text" bean="${task.description}"/>
-
</div>
The remoteTextara (an adapted version of the standard remoteField tag) fires Ajax calls to the textilePreview action, and the response is writen in de 'descPreview' div (specified by the 'update' attribute). Nice!
When I find some time I'll try and put a short video in, screenshots of dynamic updates are just a bit sad




















0 Responses to “Grails templating & Ajax”
Please Wait
Leave a Reply