Hybrid View

  1. #1
    Sencha User
    Join Date
    Jul 2012
    Posts
    4
    Answers
    1
    Vote Rating
    0
    zakaluka is on a distinguished road

      0  

    Default Answered: XTemplate loop, Collections, and functions with multiple arguments

    Answered: XTemplate loop, Collections, and functions with multiple arguments


    Hello,

    I am trying to implement XTemplates in my project and have run across an issue. All the examples / sample code I've seen only use only argument to the template function, so I can't tell what I'm doing wrong.

    The code is as follows:

    Code:
    interface NewsTemplates extends XTemplates {
      @XTemplate("<div class='{style.rounded_corners_title}'>{set.sourceName}</div>"
        + "<div class='{style.rounded_corners_content}'>"
        + "<tpl for='items'>{#}</tpl>"
        + "</div>")
      SafeHtml newsBlock(GeneralCssResource style, INewsSet set, Collection<INewsItem> items);
    }
    This code throws a runtime NullPointerException. However, if I comment out the <tpl ...></tpl> section, then it runs perfectly well. According to the GXT 3 javadocs, XTemplate should be able to iterate over a Collection. Unfortunately, all the examples use only one argument and are based on either "." or a value inside that single argument. Does anyone have any advice on how to fix this problem?

    EDIT: I've used GWT.log() to check the values of the three variables and all are fine. The exception is thrown before a call to the function can be made (during the GWT.create() process). Apparently, it looks like the generator is unable to parse the code as I've written it...

    Regards,

    zak.
    Last edited by zakaluka; 20 Jul 2012 at 5:47 PM. Reason: added some clarification, fixed title.

  2. There may be an error in the documentation - at present, tpl#for works with a for loop, not a for-each. This generally makes XTemplates more efficient, but requires either a Java array, or a List - Collection isn't supported at this time. I'll correct the documentation, and see about a better error message for this case.

    The issue about "." is what "." can mean in different contexts:

    * If there is only one argument to the template, "." refers to that argument. Likewise, if "Person person" is the argument, and person.getName() is valid Java, then you can refer to the person's name as "name" or as "person.name".
    * If there is more than one argument, "." can't resolve to any one object, so is invalid at the global level.
    * Within a for loop, "." will resolve to the current item, no matter how many arguments are in the method.

    This is why a method
    Code:
    SafeHtml render(List<Data> data)
    uses for=".", while
    Code:
    SafeHtml render(List<Data> data, String otherVar)
    must use for="data"

  3. #2
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,731
    Answers
    109
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    There may be an error in the documentation - at present, tpl#for works with a for loop, not a for-each. This generally makes XTemplates more efficient, but requires either a Java array, or a List - Collection isn't supported at this time. I'll correct the documentation, and see about a better error message for this case.

    The issue about "." is what "." can mean in different contexts:

    * If there is only one argument to the template, "." refers to that argument. Likewise, if "Person person" is the argument, and person.getName() is valid Java, then you can refer to the person's name as "name" or as "person.name".
    * If there is more than one argument, "." can't resolve to any one object, so is invalid at the global level.
    * Within a for loop, "." will resolve to the current item, no matter how many arguments are in the method.

    This is why a method
    Code:
    SafeHtml render(List<Data> data)
    uses for=".", while
    Code:
    SafeHtml render(List<Data> data, String otherVar)
    must use for="data"

  4. #3
    Sencha User
    Join Date
    Jul 2012
    Posts
    4
    Answers
    1
    Vote Rating
    0
    zakaluka is on a distinguished road

      0  

    Default


    Colin,

    Thank you very much for the quick answer.

    Quote Originally Posted by Colin Alworth View Post
    There may be an error in the documentation - at present, tpl#for works with a for loop, not a for-each. This generally makes XTemplates more efficient, but requires either a Java array, or a List - Collection isn't supported at this time. ...
    This fixed my problem perfectly. I can't wait to actually use XTemplates more extensively now.

    Regards,

    z.