简体   繁体   中英

Insert JavaScript Snippet Inside Template with Beego/GoLang

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

The answer was

 import "html/template"

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

Note: template.JSEscape did not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM