harttle / liquidjs

A simple, expressive, safe and Shopify compatible template engine in pure JavaScript.
https://liquidjs.com
MIT License
1.5k stars 238 forks source link

Add global jekyll style variable parsing #752

Open mohas opened 4 days ago

mohas commented 4 days ago

Hi, currently when we use new Hash(tagToken.args, true) we can support jekyll style variables in our custom tags, however when using built-in tags, such options is not possible for example, {% render "tmp", cur=var1 %}, as some teams have strong react background, and liquidjs Hash already support jekyll style variables, please make hash to default to a value defined in the options instead of hardcoding one:

// tokenizer.ts
readHash (jekyllStyle?: boolean): HashToken | undefined {
    this.skipBlank()
    if (this.peek() === ',') ++this.p
    const begin = this.p
    const name = this.readNonEmptyIdentifier()
    if (!name) return
    let value

    this.skipBlank()

    const sep = jekyllStyle ? '=' : ':' /* Make this read from options passed to the engine */
// ex: 
//  const sep = jekyllStyle ? '=' : seperatorFromEngineOptions

    if (this.peek() === sep) {
      ++this.p
      value = this.readValue()
    }
    return new HashToken(this.input, begin, this.p, name, value, this.file)
  }

by making this new seperatorFromEngineOptions default to ':' I think backward compatibility will be preserved.

harttle commented 23 hours ago

Please try setting keyValueSeparator option to =.