I want to change post url to
{% assign post.url = "/" %}
<div class="entry-content">{{post.url}}{{ post }}</div>
{% endfor %}
But assign didn’t work, its rendering /2021/10/welcome-to-jekyll2.html
How can I change the URL?
I want to change post url to
{% assign post.url = "/" %}
<div class="entry-content">{{post.url}}{{ post }}</div>
{% endfor %}
But assign didn’t work, its rendering /2021/10/welcome-to-jekyll2.html
How can I change the URL?
I will provide the answer below, but would like to know what it is your are trying to accomplish. Could you share more details?
If your code is just going to change the post.url to a slash, then there’s no point in modifying it. Just create a random variable, like this:
{% assign slash = "/" %}
<div class="entry-content">{{slash}}{{ post }}</div>
@BillRaymond Hi, I have 2 page, a home page and a post page. I am adding {{post}} to my homepage, but post in homepage, it still shows post’s link
I am trying to change post’s link to “/”
To list the post with a link, you can use:
<a href="{{- post.url | relative_url -}}">{{ post.title }}</a>
Then, to create a link back to your home page, you can use:
<a href="{{- '/' | relative_url -}}">Return home</a>
or
<a href="{{- '/index.html' | relative_url -}}">Return home</a>
Is this what you are looking for?
In Liquid, the [see corrections below]assign
tag can only create new named variables, not reset existing values. In this sense, Liquid is a functional language.
To set a page or post’s URL, you can set the permalink
value in the YAML front matter, or globally.
Technically, that is an incorrect statement, @chuckhoupt.
assign
creates and modifies variables in the current scope.
Try in a site with config title: "Test Site"
:
---
title: "Hello World"
---
<div>
{% assign fruit = "apple" %}
<div>{{ fruit }}</div>
{% assign fruit = site.time %}
<div>{{ fruit }}</div>
<div>{{ page.title }}</div>
{% assign page.title = "Bazinga!" %}
<div>{{ page.title }}</div>
{% assign page = site %}
<div>{{ page.title }}</div>
</div>
Indeed. You can use assign repeatedly but you just can’t seem to use it to set an attribute.
Another thing to note that makes assign more functional/ immutable is when you append to an array in Jekyll Liquid with push
, you need to store the result. Either on the same or a new variable.
---
x: ["abc"]
---
{% assign x = x | push: "def" %}
And there is no equivalent filter like push that updates a map {}
, based on discussions elsewhere.
Thanks @ashmaroli for catching my error. I got confused by the terse Liquid doc description.
I was trying to express the point @MichaelCurrin makes – objects (maps/arrays, strings, numbers) in Liquid are immutable, so they can’t be modified with the assign
or capture
tags.
Sorry to revive this old post but I have some clarifying questions:
Template text:
{% assign some_array = "foo,bar,baz" | split: "," %}
Some Array, Original: {{ some_array | inspect }}
{% assign some_array = some_array | push: "hello" %}
Some Array, modified: {{ some_array | inspect }}
Output:
Some Array, Original: ["foo", "bar", "baz"]
Some Array, modified: ["foo", "bar", "baz", "hello"]
Hasn’t some_array
been modified in this case?
I don’t know how to create a map in the same way that I did with an array and split on a string. But assuming I have a map from Jekyll data
# existing_map is filled from jekyll data
# the following does not work, cannot modify existing_map
# the liquid parser should _really_ warn or throw an error that an assignment has failed because the map is read-only
{% assign existing_map['new_key'] = "new value" %}
The 'new_key' entry is not added
{{ existing_map | inspect }}
For context, this is my use-case: I have a yml file that contains an array of maps. Each map is information about a book, e.g. { title:"War of the Worlds", description:"Some _markdown_ text" }
. Some of the values I need to run through markdownify
. Then output the result as JSON.
This is what I have
{% assign sorted_books = site.data.books | sort: "posted_date" | reverse %}
# There is no way to markdownify only the values of the maps within the array
{{ sorted_books | jsonify }}