Can you save rendered template output instead of sending it to the browser?

54 views
Skip to first unread message

James

unread,
Aug 28, 2010, 9:06:02 PM8/28/10
to play-framework
Is there any way to manually render a template and save the result?

I would like to be able to use the template engine to render a
template file, but I want to save that result rather than send it as a
response. In other words I'd like to be able to manually initiate a
template render and get the result back as a string.

Something like this ...

<code>
String textResult = renderTemplate("/path/to/template.html",
myVar1, myVar2, ...);
</code>

Thanks,

- James

GrailsDeveloper

unread,
Aug 29, 2010, 5:13:32 AM8/29/10
to play-framework
Based on http://www.playframework.org/@api/play/templates/Template.html
I would guess, that you can create a new Template and use the render-
method.
Unfortunately this isn't well documented. But perhaps you can try it
and give feedback.
Niels

Guillaume Bort

unread,
Aug 29, 2010, 1:34:49 PM8/29/10
to play-fr...@googlegroups.com
Yes, just use:

TemplateLoader.load("Application/index.html")

and you get a Template instance that you can render yourself.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

green

unread,
Aug 30, 2010, 8:34:38 PM8/30/10
to play-fr...@googlegroups.com
Here is the code i am using to compose a customized xml based protocol message (yes, using play template!):

public class Composer {
    public static String render(String template, Scope.RenderArgs binding) {
       
        binding.put("sessionId", System.currentTimeMillis());

        try {
            String path = String.format("ckmp/%1$s.xml", template);
            Template t = TemplateLoader.load(path);
            return t.render(binding.data);
        } catch (TemplateNotFoundException ex) {
            if (ex.isSourceAvailable()) {
                throw ex;
            }
            StackTraceElement element = PlayException
                    .getInterestingStrackTraceElement(ex);
            if (element != null) {
                throw new TemplateNotFoundException(template, Play.classes
                        .getApplicationClass(element.getClassName()), element
                        .getLineNumber());
            } else {
                throw ex;
            }
        }
    }
}

And the client code to call that method:
        RenderArgs b = new Scope.RenderArgs();
        b.put("mp", mp);
        return Composer.render(this.tmpl_, b);
Reply all
Reply to author
Forward
0 new messages