Obtaining the string of a GSP rendering (instead of outputting it directly to the HTTP response)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Obtaining the string of a GSP rendering (instead of outputting it directly to the HTTP response)

elvanor
Hi,

Is there any way to render a GSP, but obtain the result as a string
that can be reused later? I don't want the result to be outputted to
the HTTP reply yet.

Also I have a second question, how to obtain the path to the artefacts
in the grails-app directory (preferably in a way that will work both
in development and deployment mode). Eg, I have a file in
grails-app/views/myObject/myView.gsp, how do I obtain its path in my
application? getRealPath("/") returns the path to the web-app
directory as far as I know.

Thanks,
Jean-Noel

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the string of a GSP rendering (instead of outputting it directly to the HTTP response)

Ole Trenner
Jean-Noël Rivasseau wrote:
> Is there any way to render a GSP, but obtain the result as a string
> that can be reused later? I don't want the result to be outputted to
> the HTTP reply yet.


I think this is strongly related to Graeme's attempt to change the
taglib behaviour
(http://www.nabble.com/forum/ViewPost.jtp?post=9597727&framed=y),
because even if you use the GroovyPagesTemplateEngine to render a
template via StringWriter into a String, all dynamic tags will still
output directly to the response.
So far we haven't found a workaround for this behaviour aside from
rewriting the tag logic into a string returning method.

Goes without saying that we can't wait for the new taglib behaviour to
become reality :)


> Also I have a second question, how to obtain the path to the artefacts
> in the grails-app directory (preferably in a way that will work both
> in development and deployment mode). Eg, I have a file in
> grails-app/views/myObject/myView.gsp, how do I obtain its path in my
> application? getRealPath("/") returns the path to the web-app
> directory as far as I know.


GroovyPagesTemplateEngine.java (see
<http://svn.codehaus.org/grails/tags/GRAILS_RELEASE_0_5_6/src/web/org/codehaus/groovy/grails/web/pages/GroovyPagesTemplateEngine.java>)
contains some code to that respect, namely
getResourceForUri(String uri), getResourceWithinContext(String uri) and
getUriWithinGrailsViews(String relativeUri). Maybe this will help...

Best regards. Ole.


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the string of a GSP rendering (instead of outputting it directly to the HTTP response)

elvanor
Hi,

> I think this is strongly related to Graeme's attempt to change the
> taglib behaviour
> (http://www.nabble.com/forum/ViewPost.jtp?post=9597727&framed=y),
> because even if you use the GroovyPagesTemplateEngine to render a
> template via StringWriter into a String, all dynamic tags will still
> output directly to the response.
> So far we haven't found a workaround for this behaviour aside from
> rewriting the tag logic into a string returning method.
>
> Goes without saying that we can't wait for the new taglib behaviour to
> become reality :)

Is there an ETA on that feature? I've looked at the URL and it seems
that the discussion was rather old (March 2007).

Also, currently for what I want to achieve I need only very simple
GStrings replacements on the GSP. No Dynamic tags at all. Would there
be a way with the GroovyPagesTemplateEngine you mention?


> GroovyPagesTemplateEngine.java (see
> <http://svn.codehaus.org/grails/tags/GRAILS_RELEASE_0_5_6/src/web/org/codehaus/groovy/grails/web/pages/GroovyPagesTemplateEngine.java>)
> contains some code to that respect, namely
> getResourceForUri(String uri), getResourceWithinContext(String uri) and
> getUriWithinGrailsViews(String relativeUri). Maybe this will help...

Hmm. This seems rather specific to the views, whereas I would be
interested in getting any artefact in grails-app, or even something
that's not a Grails artefact... I guess there must be a better way to
do this...

Jean-Noel

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the string of a GSP rendering (instead of outputting it directly to the HTTP response)

peter_m
Hi Jean-Noel,

At least as of the current 0.6-SNAPSHOT:
grailsApplication is what you're looking for. It's available in your
Controller classes by convention, and you can obtain the current
grailsApplication by calling
org.codehaus.groovy.grails.commons.ApplicationHolder.getApplication().

grailsApplication.allArtefacts is a List of all artefacts registered
in the current context.

and some helper properties:

grailsApplication.controllerClasses is a List of controllers.
grailsApplication.domainClasses is a List of domain classes.

Hope this helps some,
-Peter

On 8/15/07, Jean-Noël Rivasseau <[hidden email]> wrote:

> Hi,
>
> > I think this is strongly related to Graeme's attempt to change the
> > taglib behaviour
> > (http://www.nabble.com/forum/ViewPost.jtp?post=9597727&framed=y),
> > because even if you use the GroovyPagesTemplateEngine to render a
> > template via StringWriter into a String, all dynamic tags will still
> > output directly to the response.
> > So far we haven't found a workaround for this behaviour aside from
> > rewriting the tag logic into a string returning method.
> >
> > Goes without saying that we can't wait for the new taglib behaviour to
> > become reality :)
>
> Is there an ETA on that feature? I've looked at the URL and it seems
> that the discussion was rather old (March 2007).
>
> Also, currently for what I want to achieve I need only very simple
> GStrings replacements on the GSP. No Dynamic tags at all. Would there
> be a way with the GroovyPagesTemplateEngine you mention?
>
>
> > GroovyPagesTemplateEngine.java (see
> > <http://svn.codehaus.org/grails/tags/GRAILS_RELEASE_0_5_6/src/web/org/codehaus/groovy/grails/web/pages/GroovyPagesTemplateEngine.java>)
> > contains some code to that respect, namely
> > getResourceForUri(String uri), getResourceWithinContext(String uri) and
> > getUriWithinGrailsViews(String relativeUri). Maybe this will help...
>
> Hmm. This seems rather specific to the views, whereas I would be
> interested in getting any artefact in grails-app, or even something
> that's not a Grails artefact... I guess there must be a better way to
> do this...
>
> Jean-Noel
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the string of a GSP rendering (instead of outputting it directly to the HTTP response)

Ole Trenner
In reply to this post by elvanor
Jean-Noël Rivasseau wrote:
> Also, currently for what I want to achieve I need only very simple
> GStrings replacements on the GSP. No Dynamic tags at all. Would there
> be a way with the GroovyPagesTemplateEngine you mention?

There was recently a posting of Ramon Zöllner on this list that showed a
way of achieving this.

begin quote
(http://www.nabble.com/Problem-with-groovyPagesTemplateEngine-tf4247609.html#a12088131)

     def t = groovyPagesTemplateEngine.createTemplate(templateName)
     def w = t.make(company)
     def sw = new StringWriter()
     def pw = new PrintWriter(sw)

     w.writeTo(pw)
     return sw.toString()

end quote

groovyPagesTemplateEngine is a bean that gets automatically injected
into services if a field with this name is declared.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the string of a GSP rendering (instead of outputting it directly to the HTTP response)

Ramon Zöllner
In reply to this post by elvanor
Hi, Jean-Noël


> Also, currently for what I want to achieve I need only very simple
> GStrings replacements on the GSP. No Dynamic tags at all. Would there
> be a way with the GroovyPagesTemplateEngine you mention?

If you just need replacement/binding of Variables inside a String like:

"Hello ${name}"


Take a look at the Groovy Templateframework
(http://groovy.codehaus.org/Groovy+Templates)

Maybe SimpleTemplateEngine is what you are looking for, no GSP
evaluation, just replacement.

Cheers,
Ramon

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Obtaining the string of a GSP rendering (instead of outputting it directly to the HTTP response)

elvanor
In reply to this post by peter_m
Hi,

Thanks for the information. Today I tried using grailsApplication,
however with this class I was only able to obtain controller and
domain artefacts. What I currently need is another file that I am
storing in the views folder of grails-app, but that is neither a GSP
nor JSP (it's a client side JS
template).

How could I achieve to retrieve the path to this resource?

Thanks

Jean-Noel

On 8/15/07, Peter Mondlock <[hidden email]> wrote:

> Hi Jean-Noel,
>
> At least as of the current 0.6-SNAPSHOT:
> grailsApplication is what you're looking for. It's available in your
> Controller classes by convention, and you can obtain the current
> grailsApplication by calling
> org.codehaus.groovy.grails.commons.ApplicationHolder.getApplication().
>
> grailsApplication.allArtefacts is a List of all artefacts registered
> in the current context.
>
> and some helper properties:
>
> grailsApplication.controllerClasses is a List of controllers.
> grailsApplication.domainClasses is a List of domain classes.
>
> Hope this helps some,
> -Peter
>
> On 8/15/07, Jean-Noël Rivasseau <[hidden email]> wrote:
> > Hi,
> >
> > > I think this is strongly related to Graeme's attempt to change the
> > > taglib behaviour
> > > (http://www.nabble.com/forum/ViewPost.jtp?post=9597727&framed=y),
> > > because even if you use the GroovyPagesTemplateEngine to render a
> > > template via StringWriter into a String, all dynamic tags will still
> > > output directly to the response.
> > > So far we haven't found a workaround for this behaviour aside from
> > > rewriting the tag logic into a string returning method.
> > >
> > > Goes without saying that we can't wait for the new taglib behaviour to
> > > become reality :)
> >
> > Is there an ETA on that feature? I've looked at the URL and it seems
> > that the discussion was rather old (March 2007).
> >
> > Also, currently for what I want to achieve I need only very simple
> > GStrings replacements on the GSP. No Dynamic tags at all. Would there
> > be a way with the GroovyPagesTemplateEngine you mention?
> >
> >
> > > GroovyPagesTemplateEngine.java (see
> > > <http://svn.codehaus.org/grails/tags/GRAILS_RELEASE_0_5_6/src/web/org/codehaus/groovy/grails/web/pages/GroovyPagesTemplateEngine.java>)
> > > contains some code to that respect, namely
> > > getResourceForUri(String uri), getResourceWithinContext(String uri) and
> > > getUriWithinGrailsViews(String relativeUri). Maybe this will help...
> >
> > Hmm. This seems rather specific to the views, whereas I would be
> > interested in getting any artefact in grails-app, or even something
> > that's not a Grails artefact... I guess there must be a better way to
> > do this...
> >
> > Jean-Noel
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>