Skip to content

Commit

Permalink
bug: improve go exp output (#18)
Browse files Browse the repository at this point in the history
Closes #16

No more need for importing fmt for non-string values
  • Loading branch information
gamebox authored Jan 8, 2024
1 parent 9c15a07 commit 0b6bad5
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 16 deletions.
Binary file added htmx-example/htmx-example
Binary file not shown.
4 changes: 1 addition & 3 deletions htmx-example/templates/counter.html.gwirl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@(count int)

@import "fmt"

<p id="counter" class="count">@fmt.Sprintf("%d", count)</p>
<p id="counter" class="count">@count</p>
3 changes: 1 addition & 2 deletions htmx-example/templates/mainSection.html.gwirl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@(todos []todo.Todo, filter todo.Filter, completedRemain bool)

@import "fmt"
@import "github.com/gamebox/gwirl/htmx-example/todo"

<section class="main">
Expand All @@ -9,7 +8,7 @@
@TodoList(todos)
</section>
<footer class="footer" hx-get="/todos" hx-target="#todolist" hx-swap="outerHTML" hx-trigger="filter-updated">
<span class="todo-count"><strong id="todoCount">@fmt.Sprintf("%d", len(todos))</strong> item left</span>
<span class="todo-count"><strong id="todoCount">@len(todos)</strong> item left</span>
@Filters(filter)
@if completedRemain {<button class="clear-completed" hx-delete="/completed" hx-target="#todolist" hx-swap="outerHTML" hx-trigger="click">Clear completed</button>}
</footer>
12 changes: 5 additions & 7 deletions htmx-example/templates/todoItem.html.gwirl
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
@(todo todo.Todo)

@import "fmt"
@import "strings"
@import "github.com/gamebox/gwirl/htmx-example/todo"

@{
idString := fmt.Sprintf("%d", todo.Id)
sb := strings.Builder{}
if todo.Completed { sb.WriteString("completed") }
if todo.Editing { sb.WriteString("editing") }
itemClasses := sb.String()
}

<li id="todo-@idString" hx-target="#todo-@idString" hx-swap="outerHTML" class="@itemClasses">
<li id="todo-@todo.Id" hx-target="#todo-@todo.Id" hx-swap="outerHTML" class="@itemClasses">
@if !todo.Editing {<div class="view">
<input class="toggle"
type="checkbox"
@if todo.Completed {checked}
hx-patch="/todo?action=complete&id=@idString">
<label hx-patch="/todo?action=edit&id=@idString" hx-trigger="dblclick">@todo.Text</label>
hx-patch="/todo?action=complete&id=@todo.Id">
<label hx-patch="/todo?action=edit&id=@todo.Id" hx-trigger="dblclick">@todo.Text</label>
<button class="destroy"
hx-delete="/todo?id=@idString"
hx-delete="/todo?id=@todo.Id"
hx-swap="delete"></button>
</div>} @else {
<input class="edit"
name="text"
value="@todo.Text"
hx-trigger="blur changed, keyup[key == 'Enter'] changed"
hx-patch="/todo?action=update&id=@idString">}
hx-patch="/todo?action=update&id=@todo.Id">}
</li>
4 changes: 2 additions & 2 deletions internal/gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (G *Generator) GenTemplateTree(tree parser.TemplateTree2) error {
fmt.Printf("GoExp %v\n", tree)
G.write("gwirl.WriteEscapedHTML(&sb_, ")
} else {
G.write("sb_.WriteString(")
G.write("gwirl.WriteRawHTML(&sb_, ")
}
transclusionParamsStr := transclusionParams.String()
if strings.HasSuffix(tree.Text, "()") {
Expand All @@ -171,7 +171,7 @@ func (G *Generator) GenTemplateTree(tree parser.TemplateTree2) error {
if tree.Metadata.Has(parser.TTMDEscape) {
G.write("gwirl.WriteEscapedHTML(&sb_, ")
} else {
G.write("sb_.WriteString(")
G.write("gwirl.WriteRawHTML(&sb_, ")
}
G.writeNoIndent(tree.Text)
G.writeNoIndent(")")
Expand Down
4 changes: 2 additions & 2 deletions internal/gen/testdata/testAll_gwirl.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestAll(name string, index int) string {
sb_.WriteString(`
<h2>`)

sb_.WriteString(name)
gwirl.WriteRawHTML(&sb_, name)

sb_.WriteString(`</h2>
`)
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestAll(name string, index int) string {

transclusion__20__5__1 = sb_.String()
}
sb_.WriteString(Card("title", transclusion__20__5__0, transclusion__20__5__1))
gwirl.WriteRawHTML(&sb_, Card("title", transclusion__20__5__0, transclusion__20__5__1))


sb_.WriteString(`
Expand Down

0 comments on commit 0b6bad5

Please sign in to comment.