How to store temporary variables as concatenation of two or more variables?

I think one of the major features jekyll lacks is the facility to have temporary variables which can be very convenient. For example, {{site.url}} gives me https://prahladyeri.github.io/ and {{page.url}} gives me /blog/2024/06/most-aesthetic-google-fonts.html which is fine. However, at many places I have to use a combination of these two. For setting the canonical attribute for example:

  <link rel="canonical" href="{{site.url}}{{page.url}}" />

Is there a way to concat these two into a temporary variable so that I can simply use {{full_url}} ? If so, where do I do that? Please note that I’m using Github Pages and don’t have actual Ruby/Jekyll running on my machine, Github Pages only supports a handful of official plugins.

not an answer to your original question ( I don’t think there is) but a better way to do what you want in the example - use the absolute_url filter - which will also add in the baseurl if needed which your example does not:

{{ page.url | absolute_url }}

1 Like

There is, but @rdyar solution is better.

To achieve what you asked you can use capture, like this:

{% capture full_url %}{{site.url}}{{page.url}}{% endcapture %}
1 Like

@george-gca But this needs to be declared inside every single include file where I want to use, right? Is there any way to declare it just once (for eg, _config.yml) and be able to use it in any of the jekyll includes/templates?

Right. So the best way is to use @rdyar solution in this case.

1 Like