R
R
Roman Rakzin2016-04-16 14:23:34
go
Roman Rakzin, 2016-04-16 14:23:34

How to change characters in template by replacing {{ and }} with - in Golang?

There is a handler

func Handler(w http.ResponseWriter, r *http.Request) {    
    var templates = template.Must(template.ParseGlob("Templates/Main/*/*"))
    var doc bytes.Buffer 
    err := templates.ExecuteTemplate(&doc, "indexPage", nil)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    } 
    //templates.Delims("<%", "%>").Funcs(Main_TemplateFuncs).Parse("<%шаблон   %>")    
    fmt.Fprintln(w, &doc)
}

I need to remap the characters {{ and }} as I use the same characters in angularjs.
There is a function templates.Delims("<%", "%>") that will reassign them, but it works if you put text into Parse after it.
And I have a lot of template.Must(template.ParseGlob("Templates/Main//")) files and how do I process them, replacing the standard processing {{ and }} with <% and %> ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2016-04-16
@TwoRS

template.Must(template.New("").Delims("<%", "%>").ParseGlob("Templates/Main/*/*"))

Plus, you would need to move template parsing out of the handler. Now they are being parsed at every request, it is very slow. It is necessary to parse them once at the start of the application.

O
Oleg Shevelev, 2016-04-16
@mantyr

You can always get into a situation where:

  • Delims cannot be changed (since a lot of code is already with it)
  • When Delims characters are needed only in one place and it would be necessary to display them natively
<script>
    var title = '{{title}}';
    var obj = {{ { }}{{ } }}       // var obj = {}
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question