Project

liquidize

0.0
No commit activity in last 3 years
No release in over 3 years
Ruby library that adds Liquid template language support to your project.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 4.0.0
>= 0
>= 0

Runtime

~> 3.0.6
 Project Readme

Liquidize

Build Status Code Climate

Ruby library that adds Liquid template language support to your project.

Installation

Add this line to your application's Gemfile:

gem 'liquidize'

And then execute:

$ bundle

Usage

PORO

Include Liquidize::Model mixin to your model and specify the liquidized attribute:

class Page
  include Liquidize::Model
  attr_accessor :body
  liquidize :body
end

Now you can set the body and render it with any options:

page = Page.new
page.body = 'Hello, {{username}}!'
page.render_body(username: 'John') # => "Hello, John!"

Liquid works much faster if once parsed template is cached. Just add liquid_* pair attribute and Liquidize will use it to store dump of parsed template.

class Page
  # ...
  attr_accessor :body, :liquid_body
  # ...
end

With ActiveRecord

Liquidize works the same way with ActiveRecord models. The only difference is that parsed template dump will be automatically saved before rendering if your model responds to liquid_* attribute.

rails g model Email message:text liquid_message:text
rake db:migrate

Liquidize it:

class Email < ActiveRecord::Base
  include Liquidize::Model
  liquidize :message
end

Use it the same way:

email = Email.create(message: 'How are you doing, {{who}}?')
email.render_message(who: 'friend') # parses template before rendering
# => "How are you doing, friend?"

# Now it won't parse template until message will be changed
reloaded_email = Email.find(email.id)
reloaded_email.render_message(who: 'sir') # does not parse it. Even after reload.
# => "How are you doing, sir?"

reloaded_email.message = 'Oops, I changed the message!'
reloaded_email.render_message # parses template again
# => "Oops, I changed the message!"

It makes record invalid if there are any syntax errors in the liquid template:

email.message = 'Hey, {{username, I think, there is an error.'
email.valid? # => false

email.message = 'Hey, {{username}}, everything is ok now.'
email.valid? # => true

Contributing

  1. Fork it (https://github.com/ImmaculatePine/liquidize/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request