I have used the Grails Rendering Plugin in the past with much success in creating PDFs. Throw now I would like to create a simple text file, using a gsp. I loved the ease of using a model to define how to insert information into the template. I realize I don't need to render a text file, but is there a similar way to use a template to just create an ordinary text file?
Example from how to render a jpg using the Grails Render Plugin: (notice the model use)
def bytes = gifRenderingService.render(template: '/images/coupon', model: [serial: 12345])
// Render to a file
new File("coupon.jpg").withOutputStream { outputStream ->
jpegRenderingService.render(template: '/images/coupon', model: [serial: 12345])
}
If there isn't an easy way like the above example, since my information is coming from multiple domain classes should I just create <g> tags in my gsp template that pulls based on conditions needed? If that is the case.. how would I insert a variable into my gsp template from my service?
You might take a look at the grails.gsp.PageRenderer utility class. It allows you to render .gsp templates as a String:
String gspOutput = groovyPageRenderer.render(view: '<your view>.gsp', model: [ modelObj1: ... ])
... or directly to a Writer or OutputStream:
groovyPageRenderer.renderTo(view: '<your view>.gsp', model: [ modelObj1: ... ], <writer or OS>)
Much more detail can be found here: http://mrhaki.blogspot.com/2012/03/grails-goodness-render-gsp-views-and.html
To render the template as text file you should set the content type of the response to text/plain
I'd think you just create your text template as standard, with <g> tags etc..., then call the standard grails render() function on the template with a contentType of 'text/plain'. No plugin necessary?
Add the following code to the GSP file
<%#page contentType="text/plain"%>
Related
In the following what is a template? Where do I create it and how is it used?
def RenderDemo = {
//Render text to response
render "hellooooo"
//Render text for specified content type/ encoding
render (text: "<strong><h1><marquee>Akshay</marquee></h1></strong>" , contentType: "text/html")
//Render a Template to the response for the specified model...
def theShining = [title: "The Shining" , auther : "king"]
render(template: "RenderDemo" , model: [book : theShining])
}
A template is a partial GSP or a fragment of a GSP. It's intended to be reused. Just like any other GSP it belongs in the grails-app/views/ directory structure. All templates have a filename that begins with an underscore _. So in your example the template would be: grails-app/views/render/_renderDemo.gsp.
I highly recommend you read the official documentation on views and templates that explains why you would use a template as well as providing you with even more details about templates in general.
I am working with a Grails 2.3.6 application.
I tried many different things to generate PDF, but most of them failed.
Is it possible to generate a text file of the contents of GSP file? Then have a button called EXPORT and when user clicks on that, the text file will download into there system.
Will it be possible to do that by passing the URL of GSP file?
This is quite straightforward. You just need to specify the response type (text/plain) in the render method from your controller. You can have plain text in the gsp file and use the tags where needed.
def textFile = {
response.setHeader('Content-Disposition', 'Attachment;Filename="textFile.txt"')
render view: 'textFile', contentType: 'text/plain'
}
textFile.gsp:
Dear ${name},
This is a text file.
As for pdf, I recommend the amazingly good grails rendering plugin.
For the parameters passed in the model attribute of the render method to a grails template, how can I specify the type of the parameters in the templates?
I am using grails 2.4.4. This is merely required for autocomplete features in Idea and is not at all related to the file templates of Intellij Idea.
For example, if I have the following tag definition
def markRead = { attrs ->
out << render(
template: "/templates/commons/post/markRead",
model: [
user: attrs.user,
post: attrs.post
]
)
}
And in template I have below (WIP so not complete)
<g:if test="${user != null}">
</g:if>
I want to do something inside the if. But as this was passed through the model there is no type information associated with it and hence there is no autocomplete present. The logical solution seems to be specifying the type somewhere. How can I do that?
I was trying this out when I found the solution for autocomplete in Idea. Using type coercion in the tag library was enough for the autocomplete. I did the below and it worked.
model: [user: attrs.user as User, post: attrs.post as Post]
I would like to use withFormat within my scaffolding controller and added a scaffolding view 'list.csv.gsp'. But without generating my list.csv.gsp view, grails doesn't use the scaffolding list.csv.gsp view.
After generating de list.csv.gsp view it works fine. But I won't create all these views, because they are all the same.
My scaffolding controller list action ends with the code below:
withFormat {
html {
[${propertyName}List: ${propertyName}List, ${propertyName}Total: ${propertyName}List.getTotalCount(), params: cleanParams(params)]
}
csv {
response.setContentType('text/csv')
response.setHeader('Content-Disposition', "attachment; filename=\${message(code: '${domainClass.propertyName}.label')}.csv")
[${propertyName}List: ${propertyName}List]
}
}
I don't know if you still have a problem with this but the grails docs for with format have this quote:
Grails ignores the HTTP Accept header unless you add a grails.mime.use.accept.header = true setting to your Config.groovy file. In other words, withFormat() will be completely unaffected by the Accept header without that setting.
See withFormat in the grails docs.
On your view you might need to write it generic and put in a common directory and refer your controller template to use it. Maybe make it as a _template and render it.
hope this helps.
Is there a way to have a custom taglib operate on data loaded in a .gsp file such that it picks up any tags embedded in the data stored in the database. For instance, let's say I'm doing:
<g:each in="${activities}">
<li>${it.payload}</li>
</g:each>
And inside the payload, which is coming from the database, is text like
"Person a did event <company:event id="15124124">Event Description</company:event>"
Can you have a taglib that handles company:event tags on the fly?
You could write a custom tag which uses the GroovyPagesTemplateEngine to process the text and write it to the output stream. I think you can get an instance of the TemplateEngine injected into your tag from the applicationContext.
I don't have any example code sorry,
cheers
Lee