Add maybeField
This field allows to easily introduce optional keys which can then be conditionally rendered in templates.
This is pretty useless for ListField (empty list is nullish) therefore I exported only String variant.
Wouldn't it be better to have a function like returnFromMaybe :: Maybe a -> Alternative a so that
returnFromMaybe value = maybe empty pure value
maybeField key value = field key $ returnFromMaybe <=< value
maybeField' key value = field' key $ returnFromMaybe <=< value
This is a very nice suggestion, thanks! Nice enough to not even bother with maybeField' generalisation, actually.
Right, I usually inline empty and pure into my fields. I'm not sure if this is obvious to new users though, for them maybeField might be useful.
Since $if(field)$ only cares whether field is defined or not, passing through a value makes sense only in cases like $if(field)$$field$$endif$, which can be straightforwardly handled by defining a field which expands to the empty string instead of being undefined (fromMaybe "" value) and using it as $field$. What I often miss is something like:
boolField :: String -> (Item a -> Bool) -> Context a
boolField name f = field name (\i -> if f i then pure undefined else empty)
or its monadic version.
I can see how that is useful but we'd rather have a descriptive error than undefined, I reckon.
Sure, improve it, it's just a proof of concept I picked up using. It may even be the wrong solution: we have ListField for $for$, why not have a new field type for $if()$? I'm not sure it buys us much beyond pure error "descriptive message mentioning name", though.