I'm using
<g:render template="myTmpl" model="[MandatoryItem:'value1']" />`
to render a template. But I need to pass an optional second variable into the template. I'm doing it with
<g:render template="myTmpl" model="[MandatoryVariable:'value1', Optionalvariable:'value2']" />
The second optional argument appears in my template as null
if it is not defined or as the defined value (value2
). How can I define a different default value for an unset variable?
If I understand you correctly, an easy option would be to use the Elvis operator in the template itself. Example:
<span class="variable-span">${OptionalVariable ?: 'defaultValue'}</span>
This would use the value of OptionalVariable
if it is set, otherwise it would use the right side of the operator.
Alternatively, if you would like to keep the default out of the template, you could use the same operator when calling render:
<g:render template="myTmpl" model="[MandatoryItem:'value1', OptionalVariable: someVariable ?: 'defaultValue']" />