我目前正在使用Rythm创建一些片段 - 与AST结合使用,因为模板依赖于Java文件。
调用模板按预期工作。 有一个意外的行为。 我想以递归方式调用模板,但似乎这是不可能的。
Foo.html模板
@args String someString, boolean recursion Calling foo template with recursion: @recursion @if(recursion) { A recursion was requested: @recursion @foo(someString, false) }请注意 ,根据此示例的意图,这仅提供1级递归。 但是,我想稍后在@foo(someString, false)行中使用条件而不是false 。
您可以通过简单地将提供的示例复制并粘贴到foo.html来尝试此处的代码段。
错误信息
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. */现在,错误似乎与递归无关。 虽然,这是我在Eclipse中看到的错误消息。
我想,在调用模板时,不可能在其自身内调用它,因为Rythm只查找其他模板 - 或者看起来如此。
使用上面的链接访问Rythm Fiddle ,将代码放在bar.html而不是foo.html - 将@foo(someString, false) 第6行更改为@bar(someString, false) 。
现在, foo.html下行放在foo.html :
@bar("foo", true)执行此操作时,错误将更改为:
java.lang.SecurityException: java.util.concurrent.TimeoutException我认为这证明了我的上述假设,因为Rythm现在似乎找到了模板(或方法,即)。 这基本上就是我被困住的地方。
所以,问题是: 有没有办法根据某些条件递归调用Rythm模板?
我也对其他建议持开放态度,因为递归通常可以以非递归的方式处理。 我只是想避免重复的代码。
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.TimeoutExceptionI 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.
最满意答案
Rythm支持@this()指令以递归方式加载模板。 见http://play-rythm-demo.appspot.com/demo/fibonacci
但是看起来有一个bug被引入,现在即使指定了terminate条件,它也会引发StackOverflowError 。 请将错误报告提交至https://github.com/rythmengine/rythmengine/issues
更新
StackOverflowError由boolean类型引起。 如果使用其他类型的变量来控制递归调用的终止,它的效果都很好。
@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)}) }以下是对节奏小提琴的测试
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
更多推荐
发布评论