Skip to content

Commit

Permalink
v0.1.1 fix deeply nested template names
Browse files Browse the repository at this point in the history
- add a test for deeply nested template
- if there is a go syntax error - render the unformatted go code - for debugging
  • Loading branch information
fritzkeyzer committed Nov 6, 2024
1 parent 12bb61c commit 1396b66
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 8 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# GoHTML
Generate type-safe(ish) wrapper code for Go HTML templates.

[![Version](https://img.shields.io/badge/version-v0.1.0-blue.svg)](https://github.com/fritzkeyzer/gohtml/tags)
[![Version](https://img.shields.io/badge/version-v0.1.1-blue.svg)](https://github.com/fritzkeyzer/gohtml/tags)

> Detailed documentation available in the `docs` directory
Expand All @@ -23,14 +23,14 @@ Generate type-safe(ish) wrapper code for Go HTML templates.

## Installation
```sh
go install github.com/fritzkeyzer/gohtml/cmd/[email protected].0
go install github.com/fritzkeyzer/gohtml/cmd/[email protected].1
```

## Quick start

1. Create `gohtml.yaml`:
```yaml
version: "0.1.0"
version: "0.1.1"
directories:
- path: "app/pages"
- path: "app/components"
Expand Down Expand Up @@ -78,6 +78,9 @@ Issues and PRs welcome!

# Changelog

### v0.1.1
- Fix deeply nested template directories causing bad generation

### v0.1.0
- Fix generation for conditionals with operators (not, eq, etc)
- Define multiple template components per file (sub-templates can be reused within the same package)
Expand Down
4 changes: 2 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ You can specify a config file, if needed, with the -c flag.
### Structure
Example with one target directory.
```yaml
version: "0.1.0"
version: "0.1.1"
directories:
- path: "path/to/templates/dir"
output_file: "gohtml.gen.go" # default value
Expand All @@ -16,7 +16,7 @@ directories:
### Example
This example targets 4 directories. All gohtml templates in these directories will be used to generate code.
```yaml
version: "0.1.0"
version: "0.1.1"
directories:
- path: "app/page"
- path: "app/partial"
Expand Down
2 changes: 1 addition & 1 deletion example/gohtml.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "0.1.0"
version: "0.1.1"
directories:
- path: "views"

5 changes: 4 additions & 1 deletion field.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func parseNode(path []string, node parse.Node) []Field {
case *parse.ContinueNode: // A continue action.
}

logz.Debug("parsed node", "path", path, "fields", fields, "node", node.String())
if len(fields) > 0 {
logz.Debug("parsed node", "path", path, "fields", fields, "node", node.String())
}

return fields
}

Expand Down
3 changes: 3 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func (t *GoHTML) Generate(w io.Writer) error {
// apply std go formatting
formatted, err := format.Source(buf.Bytes())
if err != nil {
// write the unformatted file (so we can inspect the error)
_, _ = w.Write(buf.Bytes())

return fmt.Errorf("go syntax error: %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion gohtml.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func ParseDir(dir string) (*GoHTML, error) {
})

for _, tmpl := range orderedTemplates {
if tmpl.Name() == path.Base(dir) {
// skip the package template
if path.Base(tmpl.Name()) == path.Base(dir) {
continue
}

Expand Down
4 changes: 4 additions & 0 deletions gohtml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func TestDirectory(t *testing.T) {
dir: "tests",
goldenFile: "tests/gohtml.gen.go.golden",
},
{
dir: "tests/nested/views",
goldenFile: "tests/nested/views/gohtml.gen.go.golden",
},
}
for _, tt := range tests {
t.Run(tt.dir, func(t *testing.T) {
Expand Down
65 changes: 65 additions & 0 deletions tests/nested/views/gohtml.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions tests/nested/views/gohtml.gen.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Code generated by gohtml. DO NOT EDIT

package views

import (
"bytes"
"embed"
"html/template"
"io"
"net/http"
"os"
)

var LiveReload = os.Getenv("GOHTML_LIVERELOAD") != ""

//go:embed *.gohtml
var tmplFiles embed.FS
var templates = template.Must(template.ParseFS(tmplFiles, "*.gohtml"))

func tmpl() *template.Template {
if LiveReload {
return template.Must(template.ParseFS(os.DirFS("tests/nested/views"), "*.gohtml"))
}
return templates
}

func mustHTML[T any](fn func(w io.Writer, data T) error, data T) template.HTML {
w := new(bytes.Buffer)
err := fn(w, data)
if err != nil {
panic(err)
}
return template.HTML(w.String())
}

func mustHTMLNoArgs(fn func(w io.Writer) error) template.HTML {
w := new(bytes.Buffer)
err := fn(w)
if err != nil {
panic(err)
}
return template.HTML(w.String())
}

// BEGIN: Nested - - - - - - - -

type NestedData struct {
Name any
}

// Nested renders the "Nested" template as an HTML fragment
func Nested(data NestedData) template.HTML {
return mustHTML(RenderNested, data)
}

// RenderNested renders the "Nested" template to a writer
func RenderNested(w io.Writer, data NestedData) error {
return tmpl().ExecuteTemplate(w, "nested.gohtml", data)
}

// RenderNestedHTTP renders nested.gohtml to an http.ResponseWriter
func RenderNestedHTTP(w http.ResponseWriter, data NestedData) error {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
return tmpl().ExecuteTemplate(w, "nested.gohtml", data)
}
1 change: 1 addition & 0 deletions tests/nested/views/nested.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Nested: {{.Name}}</h1>

0 comments on commit 1396b66

Please sign in to comment.