示例#1
0
func (t *Template) replaceExtendTag(name string, treeMap map[string]*parse.Tree, from string) error {
	var err error
	hasExtend := false
	var loc string
	for _, v := range treeMap {
		templateutil.WalkTree(v, func(n, p parse.Node) {
			if err != nil {
				return
			}
			if templateutil.IsPseudoFunction(n, "extend") {
				if hasExtend {
					loc2, _ := v.ErrorContext(n)
					err = fmt.Errorf("multiple {{ extend }} tags in %q, %s and %s", name, loc, loc2)
					return
				}
				hasExtend = true
				loc, _ = v.ErrorContext(n)
				var repl parse.Node
				if from == "" {
					// empty
					log.Debugf("removing {{ extend }} from %q", name)
					repl = &parse.TextNode{
						NodeType: parse.NodeText,
						Pos:      n.Position(),
					}
				} else {
					log.Debugf("extending %q at %s with %q", name, loc, from)
					repl = templateutil.TemplateNode(from, n.Position())
				}
				err = templateutil.ReplaceNode(n, p, repl)
			}
		})
	}
	return err
}
示例#2
0
文件: app.go 项目: rainycape/gondola
func (app *App) loadContainerTemplate(included *includedApp) (*Template, string, error) {
	container, err := app.loadTemplate(app.templatesFS, app.assetsManager, included.container)
	if err != nil {
		return nil, "", err
	}
	name := template.NamespacedName([]string{included.name}, "~")
	found := false
	var loc string
	for _, v := range container.tmpl.Trees() {
		if err != nil {
			return nil, "", err
		}
		templateutil.WalkTree(v, func(n, p parse.Node) {
			if err != nil {
				return
			}
			if templateutil.IsPseudoFunction(n, "app") {
				if found {
					dloc, _ := v.ErrorContext(n)
					err = fmt.Errorf("duplicate {{ app }} node in container template %q: %s and %s",
						included.container, loc, dloc)
					return
				}
				// Used for error message if duplicate is found
				loc, _ = v.ErrorContext(n)
				found = true
				tmpl := templateutil.TemplateNode(name, n.Position())
				err = templateutil.ReplaceNode(n, p, tmpl)
			}
		})
	}
	if err != nil {
		return nil, "", err
	}
	if !found {
		return nil, "", fmt.Errorf("container template %q does not contain an {{ app }} node", included.container)
	}
	return container, name, nil
}