2023-10-29 22:47:47 +00:00
|
|
|
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
|
2023-10-29 22:47:47 +00:00
|
|
|
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)
|
|
|
|
}
|