-
|
I have an elaborate use case which can be simplified to something like this: Please note two details:
Since I could not find a configuration option for Unfortunately the context where Is there any configuration option to tell If not, is there any way to split Do you have any other suggestions how to achieve this functionality? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 7 replies
-
|
The second argument to import { Liquid, Context } from "liquidjs";
const engine = new Liquid();
const data = new Context({ param: "{{ foo }}", foo: 42 });
const template = `{{ param }}`;
const intermediate_result = await engine.parseAndRender(template, data);
console.log(intermediate_result);
engine.parseAndRender(intermediate_result, data).then(console.log);If I've understood your requirements correctly, I can imagine a single pass solution that involves wrapping your data (strings that contain additional markup) in a custom drop class. If your drop class has a reference to an instance of |
Beta Was this translation helpful? Give feedback.
-
I am not familiar with Drops, so I cannot comment on this suggestion. But it seems complicated. I thought on something much simpler, like a global boolean option passed to the engine so that it iterates internally until all substitutions are done. |
Beta Was this translation helpful? Give feedback.
-
|
For drops, I believe we can start with And in your drop: |
Beta Was this translation helpful? Give feedback.
-
|
I updated my code and I noticed that only assigned variables are passed via the context, the In other words, something like this works: "properties": {
"configs-ci": "native-cmake-clang,native-meson-clang,qemu-cortex-m0-cmake-gcc,qemu-cortex-m7f-cmake-gcc,qemu-riscv-rv32imac-cmake-gcc,qemu-cortex-m0-meson-gcc",
"build-types": "debug,release",
"test-actions": "prepare,build,test",
"prepare-build-test-actions2": "{% assign test-actions = properties.test-actions | split: ',' %}{% for test-action in test-actions %}echo xpm run {{ test-action }} --config {{ configName }}{{ os.EOL }}{% endfor %}"
},
"actions": {
"test-ci2": [
"{% assign configs = properties.configs-ci | split: ',' %}{% assign build-types = properties.build-types | split: ',' %}{% for config in configs %}{% for build-type in build-types %}{% assign configName = config | append: '-' | append: build-type %}{{ properties.prepare-build-test-actions2 }}{% endfor %}{% endfor %}"
]
}but this does not work: "properties": {
"configs-ci": "native-cmake-clang,native-meson-clang,qemu-cortex-m0-cmake-gcc,qemu-cortex-m7f-cmake-gcc,qemu-riscv-rv32imac-cmake-gcc,qemu-cortex-m0-meson-gcc",
"build-types": "debug,release",
"test-actions": "prepare,build,test",
"prepare-build-test-actions3": "{% assign test-actions = properties.test-actions | split: ',' %}{% for test-action in test-actions %}echo xpm run {{ test-action }} --config {{ config }}-{{ build-type }}{{ os.EOL }}{% endfor %}"
},
"actions": {
"test-ci3": [
"{% assign configs = properties.configs-ci | split: ',' %}{% assign build-types = properties.build-types | split: ',' %}{% for config in configs %}{% for build-type in build-types %}{{ properties.prepare-build-test-actions3 }}{% endfor %}{% endfor %}"
]
}The difference is the variable Note: In case you wonder what this might be and where is the markup, this is a configuration file where the values in the |
Beta Was this translation helpful? Give feedback.
-
|
I think both won’t work but behave differently,
You need in place rendering. We can preprocess the input and wrap all values with a custom tag or drop. Then call parseandrender with current ctx in place. |
Beta Was this translation helpful? Give feedback.
-
|
How about something like this? Note that I've removed the import { Liquid, Drop } from "liquidjs";
class PropertiesDrop extends Drop {
#derived_properties;
#properties;
#engine;
#template_properties;
constructor(engine, properties) {
super();
this.#engine = engine;
this.#properties = properties;
// Preprocess some properties.
this.#derived_properties = new Map([
["configs-ci", properties["configs-ci"].split(",")],
["build-types", properties["build-types"].split(",")],
["test-actions", properties["test-actions"].split(",")],
]);
// Names of properties that contain markup.
this.#template_properties = new Set(["prepare-build-test-actions3"]);
}
async liquidMethodMissing(key, ctx) {
if (this.#template_properties.has(key)) {
// TODO: cache the parsed template so we parse it just the once.
// `ctx` is available here since LiquidJS version 10.22.0.
return await this.#engine.parseAndRender(this.#properties[key], ctx);
}
// Fall back to input properties
return this.#derived_properties.get(key) || this.#properties[key];
}
}
const engine = new Liquid();
const data = {
properties: {
"configs-ci":
"native-cmake-clang,native-meson-clang,qemu-cortex-m0-cmake-gcc,qemu-cortex-m7f-cmake-gcc,qemu-riscv-rv32imac-cmake-gcc,qemu-cortex-m0-meson-gcc",
"build-types": "debug,release",
"test-actions": "prepare,build,test",
"prepare-build-test-actions3":
"{% for test-action in properties.test-actions %}echo xpm run {{ test-action }} --config {{ config }}-{{ build-type }}{{ os.EOL }}\n{% endfor %}",
},
actions: {
"test-ci3": [
"{% for config in properties.configs-ci %}{% for build-type in properties.build-types %}{{ properties.prepare-build-test-actions3 }}{% endfor %}{% endfor %}",
],
},
};
// Could add `actions` here too if needed.
const ctx = { properties: new PropertiesDrop(engine, data.properties) };
for (const [action, template] of Object.entries(data.actions)) {
const rendered = await engine.parseAndRender(template, ctx);
console.log(rendered);
}Output: |
Beta Was this translation helpful? Give feedback.
The second argument to
parseAndRender()can be an instance ofContext, which might do what you need.If I've understood your requirements correctly, I can imagine a single pass solution that involves wrapping your data (strings that contain additional markup) in a custom drop class. If your drop class has a reference to an instance of
LiquidandContext,…