Functions

Soy functions are called from within Soy expressions.

The table below lists basic Soy functions that are available by default. For Soy functions related to bidirectional text, see Bidi Support. For information on writing custom Soy functions, see Plugins.

Function Usage
isFirst($var)

Use this with the foreach command. See more information in the foreach section of the Commands chapter.

isLast($var)

Use this with the foreach command. See more information in the foreach section of the Commands chapter.

index($var)

Use this with the foreach command. See more information in the foreach section of the Commands chapter.

isNonnull(value)

Returns true if the given value is both defined and not the value null.

length(list)

The length of a list.

keys(map)

The keys in a map as a list. There is no guarantee on order.

augmentMap(baseMap, additionalMap)

Builds an augmented map. The returned map contains mappings from both the base map and the additional map. If the same key appears in both, then the value from the additional map is visible, while the value from the base map is hidden. The base map is used, but not modified.

quoteKeysIfJs(mapLiteral)

This function is only valid when called on a map literal. It is a no-op in backends other than JavaScript. In JS, it causes the generated JS object literal's keys to be quoted strings instead of non-quoted. This makes no difference except in the case where your Soy-generated JS code will later be processed by Closure Compiler.

Specifically, for JS code generation, we now default to non-quoted keys. So the expression ['xxx': $aaa.bbb, 'yyy': 'boo'] might generate something like {xxx: data.aaa.bbb, yyy: 'boo'} which means you can use the usual dot-access in template code $myMap.xxx if your Soy-generated JS code will later be processed by Closure Compiler. On the other hand, if you want to generate JS code for a map with quoted keys (the old behavior), then write the expression quoteKeysIfJs(['xxx': $aaa.bbb, 'yyy': 'boo']), which might generate something like {'xxx': data.aaa.bbb, 'yyy': 'boo'}. In this case, you would have to use bracket-access in template code $myMap['xxx'] if your Soy-generated JS code will later be processed by Closure Compiler.

round(number)

Round to an integer.

round(number, numDigitsAfterDecimalPoint)

If numDigitsAfterDecimalPoint is positive, round to that number of decimal places; if numDigitsAfterDecimalPoint is negative, round to an integer with that many 0s at the end.

floor(number)

The floor of the number.

ceiling(number)

The ceiling of the number.

min(number, number)

The min of the two numbers.

max(number, number)

The max of the two numbers.

randomInt(rangeArg)

A random integer in the range [0, rangeArg - 1] (where rangeArg must be a positive integer).

strContains(str, subStr)

Checks whether a string contains a particular substring.