Golang and AngularJS template conflict

后端 未结 7 1495
闹比i
闹比i 2021-01-07 23:54

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 i

相关标签:
7条回答
  • 2021-01-08 00:12

    Solution that won't require changing delimiters:

    Use {{"{{"}} for opening and {{"}}"}} for closing angular expressions.

    Example:

    {{"{{"}} 1 + 2 {{"}}"}}
    
    0 讨论(0)
  • 2021-01-08 00:19

    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.

    0 讨论(0)
  • 2021-01-08 00:21

    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:

    http://golang.org/pkg/html/template/#Template.Delims

    0 讨论(0)
  • 2021-01-08 00:24

    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

    0 讨论(0)
  • 2021-01-08 00:26

    This worked for me.

    indexTmpl = template.New("index.html").Delims("<<", ">>")
    indexTmpl, _ = indexTmpl.ParseFiles("index.html")
    
    0 讨论(0)
  • 2021-01-08 00:36

    As Yasir said, you should change delimiters. I martini with [[ and ]] delimiters and it works with no problems.

    0 讨论(0)
提交回复
热议问题