Discussion:
[hakyll] Boolean fields in metadata
r***@gmail.com
2016-09-02 01:51:16 UTC
Permalink
Hi,

I'm trying to add a boolean "featured" field to my metadata. In the
template, I'd like to filter items based on whether or not they are
featured.

$if(featured)$
$partial("...")$
$endif$

I know that this only checks existence, so at the moment my solution is
simply leaving the field out of non-featured items. However, I was
wondering if there was a way to use something like `boolField` on metadata
so I could do something like:

featured: yes

- or -

featured: no

and have it display based on that.

Since `boolField` only accepts `Item a -> Bool`, and as far as I can
figure, Metadata can only be retrieved while wrapped in the MonadMetadata
monad, I've no idea if this is possible. The best I've managed is `Item a
-> m Bool`, which isn't particularly useful for anything.

Is there some way to do this that I'm missing, or is this sort of behavior
just not supported?

Thanks,
Rob
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Kyle Marek-Spartz
2016-09-02 12:29:09 UTC
Permalink
I do something like this on my site. Instead of `featured: no`, I would just leave the field out if the metadata for the non featured posts, only putting `featured: yes` on the featured posts.

–
Kyle Marek-Spartz
Hi,
I'm trying to add a boolean "featured" field to my metadata. In the template, I'd like to filter items based on whether or not they are featured.
$if(featured)$
$partial("...")$
$endif$
featured: yes
- or -
featured: no
and have it display based on that.
Since `boolField` only accepts `Item a -> Bool`, and as far as I can figure, Metadata can only be retrieved while wrapped in the MonadMetadata monad, I've no idea if this is possible. The best I've managed is `Item a -> m Bool`, which isn't particularly useful for anything.
Is there some way to do this that I'm missing, or is this sort of behavior just not supported?
Thanks,
Rob
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Beerend Lauwers
2016-11-14 16:05:50 UTC
Permalink
I'm doing something similar for HaskAnything that you might be able to use:

-- Given a key and a value, constructs a context that takes a key that will
come from a Hakyll template.
-- If the key (k) coming from the Hakyll template corresponds with the one
we provided (key), we return the value.
-- Otherwise, we return empty, which will make Hakyll continue down the
monoidal context chain in search of another Context that could provide
-- a value for this key.
ifField :: String -> (Item a -> Compiler ContextField) -> Context a
ifField key value = Context $ \k _ i -> if k == key then value i else empty


-- Given the key of some metadata, extracts the value as a string and
returns it.
-- If the metadata doesnt exist, we return empty.
extractMetadata :: String -> Item a -> Compiler ContextField
extractMetadata key i = do
m <- getMetadataField (itemIdentifier i) key
case m of
Just x -> return (StringField x)
Nothing -> empty


-- Example
ctx = ifField "has-permission" (extractMetadata "permission-file") <>
defaultCtx


You could probably swap out extractMetadata for your own function that
checks whether "featured" is set to yes or no, and return *something* in
case of yes, and empty in case of no.

Kinds Regards,
Beerend Lauwers
Post by r***@gmail.com
Hi,
I'm trying to add a boolean "featured" field to my metadata. In the
template, I'd like to filter items based on whether or not they are
featured.
$if(featured)$
$partial("...")$
$endif$
I know that this only checks existence, so at the moment my solution is
simply leaving the field out of non-featured items. However, I was
wondering if there was a way to use something like `boolField` on metadata
featured: yes
- or -
featured: no
and have it display based on that.
Since `boolField` only accepts `Item a -> Bool`, and as far as I can
figure, Metadata can only be retrieved while wrapped in the MonadMetadata
monad, I've no idea if this is possible. The best I've managed is `Item a
-> m Bool`, which isn't particularly useful for anything.
Is there some way to do this that I'm missing, or is this sort of behavior
just not supported?
Thanks,
Rob
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...