Replies: 2 comments 5 replies
-
See this for example: https://github.com/harttle/liquidjs/blob/master/src/tags/capture.ts |
Beta Was this translation helpful? Give feedback.
3 replies
-
Here's an example of a custom See also:
And, @harttle, I think this page is broken: import { evalToken, Liquid, Tag, Tokenizer } from "liquidjs";
// This is specifically for whitespace separated key/value arguments.
// I don't think LiquidJS exports it.
const BLANK = 4;
class WithTag extends Tag {
/** @type Map<string, ValueToken> */
arguments;
/** @type Template[] */
children;
constructor(token, remainTokens, liquid) {
super(token, remainTokens, liquid);
this.arguments = this._parseArguments(token);
this.children = [];
const stream = this.liquid.parser
.parseStream(remainTokens)
.on("tag:endwith", () => stream.stop())
.on("template", (t) => this.children.push(t))
.on("end", () => {
throw new Error(`tag ${token.getText()} not closed`);
});
stream.start();
}
_parseArguments(tagToken) {
/** @type Map<string, ValueToken> */
const args = new Map();
const tokens = new Tokenizer(tagToken.args);
while (true) {
tokens.skipBlank();
if (tokens.end()) break;
let [key, value] = this._parseArgument(tokens);
args.set(key, value);
tokens.assert(
tokens.peekType() & BLANK || tokens.end(),
"expected space separated arguments"
);
}
return args;
}
_parseArgument(tokens, delimiter = ":") {
// Read the argument's name or key
const key = tokens.readIdentifier();
tokens.assert(key.size(), "expected key/value pairs");
tokens.skipBlank();
// Check for key/value delimiter
tokens.assert(
tokens.peek() === delimiter,
`expected key/value arguments delimited with ${delimiter}`
);
++tokens.p;
tokens.skipBlank();
// Read the argument's value.
const value = tokens.readValue();
tokens.assert(value, "expected key/value pairs");
return [key.content, value];
}
*render(ctx, emitter) {
const scope = {};
for (const [k, v] of this.arguments) {
scope[k] = yield evalToken(v, ctx);
}
ctx.push(scope);
yield this.liquid.renderer.renderTemplates(this.children, ctx, emitter);
ctx.pop();
}
}
const liquid = new Liquid();
liquid.registerTag("with", WithTag);
const templateContext = {
you: "world",
};
const template = `\
Hello, {{ you }}!
{% with you:"Sue" foo:"Liquid" %}
{% if true %}
Greetings, {{ you }}!
Greetings, {{ foo }}!
{% endif %}
{% endwith %}
Goodbye, {{ you }}!
Goodbye, {{ foo }}!
`;
const result = liquid.parseAndRenderSync(template, templateContext);
console.log(result); Output
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How can I render and make sure anything within is executed as design
e.g.
{% mycustomtag %}{% for item in items %}
{{ item.property }}
{% endfor %}{% endmycustomtag %}or is there any documentation for this I missed out?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions