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

Nested component paths #603

Open
gestchild opened this issue Oct 23, 2016 · 1 comment
Open

Nested component paths #603

gestchild opened this issue Oct 23, 2016 · 1 comment

Comments

@gestchild
Copy link

Hey,

I've been trying out Fractal on a Nunjucks project I'm currently working on and am really impressed with it.

One issue I've encountered is with nested component paths.

By default, it seems I can only include/extend components with the @handle syntax or via the filename (if I first specify a path when configuring the Nunjucks instance).

What I'd like to be able to do is include a component with its full path, e.g.

{% include "path/to/files/component.njk" %}

as this is how they are included in my project.

The only way I can see to achieve this is to add another custom loader into the Nunjucks adapter, along the lines of:

  const PathLoader = nunjucks.Loader.extend({
      getSource(path) {
          if (path.indexOf('/') === -1 ) {
              return;
          }
          const templateFile = path.split("/").pop();
          const templateName = '@'+templateFile.substr(0, templateFile.lastIndexOf('.'));
          const view = self.getView(templateName);
          if (view) {
              return {
                  src: view.content,
                  path: view.content,
                  noCache: true
              };
          }
      }
  });
  loaders.push(new PathLoader());

This takes the filename from the path string and converts it to a handle so it can be passed to the getView method.

It seems to work ok, but was wondering if there was a better way.

Thanks

@mihkeleidast mihkeleidast transferred this issue from frctl/nunjucks May 23, 2020
@stale
Copy link

stale bot commented Jul 25, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label Jul 25, 2020
@stale stale bot closed this as completed Aug 1, 2020
@mihkeleidast mihkeleidast reopened this Aug 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants