Skip to content

Commit

Permalink
Added nesting for loops with name collisions.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://spacelabstudio.com/home/spacelab/svn/jpt/trunk@6 0f714a27-ac0e-0410-86d0-b7c3a97c1470
  • Loading branch information
Chris Rossi committed Apr 10, 2008
1 parent 7c2117f commit 2c73648
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/com/christophermrossi/jpt/Loop.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Loop {
Iterator iterator;
int index = -1;
int length = -1;
Loop loopParent = null;

Loop( String expression, Interpreter beanShell ) throws PageTemplateException {
if ( expression == null ) {
Expand Down Expand Up @@ -90,7 +91,7 @@ void addToRepeat( Interpreter beanShell ) throws PageTemplateException {
repeat = new TreeMap();
beanShell.set( "repeat", repeat );
}
repeat.put( this.variableName, this );
this.loopParent = (Loop)repeat.put( this.variableName, this );
} catch( bsh.EvalError e ) {
throw new PageTemplateException(e);
}
Expand All @@ -99,7 +100,13 @@ void addToRepeat( Interpreter beanShell ) throws PageTemplateException {
void removeFromRepeat( Interpreter beanShell ) throws PageTemplateException {
try {
Map repeat = (Map)beanShell.get( "repeat" );
repeat.remove( this.variableName );
if ( this.loopParent == null ) {
repeat.remove( this.variableName );
}
else {
repeat.put( this.variableName, this.loopParent );
this.loopParent = null;
}
if ( repeat.size() == 0 ) {
beanShell.unset( "repeat" );
}
Expand Down
87 changes: 85 additions & 2 deletions test/com/christophermrossi/jpt/statements.html
Original file line number Diff line number Diff line change
Expand Up @@ -1766,9 +1766,92 @@ <h1>This is a test</h1>

<p></p>

<p>
Let's try a nested loop using the same loop variable name.
Apparently if you use recursive macros, something I've never
done, you will need for this to work. Use case courtesy of
James Davies.
<tal:block xmlns:tal="http://xml.zope.org/namespaces/tal">

<div>4</div>

<ul>

<div>

<li>1</li>

</div>
<div>

<li>2</li>

</div>
<div>

<li>3</li>

</div>
<div>

<li>4</li>

</div>

</ul>
</tal:block>
<tal:block xmlns:tal="http://xml.zope.org/namespaces/tal">

<div>4</div>

<ul>

<div>

<li>5</li>

</div>
<div>

<li>6</li>

</div>
<div>

<li>7</li>

</div>
<div>

<li>8</li>

</div>

</ul>
</tal:block>
<tal:block xmlns:tal="http://xml.zope.org/namespaces/tal">

<div>2</div>

<ul>

<div>

<li>9</li>

</div>
<div>

<li>10</li>

</div>

</ul>
</tal:block>

</p>

<p>4</p>



</body>
</html>
14 changes: 14 additions & 0 deletions test/com/christophermrossi/jpt/statements.jpt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@
properly.
</p>
</p>
<p>
Let's try a nested loop using the same loop variable name.
Apparently if you use recursive macros, something I've never
done, you will need for this to work. Use case courtesy of
James Davies.
<tal:block tal:repeat="n here/table">
<div tal:content="n.length"/>
<ul>
<div tal:repeat="n n">
<li tal:content="n">n</li>
</div>
</ul>
</tal:block>
</p>
<p tal:evaluate="java: joemama = 1 + 3"
tal:content="joemama">Joe Mama</p>
</body>
Expand Down

0 comments on commit 2c73648

Please sign in to comment.