public
Last active

Jekyll JSON Filter

  • Download Gist
README.md
Markdown

This is a plugin meant for Jekyll.

This filter is described at the Shopify Liquid wiki, but somehow didn't make it into the general repository.

Just plop this guy in your _plugins directory and use it like this:

var title = {{ page.title | json }};

Easy!

json.rb
Ruby
1 2 3 4 5 6 7 8 9 10
require "rubygems"
require "json"
 
module JsonFilter
def json(input)
input.to_json
end
Liquid::Template.register_filter self
end

Shouldn't this work the same without requiring Rails?

require 'json'

module JsonFilter
  def json(input)
    input.to_json
  end

  Liquid::Template.register_filter self
end

Yes, that would work a lot better wouldn't it? I'm pretty new to Ruby, so I really appreciate the input! I also like how you put the register_filter statement inside the module and excluded the unnecessary module Jekyll...

Ideally, I'd put it in my application's own module, but I didn't want to make something up off the cuff, so there we are. I figured out the "register the module in the module block" trick pretty quickly because I'm lazy and hate retyping anything longer than five characters!

You can even do this with an anoymous module, but I think that might be getting a little nasty. I'll leave figuring that one out up to you, if you really want to. :D

Hope you enjoy learning Ruby as much as I have. :Ɔ

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.