duanchuaiwan0063 2016-08-31 16:41
浏览 343

嵌套golang模板中的变量

I want to define variables inside of a golang template, instead of in a binary so that there is no need to recompile.

In Go, I set some vars:

var animals = map[string]string{
    "spirit_animal":    "cat",
    "spirit_predator":  "dog",
}

I execute the template with this: t.ExecuteTemplate(w, "main", variables) which passes these vars to the template.

Now I would like to take these vars from go and into the "main" template.

{{$spirit_animal:="cat"}} {{$spirit_animal}}

And I call sub-templates like this:

{{ template "navbar" . }}

The problem is that nested templates (sub templates) do not appear to have access to any variables.

From the documentation, "A template invocation does not inherit variables from the point of its invocation." Reading the Documentation for "text/template", it sounds like variables may not be able to be used in nested templates.

Any suggestions on how to get these vars out of a binary and into a single text location accessible by nested templates that doesn't need to be recompiled on change?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置