No commit activity in last 3 years
No release in over 3 years
Parse liquid tags easily
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 0.3
>= 3.0, < 5.0
 Project Readme

Build Status

Liquid Tag Parser

Liquid Tag parser provides a robust interface to parsing your tag syntax in a way that makes sense, it uses Shellwords, along with escapes to allow users to do extremely robust arguments, giving you back a hash, that you get to play with. It also has the concept of argv1, deep hashes, and even defaults if you give them to us.

Installation

gem "liquid-tag-parser", "~> 1.9"

Usage

Typically you would take the raw argument data you get from Liquid and ship that to us, we will parse it, and return to you the data, as a class. You can access hash keys with #args or you can access it with #[] on the class.

require "liquid/tag/parser"
class MyTag < Liquid::Tag
  def initialize(tag, args, tokens)
    @raw_args = args
    @tag = tag.to_sym
    @args = Liquid::Tag::Parser.new(
      args
    )


    @tokens = tokens
    super
  end

  def render(_ctx_)
    return "it worked" if @args[:myArg]
    "it didn't work"
  end
end
{% mytag myArg = true %}

With argv1

Liquid::Tag::Parser.new("a b=1 c=2 !false d:e:f='3 4' @true").args
# => {
#   argv1: "a",
#   false: false,
#   true: true,
#   b: "1",
#   c: "2",
#   d: {
#     e: {
#       f: "3 4"
#     }
#   }
# }

Escaping argv1

Liquid::Tag::Parser.new("'a=1'").args
# => {
#   argv1: "a=1"
# }

Without argv1

Liquid::Tag::Parser.new("a=1 b=2 !false c:d:e=3:4:5 @true").args
# => {
#   false: false,
#   true: true,
#   a: "1",
#   b: "2",
#   c: {
#     d: {
#       e: "3:4:5"
#     }
#   }
# }

With Array's

Liquid::Tag::Parser.new("a=1 a=2 a=3").args
# => {
#   a: [
#     1, 2, 3
#   ]
# }

Escaping

Liquid::Tag::Parser.new("a=1=2").args
# => {
#   "a=1": 2
# }
Liquid::Tag::Parser.new("a='1=2'").args
# => {
#   "a": "1=2"
# }

Booleans

True

Liquid::Tag::Parser.new("@true").args
# => {
#   true: true
# }
Liquid::Tag::Parser.new("@key1:key2").args
# => {
#   key1: {
#     key2: true
#   }
# }

False

Liquid::Tag::Parser.new("!false").args
# => {
#   false: false
# }
Liquid::Tag::Parser.new("!key1:key2").args
# => {
#   key1: {
#     key2: false
#   }
# }