-
Hi, I have a custom tag where I want to be able to have an optional parameter and I want to be able to pass in either a string or variable name. For eg: I am able to accomplish the second one using this code: liquid.registerTag('hello_world', {
parse: function (tagToken, remainTokens) {
const tokenizer = new Tokenizer(tagToken.args, this.liquid.options.operatorsTrie);
this.name = tokenizer.readIdentifier().content;
this.code = tokenizer.readIdentifier().content;
if (!this.name) throw new Error('reject tag requires a name parameter');
},
render: async function (scope, hash) {
let name = await this.liquid.evalValue(this.name, scope);
let code = await this.liquid.evalValue(this.code, scope);
return `HELLO name=${name},code=${code}`;
}
}); How would I modify the above custom tag to be able to accept the |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 1 reply
-
Try make use of Hash object or readHash method in tokenizer. You can refer
to the implementation of include tag.
|
Beta Was this translation helpful? Give feedback.
-
Thank you, I will play around with that. I tried reading the code at https://github.com/harttle/liquidjs/blob/master/src/builtin/tags/include.ts but it's not clear to me how to use the hash methods properly in my case. |
Beta Was this translation helpful? Give feedback.
-
Hi @harttle I've tried to do something more simple but I don't seem to understand how to make the tokenizer work correctly. I was wondering if you could help me make the tokenizer read 2 parameters on the custom tag? Each parameter can be either an identifier or a string literal. Doesn't need to be working code, but just a very barebones example. So if the tag is I've tried readHashes(), readQuoted(), readTopLevelTokens(), readIdentifier(), readValue() |
Beta Was this translation helpful? Give feedback.
-
@amit777 I can make a basic tutorial for this, hopefully we can have a set of tutorials for custom filters and tags eventually. |
Beta Was this translation helpful? Give feedback.
-
Thank you! I think I may have figured out a solution and will post the code in the morning. I'm curious to see if you think I've done it correctly. |
Beta Was this translation helpful? Give feedback.
-
Here is an example of a basic tag structure with 2 helper functions. Does this look like the correct way to you? const { Liquid, Tokenizer, evalQuotedToken, TokenKind } = require('liquidjs');
const liquid = new Liquid({jsTruthy: true});
function _getLiquidToken (tokenizer) {
const wordToken = tokenizer.readIdentifier();
if (wordToken.content) return wordToken;
const quotedToken = tokenizer.readQuoted()
if (quotedToken) return quotedToken;
}
async function _getLiquidTokenValue(token, ctx, scope) {
let ret;
if(token?.kind == TokenKind.Quoted) { //1024
logger.debug(`token is quoted`);
ret = evalQuotedToken(token);
}
else if(token?.kind == TokenKind.Word) { //256
ret = await ctx.evalValue(token?.content, scope);
}
return ret;
}
liquid.registerTag('hello_world', {
parse: function (tagToken, remainTokens) {
const tokenizer = new Tokenizer(tagToken.args, this.liquid.options.operatorsTrie);
this.foo = _getLiquidToken(tokenizer);
if(!this.foo) throw new Error('reject requires a foo parameter');
this.bar = _getLiquidToken(tokenizer);
},
render: async function (scope, hash) {
let foo = await _getLiquidTokenValue(this.foo, this.liquid, scope);
let bar = await _getLiquidTokenValue(this.bar, this.liquid, scope);
return `foo=${foo},bar=${bar}`;
}
}); |
Beta Was this translation helpful? Give feedback.
-
Hey @amit777 , I created a tutorial here: https://liquidjs.com/tutorials/parse-parameters.html Glad to know the above snippet works for your case. Apart from Hope it helps! |
Beta Was this translation helpful? Give feedback.
-
Hi @harttle apologies, I was looking at some of our tests around liquidjs. I'm not sure when they stopped working, but is |
Beta Was this translation helpful? Give feedback.
-
Hi, so I've been digging deeper, and a change in 9.42.0 seems to have affected how |
Beta Was this translation helpful? Give feedback.
Hey @amit777 , I created a tutorial here: https://liquidjs.com/tutorials/parse-parameters.html
Glad to know the above snippet works for your case. Apart from
Quoted
andWord
, Liquid value can also beNumber
(23
) orPropertyAccess
(foo.bar
), you can make use oftokenizer.readValue()
andevalToken()
for this.Hope it helps!