Skip to content

Commit

Permalink
codegen: fix interaction between ^for and ^partial (#92)
Browse files Browse the repository at this point in the history
When a ^for loop is used in the main page, it was incorrectly being generated even when the route was to the partial.
  • Loading branch information
paulsmith authored Jan 17, 2023
1 parent 433d5d1 commit 348a6c0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2022 Paul Smith. All rights reserved.
Copyright (c) Paul Smith. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1514,9 +1514,11 @@ func (g *pageCodeGen) genNodePartial(n node, p *partial) {
g.bodyPrintf("printEscaped(%s, %s)\n", g.ioWriterVar, n.expr)
}
case *nodeFor:
g.bodyPrintf("for %s {\n", n.clause.code)
f(n.block)
g.bodyPrintf("}\n")
if state == stateInPartialScope {
g.bodyPrintf("for %s {\n", n.clause.code)
f(n.block)
g.bodyPrintf("}\n")
}
return false
case *nodeIf:
g.bodyPrintf("if %s {\n", n.cond.expr)
Expand Down
11 changes: 11 additions & 0 deletions testdata/partial_for.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


<p>0</p>
<p>1</p>
<p>2</p>

<div>

0
1
</div>
11 changes: 11 additions & 0 deletions testdata/partial_for.up
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
^layout !
^for i := 0; i < 3; i++ {
<p>^i</p>
}
^partial p {
<div>
^for ii := 0; ii < 2; ii++ {
<text>^ii</text>
}
</div>
}
1 change: 1 addition & 0 deletions testdata/partial_for/0.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requestPath=/testdata/partial_for/p
6 changes: 6 additions & 0 deletions testdata/partial_for/0.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

<div>

0
1
</div>

0 comments on commit 348a6c0

Please sign in to comment.