Discussion:
[hakyll] Tags as an optional field
Cristiano Paris
2015-08-09 12:38:49 UTC
Permalink
Hi all,

I'm on my way customizing my new Hakyll-powered blog and I wanted to insert
an optional "tags" metadata entry in my posts and have them rendered
differently when tags are specified.

So, I though that the best strategy would be to check whether some
"renderedTags" field is in the context and render the tags accordingly.
Basically, in my template I have something like this:

$if(renderedTags)$
in $renderedTags$
$endif$

I came up with a function implementing a new string field which is "empty"
when no "tags" entry is specified and a string with the rendered tags when
it is. Here is the function (I omitted the uninteresting parts):

renderedTagsField :: String -> Context String
renderedTagsField key = Context $ \ k _ i ->
if k /= key then empty else do
tags <- getTags (itemIdentifier i)
renderTags $ reverse tags
where
renderTags [] = empty
renderTags [t] = return $ StringField t
...

Now, this function works but I'm not completely satisfied with it. My
initial intention was actually to leverage on some already-available
functions like "field" but this one fmaps the returned value from the
"value" function to StringFields and I don't know how to return an empty
field when no "tags" entry was specified. It would have worked if I used
the "field'" function from Hakyll.Web.Template.Context but it is not
publicly exposed (and I think for a reason).

So my question is: is there any more elegant way to implement what I'm
doing?

Thakn you.

Regards.

Cristiano
--
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.
Jasper Van der Jeugt
2015-08-23 13:28:40 UTC
Permalink
Hey Cristiano,

Does the following not work?

renderedTagsField' :: String -> Context String
renderedTagsField' key = field key $ \i -> do
tags <- getTags (itemIdentifier i)
case reverse tags of
[] -> empty
[t] -> return t

This uses the available field function. I might be missing something,
however.

Peace,
Jasper
Post by Cristiano Paris
Hi all,
I'm on my way customizing my new Hakyll-powered blog and I wanted to insert
an optional "tags" metadata entry in my posts and have them rendered
differently when tags are specified.
So, I though that the best strategy would be to check whether some
"renderedTags" field is in the context and render the tags accordingly.
$if(renderedTags)$
in $renderedTags$
$endif$
I came up with a function implementing a new string field which is "empty"
when no "tags" entry is specified and a string with the rendered tags when
renderedTagsField :: String -> Context String
renderedTagsField key = Context $ \ k _ i ->
if k /= key then empty else do
tags <- getTags (itemIdentifier i)
renderTags $ reverse tags
where
renderTags [] = empty
renderTags [t] = return $ StringField t
...
Now, this function works but I'm not completely satisfied with it. My
initial intention was actually to leverage on some already-available
functions like "field" but this one fmaps the returned value from the
"value" function to StringFields and I don't know how to return an empty
field when no "tags" entry was specified. It would have worked if I used
the "field'" function from Hakyll.Web.Template.Context but it is not
publicly exposed (and I think for a reason).
So my question is: is there any more elegant way to implement what I'm
doing?
Thakn you.
Regards.
Cristiano
--
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.
Cristiano Paris
2015-08-26 09:47:08 UTC
Permalink
Yes, it was my bad. Thanks.
--
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...