Replies: 3 comments
-
Please try this https://liquidjs.com/tutorials/render-file.html#Abstract-File-System |
Beta Was this translation helpful? Give feedback.
0 replies
-
I like to use a Map-based template loader for such cases. Also note that import { Liquid } from "liquidjs";
/**
* A template loader that reads template content from a map of strings.
*
* @implements FS
*/
class MapFS {
// @type {Map<string, string>}
templates;
constructor(iterable) {
if (iterable !== undefined) {
this.templates = new Map(iterable);
} else {
this.templates = new Map();
}
}
async exists(filepath) {
return this.templates.has(filepath);
}
existsSync(filepath) {
return this.templates.has(filepath);
}
async readFile(filepath) {
return this.readFileSync(filepath);
}
readFileSync(filepath) {
const content = this.templates.get(filepath);
if (content === undefined) {
throw new Error("ENOENT");
}
return content;
}
resolve(dir, file, ext) {
return file + ext;
}
}
const layoutTemplate = `\
header
{% block content %}My default content{% endblock %}
{% render 'footer' %}`;
const partials = {
some_layout: layoutTemplate,
footer: "Some footer",
};
const liquid = new Liquid({
fs: new MapFS(Object.entries(partials)),
relativeReference: false,
});
const templateContext = {
you: "world",
};
const rootTemplate = `\
{% layout "some_layout" %}
{% block content %}Hello, {{ you }}!{% endblock %}
`;
const result = liquid.parseAndRenderSync(rootTemplate, templateContext);
console.log(result); |
Beta Was this translation helpful? Give feedback.
0 replies
-
@jg-rp great work, very intuitive and useful! We should try integrate this into LiquidJS. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, being new to liquidjs and running some tests, I cannot find a way to achieve layouts using template strings instead of files:
evey example uses files. using the code below ignores the content completly:
Thank you if you have any insights to share
Beta Was this translation helpful? Give feedback.
All reactions