hakyll icon indicating copy to clipboard operation
hakyll copied to clipboard

Add maybeField

Open nagisa opened this issue 11 years ago • 6 comments

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.

nagisa avatar Nov 12 '14 17:11 nagisa

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

krsch avatar Nov 12 '14 22:11 krsch

This is a very nice suggestion, thanks! Nice enough to not even bother with maybeField' generalisation, actually.

nagisa avatar Nov 13 '14 06:11 nagisa

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.

jaspervdj avatar Nov 13 '14 10:11 jaspervdj

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.

wferi avatar Dec 17 '14 10:12 wferi

I can see how that is useful but we'd rather have a descriptive error than undefined, I reckon.

jaspervdj avatar Dec 17 '14 11:12 jaspervdj

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.

wferi avatar Dec 17 '14 16:12 wferi