0

What is the best method to insert a javascript snippet into a template for Golang using the Beego framework?

Currently, I am just adding data to a template:

c.Data["Javascript"] = JavasciptStringObject

And in a script.tpl file adding insertion points:

 var canvas = new fabric.Canvas('c');
 canvas.setHeight(571); //todo: Set to height of image
 canvas.setWidth(991);
 {{.JavaScript}}

The problem is that it escapes the quotation marks from the string, rather than injecting directly in:

var canvas = new fabric.Canvas('c');
canvas.setHeight(571); 
canvas.setWidth(991);
"var path = new fabric.Path('M 617 141 L 606 126M 606 126 L 604 127 z', { stroke: 'red', strokeWidth: 2, fill: false, originX: 'left', originY: 'top',});canvas.add(path); "

Some useful resources include Golang HTML Templating Doc, which apparently Beego models: https://golang.org/pkg/html/template/

And this stack overflow seems to be getting closer, I don't think this will work with Beego: Go lang templates: always quotes a string and removes comments

1 Answer 1

1

The answer was

 import "html/template"

 //within function
 c.Data["Javascript"] = template.JS(JavasciptStringObject)

Note: template.JSEscape did not work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.