开发者

Using the output from g.render in Grails controller

开发者 https://www.devze.com 2023-01-15 07:28 出处:网络
I am rendering two GSP templates in a controller. The first template is rendered as follows: String couponspage = (g.render(template: \"availableCoupons\", contentType:\"text/html\", model : [:]))?.t

I am rendering two GSP templates in a controller. The first template is rendered as follows:

String couponspage = (g.render(template: "availableCoupons", contentType:"text/html", model : [:]))?.toString()

I include the results of the first template in the model to the call to render the second template as follows:

render(template: "itemOffer", model: [merchantOffers: merchantOffers, asinnumbers: params.asinnumbers, linkshare : couponspage])

In the second GSP template, I use jQuery to update the contents of an element using the rendered template contained in the model as follows:

$("#elementid").html("${linkshare}")

Everything works if the rendered-template has all html elements on a single line. Unfortunately, my GSP template like many other html documents have tabs and newline characters to make the file readable by humans. g.render includes these control characters which seems to cause everything to break. A simple file that has Hello World contained in a paragraph element contained in a div element doesn't work.

"\n\t

Hello World

\n"

(Sorry you can't see the paragraph and div elements in the above example.) The documentation suggest that what I am doing is straight forward and should work with no problem. I can't believe I have to strip the control characters from the rendered string or do I. Removing the call to jQuery and adding "${linkshare}" directly to the end of the second template also works. What gives?开发者_JAVA百科

Thanks


I now see that the problem is with javascript. The newline character in a string is not valid. I will escape or strip the character from the string.

Thanks, Ashley

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号