-
|
I’m probably not seeing the forest for the trees: I like to check on the presence of a character in a front matter entry, to customize a template based on the result. In the specific case, I like to extend an author field (which used to contain Twitter handles, without a leading “@”) to also cover Mastodon handles (of the form “[email protected]”). Theoretically, something like the following should work: But it doesn’t—and neither a string, nor a list, nor a string and list conversion help, nor converting “author” first, nor doing any other transformations. It’s like “author” cannot be inspected and worked with at all—all that works is check on its presence. (I’ve worked through Eleventy, Nunjucks, and Jinja docs, so the forest I was looking at is big ;) What am I missing? What’s the simplest way of accomplishing this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
A few possible suggestions (for Nunjucks; Liquid would probably need a slightly tweaked syntax): ---
author: "@twitter"
---
{% if author[0] === "@" %}TWITTER1{% endif %}
{% if author.at(0) === "@" %}TWITTER2{% endif %}
{% if author.startsWith("@") %}TWITTER3{% endif %}
{% if author|first === "@" %}TWITTER4{% endif %}
{{ "TWITTER" if author|first === "@" else "MASTODON" }}Or if this is something you're using in 1+ places, I might suggest a custom Eleventy filter that you can reuse a bit more easily. eleventyConfig.addFilter("social", function (username="") {
if (!username) {
return "";
}
if (username.startsWith("@")) {
return `<a href="https://twitter.com/${username.replace(/^@/, '')}">TWITTER</a>`;
}
return "MASTODON";
});---
author: "@pdehaan"
author2: "[email protected]"
---
<p>{{ autho | social | safe }}</p>
<p>{{ author | social | safe }}</p>
<p>{{ author2 | social | safe }}</p>OUTPUT<p></p>
<p><a href="https://twitter.com/@pdehaan">TWITTER</a></p>
<p>MASTODON</p> |
Beta Was this translation helpful? Give feedback.
A few possible suggestions (for Nunjucks; Liquid would probably need a slightly tweaked syntax):
Or if this is something you're using in 1+ places, I might suggest a custom Eleventy filter that you can reuse a bit more easily.