options.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package ace
  2. import "html/template"
  3. // Defaults
  4. const (
  5. defaultExtension = "ace"
  6. defaultDelimLeft = "{{"
  7. defaultDelimRight = "}}"
  8. defaultAttributeNameClass = "class"
  9. )
  10. // Default NoCloseTagNames
  11. var defaultNoCloseTagNames = []string{
  12. "br",
  13. "hr",
  14. "img",
  15. "input",
  16. "link",
  17. "meta",
  18. }
  19. // Options represents options for the template engine.
  20. type Options struct {
  21. // Extension represents an extension of files.
  22. Extension string
  23. // DelimLeft represents a left delimiter for the html template.
  24. DelimLeft string
  25. // DelimRight represents a right delimiter for the html template.
  26. DelimRight string
  27. // AttributeNameClass is the attribute name for classes.
  28. AttributeNameClass string
  29. // NoCloseTagNames defines a set of tags which should not be closed.
  30. NoCloseTagNames []string
  31. // DynamicReload represents a flag which means whether Ace reloads
  32. // templates dynamically.
  33. // This option should only be true in development.
  34. DynamicReload bool
  35. // BaseDir represents a base directory of the Ace templates.
  36. BaseDir string
  37. // Indent string used for indentation.
  38. Indent string
  39. formatter outputFormatter
  40. // Asset loads and returns the asset for the given name.
  41. // If this function is set, Ace load the template data from
  42. // this function instead of the template files.
  43. Asset func(name string) ([]byte, error)
  44. // FuncMap represents a template.FuncMap which is set to
  45. // the result template.
  46. FuncMap template.FuncMap
  47. }
  48. // InitializeOptions initializes the options.
  49. func InitializeOptions(opts *Options) *Options {
  50. if opts == nil {
  51. opts = &Options{}
  52. }
  53. if opts.Extension == "" {
  54. opts.Extension = defaultExtension
  55. }
  56. if opts.DelimLeft == "" {
  57. opts.DelimLeft = defaultDelimLeft
  58. }
  59. if opts.DelimRight == "" {
  60. opts.DelimRight = defaultDelimRight
  61. }
  62. if opts.AttributeNameClass == "" {
  63. opts.AttributeNameClass = defaultAttributeNameClass
  64. }
  65. if opts.NoCloseTagNames == nil {
  66. opts.NoCloseTagNames = make([]string, len(defaultNoCloseTagNames))
  67. copy(opts.NoCloseTagNames, defaultNoCloseTagNames)
  68. }
  69. if opts.Indent != "" {
  70. opts.formatter = newFormatter(opts.Indent)
  71. }
  72. return opts
  73. }
  74. // AddNoCloseTagName appends name to .NoCloseTagNames set.
  75. func (opts *Options) AddNoCloseTagName(name string) {
  76. opts.NoCloseTagNames = append(opts.NoCloseTagNames, name)
  77. }
  78. // DeleteNoCloseTagName deletes name from .NoCloseTagNames set.
  79. func (opts *Options) DeleteNoCloseTagName(name string) {
  80. var newset []string
  81. for _, n := range opts.NoCloseTagNames {
  82. if n != name {
  83. newset = append(newset, n)
  84. }
  85. }
  86. opts.NoCloseTagNames = newset
  87. }