簡體   English   中英

有沒有辦法根據某些條件遞歸調用Rythm模板?

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

我目前正在使用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模板?

我也對其他建議持開放態度,因為遞歸通常可以以非遞歸的方式處理。 我只是想避免重復的代碼。

Rythm支持@this()指令以遞歸方式加載模板。 http://play-rythm-demo.appspot.com/demo/fibonacci

但是看起來有一個bug被引入,現在即使指定了terminate條件,它也會引發StackOverflowError 請將錯誤報告提交至https://github.com/rythmengine/rythmengine/issues

更新

StackOverflowErrorboolean類型引起。 如果使用其他類型的變量來控制遞歸調用的終止,它的效果都很好。

@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)})
}

以下是對節奏小提琴的測試

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM