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

Is it possible to pre-process a Javascript template and return the content to a Liquid/Nunjucks one? #1160

Closed
noelforte opened this issue May 8, 2020 · 5 comments

Comments

@noelforte
Copy link

noelforte commented May 8, 2020

EDIT 05/11/2020: Simplified my original question to avoid confusion about what I'm asking.

If I have multiple javascript templates (.11ty.js) that do different things and then return some content, ie

// do stuff to get results

module.exports = class {
  render() {
    // return results
  }
}

and want to include their results it into a liquid/nunjucks template, I'm thinking I could just do a:

{% include "dynamic-content.11ty.js" %}

but that only inserts the raw content of the js template rather than what it renders. Is it possible to have Eleventy process the JS template first and then have the results included in a different template? Or is this something better handled by a shortcode?

@noelforte noelforte changed the title Is it possible to include templates of one type into another? Is it possible to pre-process a Javascript template and return the content to a Liquid/Nunjucks one? May 11, 2020
@zachleat
Copy link
Member

I believe this is a duplicate of enhancement request #148—do you agree?

@zachleat
Copy link
Member

A short-term thing you can do is put the stuff into a collection and reference that.

@noelforte
Copy link
Author

@zachleat possibly? I was poking around on #272 and am in this case experimenting with using 11ty to run some simple build steps to generate CSS, JS, etc.

While the flexibility of rendering one template type within another would certainly be awesome, what I'm hoping to do currently is handle the processing of output within a template, and if it's small enough, inline it.

My quick fix so far is to have these sorts of things handled by a universal shortcode that could either run in a nunjucks/liquid template and inline the result or run in a JS template and render the result with the output permalink being /main.css or something to that end.

I'll look at the documentation again, how would a collection help order the processing steps?

@LostKobrakai
Copy link

I stumbled over this by trying to have an SSR components' html included in otherwise plain old html. I tried to include an .11ty.js file from an .ejs file naively thinking I can compose multiple ways of templating just parts of a website. This would be awesome an super simple way to do that.

@noelforte
Copy link
Author

Coming back to this: filters and shortcodes ended up being the best solve for me with regard to "computing a thing"/"injecting results in template". With a configured shortcode that either takes additional parameters or not, I can either use it within a template to return the results of the job, or create a template that just contains the shortcode to generate a file containing the results of the job.

In addition, if I dont need to access the shortcode project-wide, then I can cut overhead by having a JS template run what would have otherwise come from the shortcode, or import the shortcode function from a config file. Lots of choices. Thanks for all the responses on this, going to close now.

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

3 participants