Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maybeField #311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add maybeField #311

wants to merge 1 commit into from

Conversation

nagisa
Copy link
Contributor

@nagisa nagisa commented Nov 12, 2014

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.

@krsch
Copy link
Contributor

krsch commented Nov 12, 2014

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 field allows to easily introduce optional keys which can then be
conditionally rendered in templates.
@nagisa
Copy link
Contributor Author

nagisa commented Nov 13, 2014

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

@jaspervdj
Copy link
Owner

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.

@wferi
Copy link
Contributor

wferi commented Dec 17, 2014

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.

@jaspervdj
Copy link
Owner

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

@wferi
Copy link
Contributor

wferi commented Dec 17, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants