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"/>
<dependency> <groupId>com.atlassian.soy</groupId> <artifactId>soy-template-renderer-api</artifactId> <version>1.1.7</version> </dependency>
Unrecognized function 'formatNumber' (function call "formatNumber($aNumber)")
<transformation extension="soy"> <transformer key="jiraSoyTransformer"/> </transformation>
Community moderators have prevented the ability to post new answers.
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.
SoyClientFunction's are not allowed to take in parameters, regardless of what transformers one is using.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.