Skip to content

Instantly share code, notes, and snippets.

@Ketrel
Last active June 2, 2024 21:25
Show Gist options
  • Save Ketrel/1f36fca6b4148f7263b3ee8fdb0923e7 to your computer and use it in GitHub Desktop.
Save Ketrel/1f36fca6b4148f7263b3ee8fdb0923e7 to your computer and use it in GitHub Desktop.
Relative url base for jekyll
{% assign relBase = '' %}
{% assign tempDepth = page.url | append: 'hackish-solution' | split: '/' | size | minus: 2 %}
{% for i in (1..tempDepth) %}
{% assign relBase = relBase | append: "../" %}
{% endfor %}
{% if relBase == '' %}
{% assign relBase = './' %}
{% endif %}
@Sakrecoer
Copy link

Thanks a milion!
Since all of my frontmatter has leading / in their address i went ahead adapted it a bit by adding a condition

{% assign relBase = '' %}
{% assign tempDepth = page.url | append: 'hackish-solution' | split: '/' | size | minus: 2 %}
{% for i in (1..tempDepth) %}
  {% if forloop.last %}
    {% assign relBase = relBase | append: ".." %}
  {% else %}
    {% assign relBase = relBase | append: "../" %}
  {% endif %}
{% endfor %}
{% if relBase == '' %}
    {% assign relBase = '.' %}
{% endif %}

@agowa
Copy link

agowa commented Oct 11, 2023

Does not work, at least not when included in _layouts/default.html

  Liquid Exception: stack level too deep in /workspaces/webpage/src/_layouts/default.html
                    ------------------------------------------------
      Jekyll 4.3.2   Please append `--trace` to the `serve` command 
                     for any additional information or backtrace. 
                    ------------------------------------------------
/workspaces/webpage/vendor/bundle/ruby/3.2.0/gems/liquid-4.0.4/lib/liquid/template.rb:206:in `block in render': stack level too deep (SystemStackError)
        from /workspaces/webpage/vendor/bundle/ruby/3.2.0/gems/liquid-4.0.4/lib/liquid/template.rb:240:in `with_profiling'
        from /workspaces/webpage/vendor/bundle/ruby/3.2.0/gems/liquid-4.0.4/lib/liquid/template.rb:205:in `render'
        from /workspaces/webpage/vendor/bundle/ruby/3.2.0/gems/liquid-4.0.4/lib/liquid/template.rb:218:in `render!'
        from /workspaces/webpage/vendor/bundle/ruby/3.2.0/bundler/gems/jekyll-8e4ccfc4ff30/lib/jekyll/liquid_renderer/file.rb:39:in `block (3 levels) in render!'
        from /workspaces/webpage/vendor/bundle/ruby/3.2.0/bundler/gems/jekyll-8e4ccfc4ff30/lib/jekyll/liquid_renderer/file.rb:59:in `measure_counts'
        from /workspaces/webpage/vendor/bundle/ruby/3.2.0/bundler/gems/jekyll-8e4ccfc4ff30/lib/jekyll/liquid_renderer/file.rb:38:in `block (2 levels) in render!'
        from /workspaces/webpage/vendor/bundle/ruby/3.2.0/bundler/gems/jekyll-8e4ccfc4ff30/lib/jekyll/liquid_renderer/file.rb:63:in `measure_bytes'
        from /workspaces/webpage/vendor/bundle/ruby/3.2.0/bundler/gems/jekyll-8e4ccfc4ff30/lib/jekyll/liquid_renderer/file.rb:37:in `block in render!'
         ... 9714 levels...
        from /usr/local/rvm/gems/default/gems/bundler-2.4.20/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
        from /usr/local/rvm/gems/default/gems/bundler-2.4.20/exe/bundle:29:in `<top (required)>'
        from /usr/local/rvm/gems/default/bin/bundle:25:in `load'
        from /usr/local/rvm/gems/default/bin/bundle:25:in `<main>'

@Sakrecoer
Copy link

@agowa this is not a layout. put it in _includes and call it in your layout. https://jekyllrb.com/docs/includes/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment