Aria Templates Forums
[OPEN] Behavoiur of the "Default Available Modifiers" default - Printable Version

+- Aria Templates Forums (http://ariatemplates.com/forum)
+-- Forum: Public forums (/forumdisplay.php?fid=3)
+--- Forum: General framework discussions (/forumdisplay.php?fid=4)
+--- Thread: [OPEN] Behavoiur of the "Default Available Modifiers" default (/showthread.php?tid=110)



[OPEN] Behavoiur of the "Default Available Modifiers" default - donk - 8 November 2013 10:53

Hi,

I created a template and used the default modifier to specify a default value:

${data.amountInfo.amount|default:"0"}

But when data.amountInfo is null I get an error because of a JavaScript TypeError and nothing will be printed instead of the default value.

Your documentation states:

Quote:This modifier returns the default value if the entry value is equal to null (this is a non-strict equal, so "" is considered as null).

So I guess the behavior is correct regarding your documentation. But I think it would be more practical for everyday use if you extend the check , so that the default value will always be printed if the expression evaluates to null or throws an TypeError.

Otherwise I would have to use this form instead, which is more code for the same default behavior.

{if data.amountInfo}
   ${data.amountInfo.amount|default:"0"}
{/if)

And it gets worse if the nesting is even deeper

{if data.amountInfo && data.amountInfo.xxx}
   ${data.amountInfo.xxx.amount|default:"0"}
{/if)

Or

{if data.amountInfo}
  {if data.amountInfo.xxx}
    ${data.amountInfo.xxx.amount|default:"0"}
  {/if)
{/if)

So either extending the default modifier or another modifier for this situation would be nice.


RE: Behavoiur of the "Default Available Modifiers" default - jakub-g - 11 November 2013 14:27

I agree it would indeed be useful to have this feature from the TPL writer perspective to save some code - even separated from the modifiers.

Side note: CoffeeScript supports that feature, but using a special
?.
syntax http://coffeescript.org/#operators (scroll to "The Existential Operator").
It was considered to be added to C# as
.?
(the other way around) but hasn't made it yet: http://stackoverflow.com/a/2081709/245966

Following the CoffeeScript, basically, the input string
${data?.amountInfo?.xxx?.amount}
could be transformed on our side to the following JS:
(data && data.amountInfo && data.amountInfo.xxx) ? data.amountInfo.xxx.amount : null
and then processed further.

But let's consider the following
${data?.amountInfo.xxx?.amount}

This would have to be translated recursively into sth like
data ? ( data.amountInfo.xxx ? (data.amountInfo.xxx.amount) : null ) : null

It's useful from the user perspective, however this would further complicate our parser and the Eclipse editor. What's the team opinion on this?


RE: Behavoiur of the "Default Available Modifiers" default - susanta.behera - 12 November 2013 12:17

This will be a good feature addition.
+1 for this