Discussion:
[hakyll] Conditionally including a field in the context based on each page’s metadata
b***@gmail.com
2017-10-15 17:59:05 UTC
Permalink
Hi all,

I’d like to add a field to my Hakyll site’s context. If a certain key is
present in the metadata then I’d like to transform the corresponding value
and include that in the context. If the key is not present in the metadata
then nothing should be added to the context.

I wrote this function that *should* do what I described:

-- | Creates a new field based on the item's metadata. If the metadata field is
-- not present then no field will actually be created. Otherwise, the value will
-- be passed to the given function and the result of that function will be used
-- as the field's value.
transformedMetadataField :: String -> String -> (String -> String) -> Context a
transformedMetadataField key itemName f = field key $ \item -> do
fieldValue <- getMetadataField (itemIdentifier item) itemName
return $ maybe (fail $ "Value of " ++ itemName ++ " is missing") f fieldValue

However, if the metadata field is not present then this will still insert a
field into the context with the empty string as its value. For example, I
have this line in my context:

transformedMetadataField "meta_description" "meta_description" escapeHtml

and I have this template:

$if(meta_description)$
<meta name="description" content="$meta_description$"/>
$endif$

On pages with no meta_description in their metadata, the following HTML is
produced:

<meta name="description" content=""/>

whereas what I want is for no tag to be produced at all.

What have I done wrong in my transformedMetadataField function? Or is field not
suitable for what I’m trying to use it for?

Thank you!

(I’ve also posted this question to Stack Overflow
<https://stackoverflow.com/q/46750357/371228>.)

Benjamin
--
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.
r***@gmail.com
2017-11-02 00:55:35 UTC
Permalink
Replied on SO; hope it helps.
Post by b***@gmail.com
Hi all,
I’d like to add a field to my Hakyll site’s context. If a certain key is
present in the metadata then I’d like to transform the corresponding value
and include that in the context. If the key is not present in the metadata
then nothing should be added to the context.
-- | Creates a new field based on the item's metadata. If the metadata field is
-- not present then no field will actually be created. Otherwise, the value will
-- be passed to the given function and the result of that function will be used
-- as the field's value.
transformedMetadataField :: String -> String -> (String -> String) -> Context a
transformedMetadataField key itemName f = field key $ \item -> do
fieldValue <- getMetadataField (itemIdentifier item) itemName
return $ maybe (fail $ "Value of " ++ itemName ++ " is missing") f fieldValue
However, if the metadata field is not present then this will still insert
a field into the context with the empty string as its value. For example, I
transformedMetadataField "meta_description" "meta_description" escapeHtml
$if(meta_description)$
<meta name="description" content="$meta_description$"/>
$endif$
On pages with no meta_description in their metadata, the following HTML
<meta name="description" content=""/>
whereas what I want is for no tag to be produced at all.
What have I done wrong in my transformedMetadataField function? Or is
field not suitable for what I’m trying to use it for?
Thank you!
(I’ve also posted this question to Stack Overflow
<https://stackoverflow.com/q/46750357/371228>.)
Benjamin
--
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...