No commit activity in last 3 years
No release in over 3 years
Tag class for Liquid markup that supports extra params
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 4.0.0
 Project Readme

Liquid Tag With Params

Gem Version Gem Downloads Build Status

This is a Liquid::TagWithParams that extends Liquid::Tag by adding support for, you guessed it, params.

Install

Add gem to your Gemfile:

gem 'liquid_tag_with_params', '~> 0.0.1'

Usage

This gem is primarily for extending Liquid by creating your own tags. Out of the box Liquid::Tag accepts a string and that's about it. Liquid::TagWithParams will take that string and will make parameters out of it so you can use them during rendering.

Here's an example of such tag:

require 'liquid/tag_with_params'

class ShuffleTag < Liquid::TagWithParams
  def initialize(tag_name, params, tokens)
     super
  end

  def render(context)
    # assuming that last params were of key: value format
    options = @params.extract_options!

    # note: keys and values are always strings
    times = options["times"] || 1

    # shuffle really hard
    times.to_i.times do
      @params.shuffle!
    end

    @params.join(', ')
  end
end

Liquid::Template.register_tag('shuffle', ShuffleTag)
@template = Liquid::Template.parse("{% shuffle 1, 2, 3, 4, times: 5 %}")
@template.render    # => "2, 1, 3, 4"

Copyright 2017, Oleg Khabarov