[TOC]
This chapter describes implicit coercions that Soy performs.
Some Soy operations will take an expression of a given type and coerce it to another type. This page will describe how these operations behave and when they trigger.
TODO(lukes): this page is insufficiently exhaustive, but it is a start
There are a few ways to cause an implicit string coercion in Soy.
- print commands implicitly coerce their expression to a string
- concatentation with a string will coerce a value to a string
While every value in Soy can be coerced to a string, not every value has a useful string representation, or a string representation that is consistent in all the backends.
These mostly do what you expect, but for numbers consider using a number formatting library to make I18n appropriate text.
These mostly do not do what you expect. Avoid doing this other than for debugging purposes.
There are a few ways to coerce a value to a boolean.
- use it in an if-expression
- use it in a ternary expression
All values have a boolean coercion (sometimes referred to as a 'truthiness' check), these mostly follow JavaScript semantics:
- '0' values are falsy, e.g.
null
,""
,0
- all other values are truthy
NOTE: there are some inconsistencies in how these work across backends. For example, in Python, the empty list, empty record, and empty maps are all currently falsy (b/19271140).