No release in over 3 years
Low commit activity in last 3 years
Send html-like attributes along with your Liquid tag and parse them to get a hash back
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 3.0
 Project Readme

Liquid::Tag::AttributeParser

Gem Maintainability Test Coverage GitHub

Liquid::Tag::AttributeParser allows you to send familiar html-like attributes along with your Liquid tags to receive back a more manageable Ruby hash with all the given information.

Installation

Add this line to your application's Gemfile:

gem 'liquid-tag-attribute_parser'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install liquid-tag-attribute_parser

Usage

Three easy steps:

  1. Take the raw argument data you get from the Liquid Tag
  2. Use Liquid::Tag::AttributeParser to parse the raw data
  3. Get back the data on a Hash format

Working example for Jekyll tags:

require "liquid/tag/attribute_parser"

module Jekyll
  class GreetingTag < Liquid::Tag
    def initialize(_tag_name, text, _tokens)
      super
      @attributes = Liquid::Tag::AttributeParser.new(text).attributes
    end

    def render(_context)
      "Hello! My name is #{@attributes[:name]} and I'm #{@attributes[:age]}"
    end
  end
end

Liquid::Template.register_tag('greeting', Jekyll::GreetingTag)
{% greeting name="Jekyll" age=13 %}

Supported data types

String

Liquid::Tag::AttributeParser.new('string="This is a string"').attributes

# => {:string=>"This is a string"}

Boolean

Liquid::Tag::AttributeParser.new('boolean=true').attributes

# => {:boolean=>true}
Liquid::Tag::AttributeParser.new('boolean=false').attributes

# => {:boolean=>false}

Integer

Liquid::Tag::AttributeParser.new('integer=123').attributes

# => {:integer=>123}
Liquid::Tag::AttributeParser.new('integer=+123').attributes

# => {:integer=>123}
Liquid::Tag::AttributeParser.new('integer=-123').attributes

# => {:integer=>-123}

Float

Liquid::Tag::AttributeParser.new('float=1.23').attributes

# => {:float=>1.23}
Liquid::Tag::AttributeParser.new('float=+1.23').attributes

# => {:float=>1.23}
Liquid::Tag::AttributeParser.new('float=-1.23').attributes

# => {:float=>-1.23}
Liquid::Tag::AttributeParser.new('float=123.0').attributes

# => {:float=>123.0}
Liquid::Tag::AttributeParser.new('float=+123.0').attributes

# => {:float=>123.0}
Liquid::Tag::AttributeParser.new('float=-123.0').attributes

# => {:float=>-123.0}
Liquid::Tag::AttributeParser.new('float=.123').attributes

# => {:float=>0.123}
Liquid::Tag::AttributeParser.new('float=+.123').attributes

# => {:float=>0.123}
Liquid::Tag::AttributeParser.new('float=-.123').attributes

# => {:float=>-0.123}

Combination of different data types

Liquid::Tag::AttributeParser.new('string="This is a string" boolean=true integer=-123 float=.123').attributes

# => {:string=>"This is a string", :boolean=>true, :integer=>-123, :float=>0.123}

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/unabris/liquid-tag-attribute_parser.

License

The gem is available as open source under the terms of the MIT License.