helper_method_include.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package ace
  2. import (
  3. "fmt"
  4. "io"
  5. "strings"
  6. )
  7. // helperMethodInclude represents a helper method include.
  8. type helperMethodInclude struct {
  9. elementBase
  10. templateName string
  11. pipeline string
  12. }
  13. // WriteTo writes data to w.
  14. func (e *helperMethodInclude) WriteTo(w io.Writer) (int64, error) {
  15. var s string
  16. if e.pipeline == "" {
  17. s = fmt.Sprintf(actionTemplate, e.opts.DelimLeft, e.templateName, e.opts.DelimRight)
  18. } else {
  19. s = fmt.Sprintf(actionTemplateWithPipeline, e.opts.DelimLeft, e.templateName, e.pipeline, e.opts.DelimRight)
  20. }
  21. i, err := w.Write([]byte(s))
  22. return int64(i), err
  23. }
  24. // newHelperMethodInclude creates and returns a helper method include.
  25. func newHelperMethodInclude(ln *line, rslt *result, src *source, parent element, opts *Options) (*helperMethodInclude, error) {
  26. if len(ln.tokens) < 3 {
  27. return nil, fmt.Errorf("no template name is specified [file: %s][line: %d]", ln.fileName(), ln.no)
  28. }
  29. var pipeline string
  30. if len(ln.tokens) > 3 {
  31. pipeline = strings.Join(ln.tokens[3:], space)
  32. }
  33. e := &helperMethodInclude{
  34. elementBase: newElementBase(ln, rslt, src, parent, opts),
  35. templateName: ln.tokens[2],
  36. pipeline: pipeline,
  37. }
  38. return e, nil
  39. }