What is template and how and where to create it in gsp? - grails

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.

Related

Grails : Generate text file for GSP

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.

How to use a gsp template to create a text file?

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"%>

Display razor from database

I am trying to display content such as
<p>Text and link - #Html.ActionLink("Link", "Action")</p>
from a database, but if I use #Html.Raw then it doesn't render the link.
Is there anyway to do this?
You will need to use a Razor parser in order to achieve that. Checkout RazorEngine which could be used render the Razor markup to HTML.
print it on the backend using this:
string template = "Hello #Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });
pass the results to front end using a model which contains the result in a string property.
you will need to install the following:
Install-Package RazorEngine

The tag g:include is not passing the model to the view correctly

I have been struggling with these for a few hours now, and have come to the conclusion that it may be a bug with the 2.0.0.RC1.
At first I thought it was the project I was working on, but then I created a complete new project and was able to recreate the bug. Am using grails 2.0.0.RC1.
The bug presents itself when I try to include a model object in a GSP, for example:
/hello/index.gsp
<p>This data is coming from the model:</p>
<p>content: ${content}</p>
<g:include model="${otherModel}" view="/hello/include.gsp" />
Now in my action I have something like:
HelloController.groovy
package helloworld
class HelloController {
def index() {
def model = [:]
model.content = 'content...'
def includeModel = [:]
includeModel.content = 'includeModel...'
model.otherModel = includeModel
render( view:'index', model:model )
}
}
The /hello/include.gsp file contains the following:
/hello/include.gsp
<p>This data is coming from the included model:</p>
<p>content: ${content}</p>
But, what shows up on the page is not what I am expecting, this is what shows on the page:
http://localhost:8080/helloworld/hello/index
This data is coming from the model:
content: content...
This data is coming from the included model:
content: content...
Any ideas? Any help is greatly appreciated.
Thanks,
-Cesar
It might be a bug, but according to the docs, the include tag is specifically designed to include the response of another controller/action or view in the current response -- not just additional GSPs. If you want to "include" another GSP into your page, you really should use the render tag. I have verified that your code works correctly with the render tag and renaming include.gsp to _include.gsp and making the tag be <g:render model="${otherModel}" template="include" />. I got the following output:
This data is coming from the model:
content: content...
Using g:render:
This data is coming from the included model:
content: includeModel...
I also tried adding another action to the controller to return the included content and render include.gsp, and then using the g:include tag to output that in the page and it worked:
def include() {
def includeModel = [:]
includeModel.content = 'includeModel...'
includeModel
}
And then in the index.gsp I added:
<g:include action="include"/>
And I got:
This data is coming from the model:
content: content...
Using g:render:
This data is coming from the included model:
content: includeModel...
Using g:include with action
This data is coming from the included model:
content: includeModel...
Also, you don't have to specify render(view:'viewname, ...) in your controller if the view is the same name as the method in the controller. You can just return the model and it Grails will automatically pick the GSP file with the same name as the controller action.
All that being said, it still seems like what you're trying to do with the include tag should work, and I can't explain why it's not (and the source code for the tag isn't showing up at the bottom of the docs like it's supposed to either). I'd recommend filing a JIRA though if the render tag isn't an option for you.

how to display Flash (swf) content in ASP.NET MVC

In our WebForms apps we serve flash via simple anchor tags like so:
See It
Now, I'm wanting to move that A tag into a call to a Controller/Action using an Html.ActionLink like so:
Html.ActionLink("See It", "DeliverFlash", new {fileName="whatever.swf"})
Then in the controller I'm using a FileStreamResult to push it out....
That "works" in that the flash goes out BUT....
1) It only prompts the user to download the swf I'd like it to just show it like the original implementation does.
2) I have not yet figured out how to pass along those extra parameters of class and params.
Can anyone help please?
Make sure that when you create the FileResult, that you don't set the FileDownloadName property or it will add a Content-Disposition header to specify it as an attachment. See the source code at: http://www.codeplex.com/aspnet. To set the extra parameters, you simply need to use a signature that includes the html options.
<%= Html.ActionLink( "See It", "DeliverFlash",
new { fileName = "whatever.swf" },
new {
#class = "something",
#params = "width, height, yadda, bang"
} ) %>
Note the # in front of class and params as they are C# keywords.

Resources