Injecting javascript in html templates results in double quoted expressions

1,301 views
Skip to first unread message

larry104

unread,
Sep 28, 2012, 1:01:46 PM9/28/12
to golan...@googlegroups.com
Hi,
I try to inject a bunch of JS expressions into a html template. I mark the places where the expressions need to go within the template using {{.Statment1}}, ... and I'm passing the data (struc of statements) to the t.Execute(w, data). Unfortunately, this will result in double quotes around all my JS expressions. What I do now to get around that is defining my entire JS code on the go side with buffer := bytes.NewBufferString("") and pass it to the Execute as t.Execute(w, template.JS(buffer.String()) - this is of cause not very elegant - and makes the js code not really readable anymore. Is there another way to get around the double quotes in the original approach?
Thanks.

chris dollin

unread,
Sep 28, 2012, 1:25:28 PM9/28/12
to larry104, golan...@googlegroups.com
On 28 September 2012 18:01, larry104 <larg.w...@gmail.com> wrote:
> Hi,
> I try to inject a bunch of JS expressions into a html template. I mark the
> places where the expressions need to go within the template using
> {{.Statment1}}, ... and I'm passing the data (struc of statements) to the
> t.Execute(w, data). Unfortunately, this will result in double quotes around
> all my JS expressions.

Maybe you need to surround your {{.Statementies}} with <script> ... </script>.

Remember html/template has machinery for quoting according to
context.

Chris

--
Chris "allusive" Dollin

larry104

unread,
Sep 28, 2012, 1:29:22 PM9/28/12
to golan...@googlegroups.com
Hm, that's kind of difficult - I like to do something like

var map = L.map('map', {attributionControl: false, center: [{{.X}}, {{.Y}}], zoom: 0, layers ...

where I replace within a JS expression. So, I'm already in a JS context and I would expect that I get unquoted strings.

Larry Clapp

unread,
Sep 28, 2012, 1:48:08 PM9/28/12
to golan...@googlegroups.com
On Friday, September 28, 2012 1:01:46 PM UTC-4, larry104 wrote:
I try to inject a bunch of JS expressions into a html template. I mark the places where the expressions need to go within the template using {{.Statment1}}, ... and I'm passing the data (struc of statements) to the t.Execute(w, data). Unfortunately, this will result in double quotes around all my JS expressions. What I do now to get around that is defining my entire JS code on the go side with buffer := bytes.NewBufferString("") and pass it to the Execute as t.Execute(w, template.JS(buffer.String()) - this is of cause not very elegant - and makes the js code not really readable anymore. Is there another way to get around the double quotes in the original approach?

See the section in the html/template docs on "Typed Strings".


-- Some Other Larry

Message has been deleted

larry104

unread,
Sep 28, 2012, 3:27:29 PM9/28/12
to golan...@googlegroups.com
Doh - It did not occur to me that template.JS was a type - Now it works by defining something like

struct JSData {
 Statement1 template.JS
 Statement2 template.JS
 ...
}

and then
jsData := new(JSData)
jsData.Statment1 = template.JS(fmt.Sprintf("var transformation = new L.Transformation(1, 0, -1, %v);\n",y))
...

t.Execute(w, jsData)

Thanks a lot for the help.

Reply all
Reply to author
Forward
0 new messages