How to handle variables and string literals as custom tag parameters
See original GitHub issueHi, 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: {% hello_world "foo" %}
(name is string “foo”, optional code is empty) or {% assign bar = "bar" %} {% hello_world bar "code1" %}
(name is variable bar
, optional code is string)
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 name
or code
parameters as either variable names or fixed strings while also making the code
parameter optional?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Template literals (Template strings) - JavaScript | MDN
Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values....
Read more >Combining String Literals And Variables In PHP - Droptica
In PHP we have several options for specifying strings, but most of us are only familiar with defining string literals using quotes.
Read more >Using Variables, Expressions, Wildcards, and String ...
Expressions enable you to dynamically construct server application function (SAF) parameters and to select which SAFs to execute on a request-by-request basis.
Read more >Understanding Template Literals in JavaScript - DigitalOcean
Use the tag function as the tagged template function and parse the string as follows: const string = tag`This is a string with...
Read more >How do i create custom tags for taking the input string in ...
How do i create custom tags for taking the input string in command line and store them in a variable in Python ·...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is an example of a basic tag structure with 2 helper functions. Does this look like the correct way to you?
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!