Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll - declare image path in front matter as variable using {{ site.url }}

When I use the {{ site.url }} tag for an image path inside a variable in the front matter, it doesn't get translated into HTML.

The following works perfectly:

---
layout: post
title: chickpea
img: <img class="caption__media" data-interchange="[../asset/img/chickpea-small.jpg (small)], [../asset/img/chickpea-medium.jpg, (medium)], [../asset/img/chickpea-large.jpg, (large)]">
---

This does NOT work:

---
layout: post
title: chickpea
img: <img class="caption__media" data-interchange="[{{site.url}}/asset/img/chickpea-small.jpg (small)], [{{site.url}}/asset/img/chickpea-medium.jpg, (medium)], [{{site.url}}/asset/img/chickpea-large.jpg, (large)]">
---

But when I use the same image link with the {{site.url}} tag inside a post and not as a variable, it works.

Analysing the generated site shows that Jekyll doesn't convert the {{site.url}} tag when I use it in the image variable defined in the front matter.

So the question is: How can I get Jekyll to translate the image path in the YAML front matter correctly?

like image 615
Vin Banton Avatar asked Aug 13 '14 20:08

Vin Banton


People also ask

How do I add YAML front matter?

YAML frontmatters can be defined at the beginning of a file, by starting on the first line with three dashes ( --- ) and ending the frontmatter either with three dashes or three dots (the former variant is more common). They contain valid YAML and can be used to define arbitrary variables.

What is front matter Jekyll?

Front matter is a snippet of YAML placed between two triple-dashed lines at the start of a file. You can use front matter to set variables for the page: --- my_number: 5 --- You can call front matter variables in Liquid using the page variable.

What is front matter markdown?

Overview. The Front Matter extension tries to make it easy to manage your Markdown pages/content. Within a Markdown page, we allow you to fold the file's Front Matter to be less distracting when writing. Also, do we highlight the Front Matter content to create a visual difference between content and metadata.


2 Answers

You're mixing data and template in the yaml. Your image tag (which is a template) will be duplicated in all your posts. But the only thing you need here are your image urls.

So, in your yaml front matter, do :

---
layout: post
title: chickpea
img:
  small:  chickpea-small.jpg
  medium: chickpea-medium.jpg
  large:  chickpea-large.jpg
---

And in your layout/post.html you can add :

{% if page.img %}
  <img class="caption__media" data-interchange="
  {% for img in page.img %}
    [{{site.baseurl}}/asset/img/{{img[1]}} ({{img[0]}})]
    {% unless forloop.last %}, {% endunless %}
  {% endfor %}
  ">
{% endif %}

this code is multi-lines for demo purpose. You'd better put all on one line.

Note: I'm using {{site.baseurl}} instead of {{site.url}} because if your site is not at the root of a domain baseurl will save you from broken assets paths.

And now you have a clean separation of concerns, clear yaml front matter and maintainable code. Cool isn't it ?

like image 55
David Jacquel Avatar answered Sep 24 '22 23:09

David Jacquel


I just solved the problem using this technique: Include jekyll / liquid template data in a YAML variable?

therefore i changed the use of the variable inside the post from:

{{ post.img }}

to:

{{ post.img | replace: '..', site.url }}

I hope this helps someone with the same problem. :)

like image 21
Vin Banton Avatar answered Sep 21 '22 23:09

Vin Banton