Ben
Ben

Reputation: 4984

Reuse file path in Jekyll

I am very new to writing websites and jekyll so I apologize if my terminology is unclear or wrong.

I have a bunch of saved files (foo1, foo2, etc) in a subdirectory called 'savedfiles' of my root jekyll folder. Suppose I am editing index.md which is in a sub folder called 'subfolder" and want to link to each of these. One way I can do this is to use

{{ site.url }}/savedfiles/foo1
{{ site.url }}/savedfiles/foo2
etc

Is there a way of saving the file path in a variable say pathfoo so that I could write

{{ pathfoo }}/foo1
etc

And sort of related to this can I get the file path of the subfolder that index.md is in? I know that

{{ page.path }} 

will give me {{ site.url }}/subfolder/index.md but I want {{ site.url }}/subfolder. Is this possible?

Upvotes: 0

Views: 476

Answers (1)

David Jacquel
David Jacquel

Reputation: 52829

Saving a path in a variable

{% capture path %}{{ site.url }}{{ site.baseurl}}/savedfiles/{% endcapture %}

you can now use this variable like this :

<a href="{{ path }}foo.html">Link to foo</a>

Getting file path from a file

It is a little bit tricky. But here is a way :

{% assign pathParts = page.path | split: "/" %}
{% assign length = pathParts.size | minus: 2 %}
{% assign path = "" %}
{% for c in (0..length) %}
    {% capture path %}{{ path }}/{{pathParts[c]}}{% endcapture %}
{% endfor %}

You now have a path variable like /folder/subfolder. This could be simplest with pop or shift filters, but they are not working as expected and will change in Jekyll 3.0.

Upvotes: 3

Related Questions