Search code examples
jekyllliquidyaml-front-matter

How can we create custom liquid variables within variables (i.e., custom liquid objects)?


Let us consider the following code:

---
layout: post
title:  "Welcome to Jekyll!"
test:
  name: google.com
---

{{ post.test.name }}
{% assign addr="https://www.google.com" %}

# Heading
This is a link to [the Google homepage!][ghome]
This is another link that [may or maynot work][glink]


[ghome]: https://google.com
[glink]: {{ addr }}

It outputs properly:

Output1

However, when I try to do the same but change addr to test.addr (and thus convert it to a data-member of test, the link breaks:

broken object

  1. How do I make the above work?!
  2. How can I add a new property to the variable page.test which is described in the front-matter of the page?

Solution

  • Within a Page, data flows in one direction.

    Front Matter => Content

    You can do the following:

    {% assign addr = page.test.name %}
    
    [link][glink]
    [glink]: {{ addr }}
    

    But you can't do the following:

    {% assign page.test.addr = "https://www.google.com" %}
    
    [link][glink]
    [glink]: {{ page.test.addr }}