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

Set and use custom variables in Markdown files and HTML templates #8

Closed
TheEvilSkeleton opened this issue Jun 23, 2024 · 6 comments
Closed

Comments

@TheEvilSkeleton
Copy link

TheEvilSkeleton commented Jun 23, 2024

I'm trying to set variables within Markdown files so I can reuse them in Markdown files and HTML templates.

Say I write an article in Markdown where the file name is keyboard-review.md, which contains:

Title: My First Review
Date: 2010-12-03 10:20
Category: Review
Description: Following is a review of my favorite mechanical keyboard.

{{ Description }}

Ideally, {{ Description }} is supposed to be a variable that I can reuse everywhere within the Markdown file, but it ends up printing nothing. I can only use them in HTML templates.

From what I understand, I can use the {% set %} statement/function to set and use variables within the same Markdown file, i.e. {% set Description = [...] %}, but this method has a limitation with the scope, where I can't access them in HTML templates. In other words, one method only works in Markdown files, and the other method only works in HTML templates. I can't find a method that has both.

I read the README, and discovered this:

All input that needs Pelican variables such as article, category, etc., should be put inside your theme’s templating. As such, the main use of this plugin is to automatically generate parts of your articles or pages.

To be honest, I'm not really sure what it means, and whether it's relevant or not.

@justinmayer
Copy link
Contributor

  1. Why do you need a single method that works in both templates and Markdown content? Would it not be sufficient to use two different methods, if that's what it takes to make things work in both templates and Markdown content?
  2. As far as I understand it, article metadata can't be accessed via {{ Description }} because the metadata is associated with the article object, and as noted in the part of the README you quoted above, objects such as article (and thus variables such as {{ article.description }}) are only available from within theme templates — not inside Markdown content.

@TheEvilSkeleton
Copy link
Author

Why do you need a single method that works in both templates and Markdown content? Would it not be sufficient to use two different methods, if that's what it takes to make things work in both templates and Markdown content?

To be honest, I don't necessarily need it. It's just something I hope to have in Pelican, because I've been relying on it with Jekyll for several years: https://gitlab.com/TheEvilSkeleton/theevilskeleton.gitlab.io/-/blob/bf07b34709856282bf2ba45ade7b216a23c44e0b/src/_posts/2024-06-03-libadwaita-splitting-gtk-and-design-language.md#L18. I find it really convenient as a blogger.

As far as I understand it, article metadata can't be accessed via {{ Description }} because the metadata is associated with the article object, and as noted in the part of the README you quoted above, objects such as article (and thus variables such as {{ article.description }}) are only available from within theme templates — not inside Markdown content.

Bummer, what do you think would be the best alternative? Preferably one that would not need to copy paste the values?

@justinmayer
Copy link
Contributor

I think it really depends on the specific use case. For the use case described here — using a summary/description as the first paragraph of the article — I would do the inverse and use the article's first paragraph as the summary by setting SUMMARY_MAX_PARAGRAPHS = 1. (Requires as-yet-unreleased Pelican main branch.)

@TheEvilSkeleton
Copy link
Author

TheEvilSkeleton commented Jul 1, 2024

I see... my articles are generally long, technical, and verbose. The number of paragraphs I write for the summary (or introduction) varies, as you can see at https://tesk.page/posts. Short articles/letters with introductions like this open letter don't have subheadings, so I kinda need the flexibility to place them in Markdown content.

@justinmayer
Copy link
Contributor

In that case, I would suggest trying the Summary plugin, which allows you to explicitly specify in the article where the beginning and end of the summary should be.

@TheEvilSkeleton
Copy link
Author

Awesome, thanks!

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

No branches or pull requests

2 participants