Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to use a custom soy-function in a jira plugin

jchuet January 27, 2013

Hola,

I am trying to use a custom soy-function in my templates but I didnt find the way to register it properly.

First I have created my Class:

public class JsNumberFormatFunction extends AbstractLoggable implements SoyClientFunction, SoyServerFunction<String>
{
    @Override
    public String getName() {

        return "formatNumber";
    }

    @Override
    public JsExpression generate(JsExpression... args) {

        return new JsExpression("KT.NumberFormat.format(" + args[0] + ")");
    }

    @Override
    public String apply(Object... args) {

        return AmountFormat.getInstance(Locale.CANADA).formatAmount(BigDecimal.valueOf((Double)args[0]), AmountFormat.NO_CURRENCY);
    }

    @Override
    public Set<Integer> validArgSizes() {

        return ImmutableSet.of(1);
    }
}

Then I have added in my atlassian-plugin.xml the following

<soy-function key="numberFormat" class="com.my.plugin.JsNumberFormatFunction"/>

And in my pom.xml the dependency:
<dependency>
    <groupId>com.atlassian.soy</groupId>
    <artifactId>soy-template-renderer-api</artifactId>
    <version>1.1.7</version>
</dependency>

I am calling in my soy template: {formatNumber($aNumber)}
But I get the following err:
Unrecognized function 'formatNumber' (function call "formatNumber($aNumber)")

Also I am using this in my template web-resource
<transformation extension="soy">
     <transformer key="jiraSoyTransformer"/>
</transformation>

Any adivse would be much appreciated.
Cheers,
JC

3 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Bogdan Butnaru August 28, 2015

As far as I can tell, the JsExpression class does not have a toString() method, so your generate() method as given will generate broken JavaScript. It should instead read

return new JsExpression("KT.NumberFormat.format(" + args[0].getText() + ")");

(Note the explicit call to args[0].getText(), which should return the correct expression.)

I’m writing this a couple years after the question, though, so even if it’s still relevant the code might have changed in the mean time.

0 votes
Tyler B [Atlassian]
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 16, 2014

SoyClientFunction's are not allowed to take in parameters, regardless of what transformers one is using.

0 votes
Matej K
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 29, 2013

This worked fine for me on JIRA 5.2.11 and soy-template-renderer-api 1.1.9

Although, I've only tried it in server-side template and I am not using <transformation>

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events