frctl / fractal

A tool to help you build and document website component libraries and design systems.
https://fractal.build
MIT License
2.11k stars 170 forks source link

Nested component paths #603

Open gestchild opened 7 years ago

gestchild commented 7 years ago

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

stale[bot] commented 4 years ago

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.