You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Rob Kischuk <RK...@sprich.com> on 2004/07/14 15:28:22 UTC

Macro not evaluating

I'm having one of those bang-head-against-the-wall moments with Velocity
1.4.  I've got a formatter class with the following methods:

 

  public String currency(float amount) {

    Format formatter = NumberFormat.getCurrencyInstance();

    return formatter.format(new Float(amount));

  }

 

  public String leftPad(String string, int width) {

    if (string.length() >= width) {

      return string.substring( string.length()-(width+1));

    }

    StringBuffer output = new StringBuffer();

    for (int i=0; i < (width - string.length()); i++) {

        output.append(' ');

    }

    output.append(string);

 

    return output.toString();

  }

 

And have defined macros in my template:  

 

#macro(leftpad $message)

  ${formatter.leftpad($message, 8)}

#end

#macro(currency $amount)${formatter.currency($amount)}#end

 

And called them in the template:

 

#foreach($orderLine in $orderLines)

#leftpad($orderLine.quantity)

#currency($order.shippingPrice)

#end

 

Strangely, the #currency macro fully executes, but the #leftpad macro leaves
$formatter.leftpad($orderLine.quantity, 8) after the template has been
processed.  Is there something I'm doing wrong here?  And is there a
different way I should be doing this?

 

Thanks,

      Rob


Re: Macro not evaluating

Posted by Nathan Bubna <na...@esha.com>.
Rob Kischuk said:
...
>   public String leftPad(String string, int width) {
--------------------^-----------------------

note the capitalization
...
> And have defined macros in my template:
...
> #macro(leftpad $message)
>   ${formatter.leftpad($message, 8)}
-------------------^-----------------
> #end

note the capitalization.

...
> Strangely, the #currency macro fully executes, but the #leftpad macro leaves
> $formatter.leftpad($orderLine.quantity, 8) after the template has been
> processed.  Is there something I'm doing wrong here?

it looks mismatched capitalization.  if not (and that typo is only in this
email), then you might double check that $orderLine.quantity is actually
returning a string.

> And is there a different way I should be doing this?

apart from the capitalization typo, it looks like a good pattern.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org