1
votes

I would like to manipulate the "post_url" tag of Liquid/Jekyll with some variables. These are the two things I would like to do :

Store result into variable

I would like to store the result of post_url in a variable, currently, this does not work :

{%- assign _page_url = post_url 2019-05-19-test -%}

_page_url is empty after this line. :(

Use variable as an argument

But the main issue occurs when I try to use a variable as an argument. _item.post is a variable which contains a string pointing to a correct post.

{%- post_url _item.post -%}

Throw the exception :

Liquid Exception: Could not parse name of post "_item.post" in tag 'post_url'. Make sure the post exists and the name is correct.

Jekyll::Errors::InvalidPostNameError: '_item.post' does not contain valid date and/or title. in /_layouts/page.html Error: Could not parse name of post "_item.post" in tag 'post_url'. Make sure the post exists and the name is correct. Jekyll::Errors::InvalidPostNameError: '_item.post' does not contain valid date and/or title. Error: Run jekyll build --trace for more information.

1

1 Answers

3
votes

Because post_url is a Liquid tag, you need to include it in a way that's executing that tag's logic first so it can store the result. In these cases, you can use {% capture %} instead of {% assign %}:

{%- capture _page_url -%}
  {% post_url 2019-05-19-test %}
{%- endcapture -%}

As for using variables as an argument to post_url, this is not currently possible with this tag. Looking at the tag's code, it takes everything after post_url in the tag literally and attempts to parse it using their defined structure for posts, whereas something like the include tag has an actual step to detect variables and resolve their value. However, it looks like it will be possible in Jekyll 4.0 (see this PR). You may be able to get this behavior now by using the pre-alpha version, though it's not stable so you may see unexpected behavior elsewhere.