How to reference a metadata value in reStructuredText?

43 views
Skip to first unread message

Steve

unread,
May 1, 2020, 4:11:01 PM5/1/20
to nikola-discuss
Hi all,

I'm sure I overlooked something basic - how can I output a metadata variable from my document via reStructuredText?    I've read extensively about these values but cannot find how to use one in a post or file.  None of the tests below work to render the value assigned to the slug.  Also, are there any examples of more advanced .rst documents used with Nikola?  Maybe I can find some answers by example.


.. title: 1960s Macho Legends
.. slug: macho-legends

My slug is {$slug}

or is it ``slug``

or is it :slug:

our could it be {{% slug %}}

nfw it is {{% raw %}}{{% slug %}}{{% /raw %}}

Chris Warrick

unread,
May 1, 2020, 4:13:09 PM5/1/20
to Nikola—Discuss
You could use a template-based shortcode:
https://getnikola.com/handbook.html#template-based-shortcodes

--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

Steve

unread,
May 2, 2020, 11:03:56 AM5/2/20
to nikola-...@googlegroups.com
Chris, the docs indicate that the value still needs to be resolved to be passed to the template.

Suppose I create  shortcodes/foo.tmpl as such:
This uses the bar variable: ${bar}

And my post contains this:

{{% foo bar=slug %}}

Then the output file will contain:

This uses the bar variable: slug
But I want to output the value of slug.  In shell it would be the simple difference of echo $slug vs echo slug.
 













--
You received this message because you are subscribed to the Google Groups "nikola-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nikola-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nikola-discuss/CAMw%2Bj7LWhcs4JrF7Y2tfE94%2B07grZ3hJU9bNaMHFq1fnDm9YkA%40mail.gmail.com.

Chris Warrick

unread,
May 2, 2020, 11:07:44 AM5/2/20
to Nikola—Discuss
On Sat, 2 May 2020 at 17:03, Steve <gp6...@gmail.com> wrote:
>
> Chris, the docs indicate that the value still needs to be resolved to be passed to the template.
>
> Suppose I create shortcodes/foo.tmpl as such:
>
> This uses the bar variable: ${bar}
>
> And my post contains this:
>
> {{% foo bar=slug %}}
>
>
> Then the output file will contain:
>
> This uses the bar variable: slug
>
> But I want to output the value of slug. In shell it would be the simple difference of echo $slug vs echo slug.

The docs also have this paragraph:

The post in which the shortcode is being used is available as the post
variable, so you can access the title as post.title, and data loaded
via the data field in the metadata using post.data(key).

Steve

unread,
May 4, 2020, 10:00:43 AM5/4/20
to nikola-discuss
Chris, thanks for the clue.  I still have not escaped the room.  Tried with no avail:

{{% post.title %}}
{{% post.data(title) %}}
{{% post.data title %}}

Chris Warrick

unread,
May 4, 2020, 10:03:04 AM5/4/20
to Nikola—Discuss
On Mon, 4 May 2020 at 16:00, Steve <gp6...@gmail.com> wrote:
>
> Chris, thanks for the clue. I still have not escaped the room. Tried with no avail:
>
> {{% post.title %}}
> {{% post.data(title) %}}
> {{% post.data title %}}

You need to create templated shortcode, and use `post` inside it:

echo '${post.title()}' > shortcodes/foo.tmpl

And then you can use it in a post:

Post title is: {{% foo %}}

Roberto Alsina

unread,
May 4, 2020, 11:05:38 AM5/4/20
to Nikola—Discuss
This works too:

<!--
.. title: Test templated
.. slug: test-templated
.. date: 2020-05-04 15:03:43 UTC
.. tags:
.. category:
.. link:
.. description:
.. type: text
-->

{{% template %}}

The title is: ${post.title()}

{{% /template %}}
~                                     

--
You received this message because you are subscribed to the Google Groups "nikola-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nikola-discus...@googlegroups.com.

Roberto Alsina

unread,
May 4, 2020, 11:07:20 AM5/4/20
to Nikola—Discuss
And yes, the shortcode there HAS to say "template" :-)

Steve

unread,
May 4, 2020, 11:46:44 AM5/4/20
to nikola-discuss
Why does "title" work but not "slug"?    

<!--
.. title: Test templated
.. slug: test-templated
.. date: 2020-05-04 15:03:43 UTC
.. tags:
.. category:
.. link:
.. description:
.. type: text
-->

{{% template %}}

The title is: ${post.slug()}

{{% /template %}}

... rejected

Steve

unread,
May 4, 2020, 11:54:09 AM5/4/20
to nikola-discuss
What is the context for using echo '${post.title()}' > shortcodes/foo.tmpl ?  This is not restructured text, it would be some sort of shell escape that is recognized and executed during a build I suppose?  I could not find this documented.  If embedded shell hooks are possible, perhaps I could parse the slug value out of the header via awk in order to output it.  Convoluted but it could work if I can get a reference to the filename being processed.

Chris Warrick

unread,
May 4, 2020, 12:09:02 PM5/4/20
to Nikola—Discuss
Title has its own function, but slug does not. Use:

${post.meta('slug')}

On Mon, 4 May 2020 at 17:54, Steve <gp6...@gmail.com> wrote:
>
> What is the context for using echo '${post.title()}' > shortcodes/foo.tmpl ? This is not restructured text, it would be some sort of shell escape that is recognized and executed during a build I suppose? I could not find this documented. If embedded shell hooks are possible, perhaps I could parse the slug value out of the header via awk in order to output it. Convoluted but it could work if I can get a reference to the filename being processed.

It’s a one-time shell command (not part of any build process) that
creates the required shortcode file. You can use a text editor or
anything, but this `echo` command was meant to show what to put where.
Reply all
Reply to author
Forward
0 new messages