Assign doesn't work

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>
1 Like

@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?

1 Like

In Liquid, the assign tag can only create new named variables, not reset existing values. In this sense, Liquid is a functional language. [see corrections below]

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.

2 Likes