Is there a way to recursively call the Rythm template based on some condition?
I am currently using Rythm to create some snippets - in conjunction with AST as templates depend on Java files.
Calling templates works as expected. One unexpected behavior. I would like to call the template recursively, but it doesn't seem to be possible.
Foo.html Template
@args String someString, boolean recursion
Calling foo template with recursion: @recursion
@if(recursion) {
A recursion was requested: @recursion
@foo(someString, false)
}
Note , this only provides 1 level of recursion, as is the case for this example. However, I would like to use the condition instead false
in the line @foo(someString, false)
later.
You can try the snippet here by simply copying and pasting the given example into foo.html
.
Error message
The method foo(String, boolean) is undefined for the type Cfoo_html__R_T_C__
Template: /foo.html
Relevant template source lines:
-------------------------------------------------
1: @args String someString, boolean recursion
2:
3: Calling foo template with recursion: @recursion
4: @if(recursion) {
5: A recursion was requested: @recursion
>> 6: @foo(someString, false)
7: }
/*
* Omitted for the sake of readability.
*/
Now the error is not related to recursion. Although, this is the error message I see in Eclipse.
I think when calling a template it is not possible to call it internally because Rythm is only looking for other templates - or so it seems.
Use the link above to access Rythm Fiddle , enter code inside bar.html
instead foo.html
- change line # 6 from @foo(someString, false)
to @bar(someString, false)
.
Now put the following line inside foo.html
:
@bar("foo", true)
This changes the error to:
java.lang.SecurityException: java.util.concurrent.TimeoutException
I think this proves my assumption above, since Rythm is now finding a template (or a method that is). And that's basically where I am stuck.
So the question is, is there a way to recursively call the Rythm template based on some condition?
I'm also open to other suggestions as recursions can usually be handled in a non-recursive way. I just want to avoid code duplication.
source to share
Rythm support @this()
for loading a template in a recursive way. See http://play-rythm-demo.appspot.com/demo/fibonacci
However, it looks like there was an error and now it will raise StackOverflowError
even if an end condition is given. Submit a bug report to https://github.com/rythmengine/rythmengine/issues
Update
StackOverflowError
called by the type boolean
. It works great if a different type of variable is used to control the interruption of the recursive call.
@args String foo, int i
<h1>@foo</h1>
Calling foo template with recursion: @i
@if(i > 1) {
A recursion was requested: @i
@this({foo: foo, recursion: false, i: (i - 1)})
}
Below is a test done on a rhythm script
source to share