question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle variables and string literals as custom tag parameters

See original GitHub issue

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: {% 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:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
amit777commented, Jan 21, 2022

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}`;
  }
});

0reactions
harttlecommented, Jan 22, 2022

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 and Word, Liquid value can also be Number(23) or PropertyAccess(foo.bar), you can make use of tokenizer.readValue() and evalToken() for this.

Hope it helps!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found