user39063
user39063

Reputation: 315

Dynamic Links in jekyll

currently I'm working on static website, so I'm using jekyll to generate it. To have a nice structure and fancy URLs, I use permalinks.

permalink: /impressum/

So for example the impressum.html is rendered to impressum/index.html. And in my HTML i can simply link to that file with

<a href="/impressum">

That works for me very well. But you know, I'm a programmer. What if I want for example to change the URL to /imprint/? Well, I can change the permalink without any problems. But what's about all the other links on the site? Yeah, sure, I can use the search & replace function of my editor to change the linked URLs and check the whole site with a site checker for broken links, but that's not the fancy way I want to go. That's why I tried to create some kind of global variables with the permalink.

_config.yml:

lnk_impressum: /impressum/

impressum.html

---
layout: layout
title: Your New Jekyll Site
permalink: {{ site.lnk_impressum }}
---

But that does not work. I get this error:

Generating... error: no implicit conversion of Hash into String. Use --trace to view backtrace

So what's wrong or is there a better way?

Upvotes: 3

Views: 2643

Answers (1)

nicksuch
nicksuch

Reputation: 1564

It doesn't seem to be possible to place Liquid tags inside the YAML Frontmatter or _config files, per this SO answer.

Something else you could try is based on the approach used by the documentation pages for Bootstrap, which uses a Page Variable they call slug that provides a unique, unchanging reference to each page.

For instance, if you'd like to place a link to your impressum.html page (for which the permalink could change), you can place this code on another page, such as index.html:

{% for mypage in site.pages %}
 {% if mypage.slug == 'impressum' %}
  <a href="{{ mypage.url }}">Link to Impressum page</a>
 {% endif %}
{% endfor %}

Then, in the YAML frontmatter for each of your pages, place code similar to the following:

---
slug: impressum
permalink: /my-permalink-to-impressum/
---

To change your permalinks in the future, you would just make the change to the Page Variable permalink in each page. The URLs referenced in your other pages would be automatically updated by Jekyll, as long as the slug variable remains unchanged.

Upvotes: 0

Related Questions