Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Jekyll JSON Filter

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!

require "rubygems"
require "json"
module JsonFilter
def json(input)
input.to_json
end
Liquid::Template.register_filter self
end
@erisdev
erisdev commented Feb 17, 2012

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
@joelverhagen
Owner

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...

@erisdev
erisdev commented Feb 17, 2012

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. :Ɔ

@axelheer
axelheer commented Sep 6, 2014

You can use the jsonify filter instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.