Skip to content

Commit

Permalink
fix: add script template support to scripts, fixes #754
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed May 22, 2024
1 parent c1e734b commit 1ecd566
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.700
0.2.702
10 changes: 7 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ func (g *generator) writeElement(indentLevel int, n parser.Element) (err error)
return err
}
// <script type="text/javascript"></script>
if err = g.writeElementScript(indentLevel, n); err != nil {
if err = g.writeElementScript(indentLevel, n.Attributes); err != nil {
return err
}
// <div
Expand Down Expand Up @@ -1000,9 +1000,9 @@ func isScriptAttribute(name string) bool {
return false
}

func (g *generator) writeElementScript(indentLevel int, n parser.Element) (err error) {
func (g *generator) writeElementScript(indentLevel int, attrs []parser.Attribute) (err error) {
var scriptExpressions []string
for _, attr := range n.Attributes {
for _, attr := range attrs {
scriptExpressions = append(scriptExpressions, getAttributeScripts(attr)...)
}
if len(scriptExpressions) == 0 {
Expand Down Expand Up @@ -1275,6 +1275,10 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e
return err
}
} else {
// <script type="text/javascript"></script>
if err = g.writeElementScript(indentLevel, n.Attributes); err != nil {
return err
}
// <div
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(`<%s`, html.EscapeString(n.Name))); err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions generator/test-script-usage/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@
}
</script>
<input type="button" value="Click me" onclick="__templ_conditionalScript_de41()" />
<script type="text/javascript">
function __templ_alertTest_eadf(){alert('testing');
}
</script>
<script async crossorigin="true" onload="__templ_alertTest_eadf()" src="url.to.some.script"></script>
9 changes: 9 additions & 0 deletions generator/test-script-usage/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ templ ThreeButtons() {
<button hx-on::click="alert('clicked inline')" type="button">Button D</button>
<button hx-on::click={ onClick() } type="button">Button E</button>
@Conditional(true)
@ScriptOnLoad()
}

script conditionalScript() {
Expand All @@ -42,3 +43,11 @@ templ Conditional(show bool) {
}
/>
}

script alertTest() {
alert('testing');
}

templ ScriptOnLoad() {
<script async crossorigin="true" onload={ alertTest() } src="url.to.some.script"></script>
}
51 changes: 51 additions & 0 deletions generator/test-script-usage/template_templ.go

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

0 comments on commit 1ecd566

Please sign in to comment.