Replies: 1 comment 1 reply
-
In strict variables mode, the best way to handle optional variables is really to ensure that those "optional" variables are initialized to Having the ability to define optional variables in templates is something that could make its way to the framework, but the proposed syntaxes appear inconsistent with the Marten templating language. Something that I think could eventually work would be the ability to define an optional variable (with an optional default value eventually) from a template using a new {# Assigns possibly_missing to nil if it is not defined #}
{% optional possibly_missing %}
{# Assigns possibly_missing to "text" if it is not defined #}
{% optional possibly_missing = "text" %} Extending the capabilities of the {# Assigns possibly_missing to nil if it is not defined #}
{% assign possibly_missing unless defined %}
{# Assigns possibly_missing to "text" if it is not defined #}
{% assign possibly_missing = "text" unless defined %} In both cases, the important point is that it'd be mandatory to explicitly define the optional variable before using it in filter expressions. |
Beta Was this translation helpful? Give feedback.
-
When template errors are enabled for missing variables, it's difficult to work with optional values. This can be made easier by adding a feature to specify that a variable is optional, so it shouldn't throw error when missing.
Some ideas for syntax:
I would prefer the first or second as they enable using other filters on the resulting value.
Beta Was this translation helpful? Give feedback.
All reactions