Rythm. render() throws exception while generating an xml file

79 Views Asked by At

I am trying to generate an xml file using Rythm template and I get the org.rythmengine.utils.TextBuilder build() error when I add an @if() condition with a @for() loop.

@if(errorObjs != null) {
  <Errors>
  @for(ErrorObject errObj : errorObjs) {
     <Error>
        <ErrorCode>@errObj.errorid</ErrorCode>
        <ErrorMessage>@errObj.errorcode</ErrorMessage>
     </Error>
  }
  </Errors>
}
2

There are 2 best solutions below

0
On BEST ANSWER

There is nothing wrong with your template try the code below at:

http://fiddle.rythmengine.org/#/editor

The issue might be in your Java Code.

@def class ErrorObject {
  String errorid;
  String errorcode;
  public ErrorObject(String id,String code) {
     errorid=id;
     errorcode=code;
  }
}
@{
  List<ErrorObject> errorObjs=new ArrayList<ErrorObject>();
  errorObjs.add(new ErrorObject("id1","code 1"));
  errorObjs.add(new ErrorObject("id2","code 2"));
  errorObjs.add(new ErrorObject("id3","code 3"));
}
@if(errorObjs != null) {
  <Errors>
  @for(ErrorObject errObj : errorObjs) {
     <Error>
        <ErrorCode>@errObj.errorid</ErrorCode>
        <ErrorMessage>@errObj.errorcode</ErrorMessage>
     </Error>
     }
 </Errors>
}
1
On

Not the answer to the question, just a comment about the usage of the @if:

if you want to check if something is null go directly with:

@if(errorObjs) { // display the errors }

See more about @if directive at http://rythmengine.org/doc/directive.md#if

Another suggestion about the code is container object like List shall always be non-null value, instead of return null for container, return empty container to make your code be much cleaner.