salix/include_tag.go

39 lines
692 B
Go
Raw Normal View History

package salix
import (
"errors"
"go.elara.ws/salix/internal/ast"
)
var (
ErrIncludeInvalidArgs = errors.New("include expects one string argument")
ErrNoSuchTemplate = errors.New("no such template")
)
2023-10-30 02:56:15 +00:00
// includeTag represents an #include tag within a Salix template
type includeTag struct{}
func (it includeTag) Run(tc *TagContext, block, args []ast.Node) error {
if len(args) != 1 {
return ErrIncludeInvalidArgs
}
val, err := tc.GetValue(args[0], nil)
if err != nil {
return err
}
name, ok := val.(string)
if !ok {
return ErrIncludeInvalidArgs
}
tmpl, ok := tc.t.ns.GetTemplate(name)
if !ok {
return ErrNoSuchTemplate
}
return tc.Execute(tmpl.ast, nil)
}