~samwhited/tmpl

28e2737f84004d59f10925f618cf2df6eba838c1 — Sam Whited 6 months ago a46ed7d master v0.0.6
Fix walking templates
2 files changed, 14 insertions(+), 20 deletions(-)

M tmpl.go
M tmpl_test.go
M tmpl.go => tmpl.go +14 -11
@@ 44,25 44,28 @@ func New(dev bool, vfs fs.FS, c catalog.Catalog, funcMap template.FuncMap) (Temp
		funcMap:  funcMap,
	}

	tmpls, err := fs.ReadDir(vfs, ".")
	if err != nil {
		return t, err
	}
	for _, finfo := range tmpls {
		name := finfo.Name()
	err := fs.WalkDir(vfs, ".", func(p string, d fs.DirEntry, err error) error {
		if p == "." {
			return nil
		}
		if err != nil {
			return err
		}
		name := d.Name()
		// Skip files that aren't templates.
		if path.Ext(name) != ".tmpl" {
			continue
			return nil
		}
		contents, err := fs.ReadFile(vfs, name)
		if err != nil {
			return t, err
			return err
		}

		name = name[:len(name)-5]
		tmpl = template.Must(tmpl.New(name).Parse(string(contents)))
	}
	return t, nil
		t.Template = template.Must(t.Template.New(name).Parse(string(contents)))
		return nil
	})
	return t, err
}

// Execute executes the template, reloading it first if we're in dev mode.

M tmpl_test.go => tmpl_test.go +0 -9
@@ 143,15 143,6 @@ func TestTemplates(t *testing.T) {
					t.Errorf("Wrong output on rerender:\nwant=%q,\ngot=%q", tc.out, s)
				}
			}

			buf.Reset()
			err = tmpls.Execute(buf, tc.data)
			if s := buf.String(); s != "" {
				t.Errorf("Execute should not return any data: %q", s)
			}
			if err == nil {
				t.Errorf("Execute should always error")
			}
		})
	}
}