https://github.com/gbh/liquid_tag_with_params
Liquid Markup Tag that accepts parameters
https://github.com/gbh/liquid_tag_with_params
liquid liquid-templating-engine ruby
Last synced: about 1 year ago
JSON representation
Liquid Markup Tag that accepts parameters
- Host: GitHub
- URL: https://github.com/gbh/liquid_tag_with_params
- Owner: GBH
- License: mit
- Created: 2017-09-14T23:02:13.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-23T16:42:20.000Z (over 8 years ago)
- Last Synced: 2025-03-11T02:46:23.236Z (over 1 year ago)
- Topics: liquid, liquid-templating-engine, ruby
- Language: Ruby
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Liquid Tag With Params
[](http://rubygems.org/gems/liquid_tag_with_params) [](http://rubygems.org/gems/liquid_tag_with_params) [](https://travis-ci.org/GBH/liquid_tag_with_params)
This is a `Liquid::TagWithParams` that extends `Liquid::Tag` by adding support for, you guessed it, params.
## Install
Add gem to your Gemfile:
```ruby
gem 'liquid_tag_with_params', '~> 0.0.1'
```
## Usage
This gem is primarily for extending Liquid [by creating your own tags](https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers#create-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:
```ruby
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)
```
```ruby
@template = Liquid::Template.parse("{% shuffle 1, 2, 3, 4, times: 5 %}")
@template.render # => "2, 1, 3, 4"
```
---
Copyright 2017, Oleg Khabarov