go - How to return a HTML template from a template.FuncMap? -
i asked question upon https://groups.google.com/forum/#!topic/golang-nuts/cubdahkesjk received no replies.
i'm writing web based media viewer , i've hit snag how mark different media.
my current code here: https://github.com/kaihendry/lk/blob/5de96f9fe012e9894deef7b9924f96dd8d9c806c/main.go#l181 wrong. puzzled how use template here in light of https://golang.org/pkg/html/template/#template.execute
all examples see of using template.funcmap use strings...
reduced example: https://play.golang.org/p/maue8sddw6
ideally use html templates here , not fmt.sprintf. yes, realise not html escaping filename wrong, not sure how use html templates again function.
thanks in advance guidance,
you should separate logic (function) presentation (template). function registered in template.funcmap
should not depends on template input produce output. if want return html template function output, should generate manually (using fmt.sprintf
, etc.).
in case, can register function check media type, generate different output using template's {{if}}
action. function may looks like:
func matchtype(ext, s string) bool { return strings.tolower(ext) == strings.tolower(path.ext(s)) }
and template looks like:
{{ range .media }}<p> {{if . | matchtype ".jpg"}}<img src={{.}}> {{else if . | matchtype ".mp4"}}<video controls src={{.}}></video> {{else}}{{.}} {{end}}</p> {{ end }}
a working example based on reduced example: https://play.golang.org/p/u64_7uhzqu
Comments
Post a Comment