Include Jekyll/Liquid Template Data in a Yaml Variable

Include jekyll / liquid template data in a YAML variable?

I don't believe it's possible to nest liquid variables inside YAML. At least, I haven't figure out how to do it.

One approach that will work is to use a Liquid's replace filter. Specifically, define a string that you want to use for the variable replacement (e.g. !SITE_URL!). Then, use the replace filter to switch that to your desired Jekyll variable (e.g. site.url) during the output. Here's a cut down .md file that behaves as expected on my jekyll 0.11 install:

---
layout: post

excerpt: In the [earlier post in this series](!SITE_URL!/2013/01/12/)

---

{{ page.excerpt | replace: '!SITE_URL!', site.url }}

Testing that on my machine, the URL is inserted properly and then translated from markdown into an HTML link as expected. If you have more than one item to replace, you can string multiple replace calls together.

---
layout: post

my_name: Alan W. Smith
multi_replace_test: 'Name: !PAGE_MY_NAME! - Site: [!SITE_URL!](!SITE_URL!)'

---

{{ page.multi_replace_test | replace: '!SITE_URL!', site.url | replace: '!PAGE_MY_NAME!', page.my_name }}

An important note is that you must explicitly set the site.url value. You don't get that for free with Jekyll. You can either set it in your _config.yml file with:

url: http://alanwsmith.com

Or, define it when you call jekyll:

jekyll --url http://alanwsmith.com

How can I pass a variable to a jekyll layout?

What you are calling the "header of a markdown file" actually have a name in Jekyll, and this is called the Front Matter.

And the elements of the front matter can be accessed via the page variable, as pointed here and there.

So, your layout should read:

---
layout: default
---
<article id="postBody">
<h1>{{ page.title }}</h1>
<p>{{ page.date | date_to_string }}</p>

{{ content }}

{% if page.next_tutorial %}
<h2>{{ page.next_tutorial }}</h2>
{% endif %}
</article>

Jekyll Liquid Template: Use Variable of Layout in Page

You can put that variable outside the layout and posts, and then include it where you need it:

Create an include in _includes/customdata.html:

{% assign col = site.interfaceServer | where:"neededTopCollectionItem", page.topCollectionItem | sort: "order" %}

Then include it in someFile.md:

{%include customdata.html %}
{{col}}

or in the layout.

How to use Liquid include variable within a for loop?

Correct syntax in for loop is : {% for image in include.images %}

Conditional Jekyll Liquid class based on _data file

The following should render markup similar to your graphic..

{% for message in site.data.message-inbox %}
<div class="message-card__content {% if message.active == 'yes' %}active{% endif %}">
<div class="message-card__meta">
<div class="message-card__avatar">
<img src="{{ message.avatar }}" alt="{{ message.name }}">
</div>

{% if message.new == 'yes' %}
<img class="message-card__new" src="{{ 'images/icons/new.svg' | relative_url }}" alt="New Message">
{% elsif message.replied == 'yes' %}
<img class="message-card__reply" src="{{ 'images/icons/reply.svg' | relative_url }}" alt="Replied">
{% endif %}
</div>

<div class="message-card__info">
<ul>
<li class="message-card__name">{{ message.name }}</li>
<li class="message-card__subject">{{ message.subject }}</li>
<li class="message-card__preview">{{ message.message | truncate: 100 }}</li>
</ul>
</div>
</div>
{% endfor %}


Related Topics



Leave a reply



Submit