使用Beego / GoLang在模板内插入JavaScript代码段
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
使用Beego框架将JavaScript代码段插入Golang模板的最佳方法是什么? p>
当前,我只是向模板中添加数据: p>
c.Data [“ Javascript”] = JavasciptStringObject
code> pre>
并在script.tpl文件中添加插入点: p>
var canvas = new fabric.Canvas('c');
canvas .setHeight(571); // todo:设置为图片的高度
canvas.setWidth(991);
{{.JavaScript}}
code> pre>
问题是它逃脱了 p>
var canvas = new fabric.Canvas('c');
canvas.setHeight(571);而不是直接在字符串中加上引号。
canvas.setWidth(991);
“ var path = new fabric.Path('M 617141 L 606 126M 606 126 L 604127z',{stroke:'red',strokeWidth:2,fill:false,originX :'left',originY:'top',}); canvas.add(path);“
code> pre>
一些有用的资源包括Golang HTML模板文档,显然 Beego模型: https://golang.org/pkg/html/template/ p>
这个堆栈溢出似乎越来越近了,我认为这不适用于Beego:转到lang模板:始终引用字符串并删除注释 p>
div>
The answer was
import "html/template"
//within function
c.Data["Javascript"] = template.JS(JavasciptStringObject)
Note: template.JSEscape did not work.