Home Jekyll: Define a new variable based on page variables
Reply: 0

Jekyll: Define a new variable based on page variables

user1400 Published in May 21, 2018, 3:11 am

My Problem

I am writing a static site in Jekyll. Each post should have a "Posted by" subtitle with the author's name, with an optional link to their webpage, and a fallback to the site title if no author name is defined.

For example, suppose that my site title is "Acme corp".

Author and "URL

author: Someone
author_url: https://their_site.com

Should give [Someone](https://their_site.com)


author: Someone

Should give Someone.

No Author

Should give Acme corp.

What have I tried

I can hard-code the logic into the _layouts/post.html page, but I will have to repeat myself in other page templates:

<span class="meta">Posted by
{% if page.author_url %}
<a href="{{ page.author_url }}">
{% endif %}
{% if page.author %}{{ page.author }}{% else %}{{ site.title }}{% endif %}
{% if page.author_url %}
{% endif %}
on {{ page.date | date: "%B %-d, %Y" }}</span>

My question

Is there a way to define a global variable (e.g. author_html) based on the values of page.author and page.author_url, that will be accessible on all template pages?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312996 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO