mcuenez
mcuenez

Reputation: 1619

Is there any way to recursively invoke a Rythm template based on some condition?

I'm currently using Rythm to create some snippets - in combination with AST, as the templates depend on Java files.

Invoking templates works as expected. With one unexpected behaviour. I'd like to invoke a template recursively, but it seems this is not 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 provides only 1 level of recursion, as per intention for this example. However, I'd like to use a condition instead of false within the @foo(someString, false) line later on.

You can try the snippet here by simply copying & pasting the provided 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 seems not recursion related. Though, it is the error message I'm seeing in Eclipse.

I guess, when invoking a template it is not possible to invoke it within itself, because Rythm only looks for other templates - or so it seems.

Use the link above to access Rythm Fiddle, put the code inside bar.html instead of foo.html - change line #6 from @foo(someString, false) to @bar(someString, false).

Now, put the following line inside of foo.html:

@bar("foo", true)

When doing so, the error changes to:

java.lang.SecurityException: java.util.concurrent.TimeoutException

I think this proves my assumption above, as Rythm now seems to find the template (or method, that is). And this is basically where I'm stuck.

So, the question is: is there any way to recursively invoke a Rythm template based on some condition?

I'm also open for other suggestions, as recursions usually can be handled in a non-recursive way. I just want to avoid duplicate code.

Upvotes: 3

Views: 93

Answers (1)

Gelin Luo
Gelin Luo

Reputation: 14373

Rythm support @this() directive to load the template in a recursive way. See http://play-rythm-demo.appspot.com/demo/fibonacci

However it looks like there is a bug introduced and now it will raise StackOverflowError even when the terminate condition is specified. Please submit a bug report to https://github.com/rythmengine/rythmengine/issues

Updates

The StackOverflowError is caused by boolean type. It works all good if using other type of variable to control the termination of 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 the test done on rythm fiddle

enter image description here

Upvotes: 1

Related Questions