Skip to content

Instantly share code, notes, and snippets.

@joelverhagen
Created February 17, 2012 04:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelverhagen/1850654 to your computer and use it in GitHub Desktop.
Save joelverhagen/1850654 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

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
Copy link

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
Copy link

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