RockOnGom
RockOnGom

Reputation: 3961

Golang accessing settings from template

I have a golang file which contains all global variables, such as static file path, version id etc. I need to use it in templates but without passing a context when rendering template. Here is a demonstrate:

settings.go

const STATIC_FILE="/static/"
const VERSION = 1

example.html

<script src="{{.STATIC_FILE}}assets/plugins/angular/angular-checklist.js?v={{.VERSION}}" type="text/javascript"></script>

Note: I am looking a different way, not passing a context to template execute method.

Upvotes: 0

Views: 248

Answers (1)

Tractatus
Tractatus

Reputation: 108

I don't know can you access directly to a global variable, but I have been using template functions to do that.

var func_map = template.FuncMap{
    "getSettings" : func() {
        return Settings{}
    }
}
temp,err := template.New(template_name).Funcs(funcMap).Delims("{[", "]}").Parse(string(dat))

Upvotes: 1

Related Questions