Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Liquid include path as variable #209

Closed
ghost opened this issue Sep 6, 2018 · 7 comments
Closed

Liquid include path as variable #209

ghost opened this issue Sep 6, 2018 · 7 comments
Assignees
Labels
education: template language Learning about a template language.

Comments

@ghost
Copy link

ghost commented Sep 6, 2018

Hi -
Fantastic project! I've been using Eleventy for a few months now and reciently ran into something that doesn't seem quite right. I'm using Liquid and am attempting to create include path's dynamically by:

# index.liquid

---
namespace: project_name
---
{% include 'header' %}
# header.liquid

{% capture path %}
  {{namespace}}/logo-header
{% endcapture %}
{% include {{path}} %}

The path var inside the include throws a render error: Failed to lookup: project_name/logo-header.liquid yet that file exists.

Any guidance would be greatly appeaciated!

@zachleat
Copy link
Member

zachleat commented Sep 6, 2018

You’re likely running into a discrepancy between Ruby liquid and LiquidJS. This is likely a good candidate for our “Common Pitfalls” documentation.

Check out the dynamicPartials: true option in liquidjs: https://github.com/harttle/liquidjs#options

You can enable this behavior in the Configuration API here: https://www.11ty.io/docs/languages/liquid/

This will let you do {% include path %} (note that it’s not using the {{ }} syntax there).

See also previous issues #177 and #73 for additional info. Hope that helps! I’m going to close this for now but feel free to continue commenting here with more questions!

@zachleat zachleat closed this as completed Sep 6, 2018
@zachleat zachleat added the education: template language Learning about a template language. label Sep 6, 2018
@zachleat zachleat self-assigned this Sep 6, 2018
@ghost
Copy link
Author

ghost commented Sep 6, 2018

Thanks, @zachleat
I have dynamicPartials: true in my config yet {% include path %} and {% include {{path}} %} are both throwing the same error as above.

@zachleat zachleat reopened this Sep 14, 2018
@zachleat
Copy link
Member

Definitely stick with dynamicPartials: true and {% include path %} but I’m guessing that your includes folder probably isn’t where you expect.

Have you reviewed https://www.11ty.io/docs/config/#directory-for-includes ?

If your input directory is . then it will expect your includes folder (by default) to be in ./_includes/ which means that it’s looking for your file in ./_includes/project_name/logo-header.liquid

Does that make sense?

You can change these directories in your config file.

@zachleat
Copy link
Member

Please reopen if you have additional questions!

@jeremenichelli
Copy link

jeremenichelli commented May 14, 2019

Hi @zachleat, I've been playing around with eleventy and I'm actually considering it for my personal site. First of all, thanks for all your work on this 💟

I'm having an issue related to this (sorry if it's a liquidjs thing and not an eleventy one), my case is that I'm dynamically including a file but leventy takes its literal name instead.

The file containing the include looks like this:

<head>
  {% capture stylesPath %}
    styles/{{layout}}.liquid
  {% endcapture %}
  <style>{% include stylesPath %}</style>
</head>

I have dynamicPartials: true set in the config and the file exists and is in the right folder (_includes/styles/home.liquid), but I get an error that says:

> ENOENT: Failed to lookup stylesPath.liquid in: _includes (Error):
    Error: ENOENT: Failed to lookup stylesPath.liquid in: _includes

Looks like eleventy doesn't understand that the name passed is a variable holding the actual path, am I missing something?

@jeremenichelli
Copy link

So, quick update. I figure it out.

The issue is that liquidjs support for variable capture + include is really unstable and the errors you get in the console are not clear enough and some time not meaningful. I think there`s a good opportunity for improvements here in that package. I will collect all the cases + errors with more time and hand you a more detailed report, we can connect with him if you are interested and explain better, even apply a fix to the parser if it's possible. But pay no mind on the entry above. Sorry for the noise.

@rupl
Copy link

rupl commented Oct 21, 2021

I just bumped into this and it ended up being whitespace inside {% capture %}. Something to consider if you're sure that the file exists, and the lookup error reports a location that looks correct.

<!-- ❌ Does NOT work, same error as OP described, and the file definitely exists -->
{% capture path %}
  maps/{{ mapDataFile }}
{% endcapture %}
{% include path %}

<!-- ✅ DOES work -->
{% capture path %}maps/{{ mapDataFile }}{% endcapture %}
{% include path %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
education: template language Learning about a template language.
Projects
None yet
Development

No branches or pull requests

3 participants