Skip to content

Commit

Permalink
html/template wrapper for fsys
Browse files Browse the repository at this point in the history
  • Loading branch information
adoublef committed Feb 10, 2024
1 parent 3d2192b commit a3bc6f5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 64 deletions.
9 changes: 6 additions & 3 deletions net/http/template/fs.go → html/template/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/fs"
"maps"
"path/filepath"
)

// An FS provides access to a file system that produces a safe HTML document templates.
Expand All @@ -14,11 +15,9 @@ type FS struct {
funcs template.FuncMap
}

var ErrParseTemplate = errors.New("template: parse template files")

// Parse parses the named files and associates the resulting templates with t
func (fsys *FS) Parse(filenames ...string) (Template, error) {
t, err := template.New(filenames[0]).Funcs(fsys.funcs).ParseFS(fsys.fsys, filenames...)
t, err := template.New(filepath.Base(filenames[0])).Funcs(fsys.funcs).ParseFS(fsys.fsys, filenames...)
if err != nil {
return nil, errors.Join(ErrParseTemplate, err)
}
Expand Down Expand Up @@ -51,3 +50,7 @@ type Template interface {
// Execute applies a parsed template to the specified data object, writing the output to wr
Execute(wr io.Writer, data any) error
}

var (
ErrParseTemplate = errors.New("template: parse template files")
)
52 changes: 52 additions & 0 deletions html/template/fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package template_test

import (
"embed"
"html/template"
"strings"
"testing"

"go.adoublef.dev/is"
. "go.adoublef.dev/sdk/html/template"
)

//go:embed all:testdata/*.html
var fsys embed.FS

func TestFS_Parse(t *testing.T) {
t.Run("OK", func(t *testing.T) {
var (
is = is.NewRelaxed(t)
)

tt, err := template.ParseFS(fsys, "testdata/base.html", "testdata/home.html")
is.NoErr(err) // template.ParseFS

var sb strings.Builder
err = tt.Execute(&sb, nil)
is.NoErr(err) // Template.Execute

html := `<a>A</a><b>2</b>
<c>C</c>`
is.Equal(strings.TrimSpace(sb.String()), html)
})

t.Run("OK", func(t *testing.T) {
var (
is = is.NewRelaxed(t)
)

fs := NewFS(fsys)

tt, err := fs.Parse("testdata/base.html", "testdata/home.html")
is.NoErr(err) // FS.Parse

var sb strings.Builder
err = tt.Execute(&sb, nil)
is.NoErr(err) // Template.Execute

html := `<a>A</a><b>2</b>
<c>C</c>`
is.Equal(strings.TrimSpace(sb.String()), html)
})
}
2 changes: 2 additions & 0 deletions html/template/testdata/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<a>{{template "a" .}}</a><b>{{block "b" .}}B{{end}}</b>
<c>{{block "c" .}}C{{end}}</c>
1 change: 1 addition & 0 deletions html/template/testdata/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "a"}}A{{end}}{{define "b"}}2{{end}}
61 changes: 0 additions & 61 deletions io/fs/template/template.go

This file was deleted.

0 comments on commit a3bc6f5

Please sign in to comment.