I’m trying to pass a variable into an included template, and can’t seem to get it to work. I am new to go-lang, but not programming. My setup:
page.html:
{{$foo := "bar"}
{{.Include "base.html"}}
base.html:
foo is: <b>{{???}}</b>
I’ve tried {$.foo} (See error below) and other variations, but nothing seems to work:
14/Nov/2016:21:20:05 -0800 [ERROR 500 /page.html] template: page.html:2:2: executing "page.html" at <.Include>: error calling Include: template: base.html:1:13: executing "base.html" at <$.foo>: can't evaluate field foo in type httpserver.Context
Is there anything specific I need to do to pass context into the included template? Reading through the code in context.go it sure seems to be passed properly. Any advice would be appreciated.
I guess I was making some assuming that the c Context passed in ContextInclude contained the scope of the parent template (Hugo uses context to pass variables into subtemplates).
I’m trying to replace SSIs with Caddy templates, and the ability to set variables globally allows for template re-use (eg, for the page title). Is there a way to do that with Caddy’s go templating?
It does, but that context is not the same thing as the interpreter environment of the template. So when you declare $foo in your template, that has no effect on the context. That’s something stored in the interpreter – and actually I don’t know how to get it to transfer over to included templates. Anyone is welcome to look into this. Could come in handy.