@@ -114,6 +114,40 @@ func IndexOf(srch interface{}, array interface{}, strictOptional ...bool) int {
114114// DefaultFuncs returns a list of default functions for binding in the template
115115func (t * Template ) DefaultFuncs () []Function {
116116 return []Function {
117+ {
118+ Name : "source" ,
119+ Description : []string {
120+ "Source / evaluate the template at the input location (as URL)." ,
121+ "This will make all of the global variables declared there visible in this template's context." ,
122+ },
123+ Func : func (p string ) (string , error ) {
124+ loc := p
125+ if strings .Index (loc , "str://" ) == - 1 {
126+ buff , err := getURL (t .url , p )
127+ if err != nil {
128+ return "" , err
129+ }
130+ loc = buff
131+ }
132+ sourced , err := NewTemplate (loc , t .options )
133+ if err != nil {
134+ return "" , err
135+ }
136+ // copy the binds in the parent scope into the child
137+ for k , v := range t .binds {
138+ sourced .binds [k ] = v
139+ }
140+ // inherit the functions defined for this template
141+ for k , v := range t .funcs {
142+ sourced .AddFunc (k , v )
143+ }
144+ // set this as the parent of the sourced template so its global can mutate the globals in this
145+ sourced .parent = t
146+
147+ // TODO(chungers) -- let the sourced template define new functions that can be called in the parent.
148+ return sourced .Render (nil )
149+ },
150+ },
117151 {
118152 Name : "include" ,
119153 Description : []string {
@@ -187,7 +221,9 @@ func (t *Template) DefaultFuncs() []Function {
187221 "Global variables are propagated to all templates that are rendered via the 'include' function." ,
188222 },
189223 Func : func (name string , v interface {}) interface {} {
190- t .binds [name ] = v
224+ for here := t ; here != nil ; here = here .parent {
225+ here .updateGlobal (name , v )
226+ }
191227 return ""
192228 },
193229 },
0 commit comments