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
Support Jekyll-like includes: {% include {{ page.my_variable }} %} #433
Comments
I think you can do this now (at least in v9.29.0) w/ dynamic includes and const { Liquid } = require('liquidjs');
const engine = new Liquid();
const tpl = engine.parse(`
{%- assign myVariable = "home" -%}
Option 1:
{%- assign inclFile = myVariable | prepend: "_includes/header-" | append: ".liquid" -%}
<!-- "{{ inclFile }}" -->
{% include inclFile %}
Option 2:
{%- capture inclFile2 -%}
_includes/header-{{ myVariable }}.liquid
{%- endcapture -%}
<!-- "{{ inclFile2 }}" -->
{% include inclFile2 %}
`);
engine.render(tpl, {v: "Liquid"}).then(console.log); Where my ./_includes/header-home.liquid file looks like this: Welcome to {{ v }}! And the output looks like: > node example1
Option 1:<!-- "_includes/header-home.liquid" -->
Welcome to Liquid!
Option 2:<!-- "_includes/header-home.liquid" -->
Welcome to Liquid! |
@pdehaan Modifying the source files is out of question. We have around 5,000 I need a solution on liquidjs level, not on the Liquid source files level, that's why I'm asking to either introduce Jekyll-style includes into liquidjs core, or extend the |
I can try make this happen. If there's no perf regression I think we can ship an updated include. Otherwise, extending include would be better compared to rendering templates in FS. |
@harttle Yeah, rendering in FS is definitely not the best solution! It's just a solution that I, a liquidjs user, can easily go for to get the result I need. :) This is the code I have for now: fs: {
resolve(root, file, ext) {
return root + '/' + file
},
async readFile(file) {
let urlToFetch = file
if (file.indexOf('{{') !== -1) {
const ctx = JSON.parse(pageContext.page)
urlToFetch = await engine.parseAndRender(file, { page: ctx })
}
const data = await fetch(urlToFetch)
if (data.ok) {
return data.text()
} else {
throw new Error(`readFile(file): ${file} not found`)
}
}, It's not ideal, though. It doesn't preserve ALL context. So my solution works partially, and either a support for Thanks for offering the help! |
A few more questions @Nowaker :
Maybe through a |
This sounds good. Maybe "jekyll" would be an even better name for this mode. |
# [9.30.0](v9.29.0...v9.30.0) (2021-12-18) ### Features * support jekyll-like include, see [#433](#433) ([23279a8](23279a8))
I find it's already supported if On v9.30.0, I ported that feature also to Lines 164 to 179 in 23279a8
|
@harttle Implementation & docs are perfect, thank you! Thanks a lot! I'll promptly update and remove our In the meantime, my project - rendering Jekyll sites in Gatsby - is moving forward! We have almost everything implemented already... Very exciting. |
Glad to hear! Also feel free to add an entry to Related Packages if you want to. Adding corresponding icons is also OK. |
Just like it's included in Jekyll: https://jekyllrb.com/docs/includes/#using-variables-names-for-the-include-file
It's basically an improved and more flexible version of dynamic partials.
I tried working this limitation around in
fs.readFile(path)
interface, but unfortunately, the spec is too limited. The only argument passed isprefix/{{ page.my_variable }}/suffix
. Liquid context is lost, so I cannot run this string through the Liquid engine again for processing to read the correct file.If the spec of
fs
is modified tofs.readFile(path, context)
or there'sthis.liquidContext
, it would make my use case trivial to implement infs
, unless you want to make Jekyll-like dynamic includes an official part of Liquidjs.The text was updated successfully, but these errors were encountered: