-
Reference to Liquidjs: Abstract File System, We can load template from database or an object and so on, and how to resolve include / render tag inside those template? Let's see there is an object like: const TEMPLATE = {
"a.com": {
"home.liquid": "{% include 'product' %}",
"product.liquid": "xxx",
},
"b.com": {
"home.liquid": "{% include 'product' %}",
"product.liquid": "xxx",
},
} And I custom the renderFileSync function: {
readFileSync (file: string) {
const index = file.indexOf('@');
const [site, fileName] = [file.slice(0, index), file.slice(index + 1)];
const siteTemplateString = global.__TEMPLATE.get(site);
if (siteTemplateString !== undefined) {
const siteTemplate = JSON.parse(siteTemplateString);
const template = siteTemplate[fileName];
if (template !== undefined) {
return template;
} else throw new Error(`Cannot find ${ fileName } template under site ${ site }.`);
} else throw new Error(`Site ${ site } is un-exist.`);
},
async readFile (file) {
throw new Error('Please use Liquid.readFileSync instead.');
},
existsSync () { return true },
async exists () { return true },
resolve(root, file, ext) { return file },
} So I can resolve home page of a.com like this: router.get('/', async (ctx, next) => {
const { host } = ctx.request; // such as 'a.com'
const data = liquidEngine.renderFileSync(`${ host }@home.liquid`);
ctx.body = data;
next();
}); In custom renderFileSync function, I can correctly get home.liquid of a.com, but there will missing product.liquid, because I lose host info, so how can I store the host info for every render? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
For the partial reference part, try the recently added relativeReference option (enabled by default). Add both hosts into For the cross origin
Then in |
Beta Was this translation helpful? Give feedback.
For the partial reference part, try the recently added relativeReference option (enabled by default). Add both hosts into
root
option, then include partials by relative path, like{% include './product' %}
instead of{% include 'product' %}
.For the cross origin
{% include %}
, maybe you'll need to specify the absolute URL to make it work, like{% include "https://example.com/product.liquid" %}
. Or, alternatively, you can invent an "abstracted FS" (implement multi domain by pseudo-directory), like:Then in
foo.liquid
, includebar.liquid
by{% include "../bar.com/bar.liquid" %}