branaway / Japid

A Java-based statically-typed fast template engine that can be used in any Java code. It has special adapter for use with the Play! Framework.
113 stars 18 forks source link

Loosing reference of the original StringBuilder in the RenderResultPartial #51

Open BogdanNicolau opened 12 years ago

BogdanNicolau commented 12 years ago

Hi,

Great work on the module. However I have a big problem that's preventing me from upgrading to the latest 0.9.4.3. Inside my controller, I'm making some final checks on the request parameters trying to see if I need to return a jsonp response. If so, I would've simply done something like:

JapidResult jR = new JapidResult(getRenderResultWith(template(), stuffToRender)); StringBuilder res = jR.getRenderResult().getContent(); res.insert(0, "(").insert(0, callback).append(")"); throw jR;

But now, the RenderResultPartial sends back a new StringBuilder upon every access so my method obviously fails. It would be optimal to always return the original StringBuilder, and not just enhance a new one every time getContent() is called.

branaway commented 12 years ago

Hi,

The reason that a new instance of StringBuilder is returned is due to the fact the StringBuilder buried inside of the RenderResultPartial is only part of the text output of the result. It's immutable.

Solutions:

  1. Put the additional checking in the template or in a Java method can be in invoked in the template.

Or,

  1. Compose a new instance of RenderResult using this constructor:
public RenderResult(Map<String, String> headers , StringBuilder content, *

long* renderTime)

and throw a new instance of JapidResult that wraps the above RenderResult.

Something like this:

RenderResult rr = getRenderResultWith(template(), stuffToRender);
StringBuilder res = rr.getContent();
res.insert(0, "(").insert(0, callback).append(")");
throw new JapidResult(new RenderResult(rr.getHeaders(), res, 0));

Hope this helps.

Bing

2012/7/6 BogdanNicolau < reply@reply.github.com

Hi,

Great work on the module. However I have a big problem that's preventing me from upgrading to the latest 0.9.4.3. Inside my controller, I'm making some final checks on the request parameters trying to see if I need to return a jsonp response. If so, I would've simply done something like:

JapidResult jR = new JapidResult(getRenderResultWith(template(), stuffToRender)); StringBuilder res = jR.getRenderResult().getContent(); res.insert(0, "(").insert(0, callback).append(")"); throw jR;

But now, the RenderResultPartial sends back a new StringBuilder upon every access so my method obviously fails. It would be optimal to always return the original StringBuilder, and not just enhance a new one every time getContent() is called.


Reply to this email directly or view it on GitHub: https://github.com/branaway/Japid/issues/51