英文:
Golang and AngularJS template conflict
问题
我与golang的html/template和angularjs分隔符之间存在冲突。我知道Go有一种方法可以更改分隔符,但对我来说不起作用。也许应该在解析文件之前调用该方法?你能否提供一个示例,说明应该如何实现?
我发现很多评论说AngularJS和Go不应该有任何冲突,因为它们应该分别使用。据我了解,Go应该仅用于后端(REST)。问题是,AngularJS和HTML应该如何加载?在这种情况下,我是否需要两个服务器?
英文:
I have a conflict with golang html/template and angularjs delimiters. I know that there is a method that lets to change delimiter in Go, but it does not work for me. Maybe it should be called before parsing of files? Could you please provide an example, how it should be implemented.
I found a lot of comments that AngularJS and Go should not have any conflict because they should be used separately. As I understand, Go should be used only for backend (REST). The question is, how AngularJS, HTML should be loaded? In this case, should I have to servers?
答案1
得分: 32
你需要更改使用的分隔符字符,就是这样。我之前在Beego上遇到过相同的问题,因为模板字符{{ }}
完全相同,一旦我将其更改为<<<>>>
,我就成功地无问题地提供了HTML。
在进行任何解析之前,你基本上需要调用这个函数:func (t *Template) Delims(left, right string) *Template
更多详细信息请参考:
英文:
you need to change the delimiters characters used that's all. I've had the same problem before on Beego because of the exact templating characters {{ }}
and once I changed that to <<<>>>
I managed to serve the html without any problems.
You basically need to call this function before you do any parsing: func (t *Template) Delims(left, right string) *Template
for more details:
答案2
得分: 16
AngularJS的理念是将所有的渲染过程都在客户端上运行。
如果你一直按照这个逻辑进行,那么在提供AngularJS的HTML文件时,就不需要在服务器端进行任何渲染(例如:不需要调用template.Execute
),你只需要提供静态内容即可。
英文:
The idea behind AngularJS is to have all the rendering process run on the client side.
If you follow this logic all along, you do not need any rendering on the server side (e.g : no call to template.Execute
) when serving AngularJS html files, you simply have to serve static content.
答案3
得分: 8
这对我有效。
indexTmpl = template.New("index.html").Delims("<<", ">>")
indexTmpl, _ = indexTmpl.ParseFiles("index.html")
英文:
This worked for me.
indexTmpl = template.New("index.html").Delims("<<", ">>")
indexTmpl, _ = indexTmpl.ParseFiles("index.html")
答案4
得分: 8
不需要更改分隔符的解决方案:
使用{{ "{{" }}
作为开头和{{ "}}" }}
作为结尾的Angular表达式。
示例:
{{ "{{" }} 1 + 2 {{ "}}" }}
英文:
Solution that won't require changing delimiters:
Use {{"{{"}}
for opening and {{"}}"}}
for closing angular expressions.
Example:
{{"{{"}} 1 + 2 {{"}}"}}
答案5
得分: 5
这是一个完整的示例,演示了如何使用Golang HTML模板 + Angular.js,并按照其他人提到的设置分隔符。这个示例之所以重要,是因为它展示了端到端的体验,包括设置静态资源的服务、模板解析以及Angular.js的集成。
英文:
Here is a full example of using Golang HTML templates + Angular.js by setting delimiters as mentioned by others. The reason for this example is that it shows an end to end experience, setting up the serving of static assets, template parsing and of course angular.js integration.
答案6
得分: 2
类似Bijan的回答,但更易读。您可以在模板中使用"printf"来打印字符串字面量。对于 "html/template" 包,这个方法同样适用。
例如:{{ printf "{{}}"}}
。https://golang.org/pkg/text/template/#pkg-overview
英文:
Similar to Bijans answer but a bit more readable. You can use "printf" in your template to print a string literal. This works just the same for the "html/template" package.
For example: {{ printf "{{}}"}}
. https://golang.org/pkg/text/template/#pkg-overview
答案7
得分: 1
正如Yasir所说,你应该更改分隔符。我使用[[和]]作为分隔符,它可以正常工作。
英文:
As Yasir said, you should change delimiters. I martini with [[ and ]] delimiters and it works with no problems.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论