Search code examples
javatapestry

pass variable to a method as a parameter


I have following code in my tml file:

<t:loop source="navItem.subPages" value="var:subPage">
    <t:any element="li" class="prop:classForPageName">
        <t:pagelink page="var:subPage">${getMenuPageName(var:subPage)}</t:pagelink>
    </t:any>
</t:loop>

I have a problem to pass a variable var:subPage to method ${getMenuPageName(var:subPage)}, as this throws an exception:

Could not convert 'getMenuPageName(var:subPage)' into a component parameter binding: Error parsing property expression 'getMenuPageName(var:subPage)': line 1:15 no viable alternative at input '('.

Solution

  • You can't use binding prefixes (like var:) inside property expressions.

    You may only use prefix in front of the expression to let Tapestry know how it should interpret the remainder (the part after the colon).

    Refer to NBF grammar for property expressions to see what's allowed inside:

    Tapestry Documentation > User Guide > Property Expressions.

    Property expressions were created to support just very basic constructs. If you need more complex expressions you should create corresponding methods in your java class and refer to them using the prop: binding prefix.

    Template expansions you've mentioned (${...}) work the same as parameter bindings:

    Under the covers, expansions are the same as parameter bindings. The default binding prefix for expansions is "prop:" (that is, the name of a property or a property expression), but other binding prefixes are useful, especially "message:" (to access a localized message from the component's message catalog).