Project

mold

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Mold is a view template handler for Rails 3+ that renders Ruby objects to JSON.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Overview

Mold is a view template handler for Rails 3+ that renders Ruby objects to JSON.

Compatibility

Mold is compatible with Rails Rails 3.x.

Installation

Install Mold as a gem:

gem install mold

or add to your Gemfile:

gem 'mold'

Usage

It's pretty straight-forward to create a Mold JSON template, just create the structure as you would in a Hash:

# app/controllers/users_controller.rb
class Users < ActionController:Base
  def show
	  @user = User.first
  end
end

# app/views/users/show.mold
{
	:user => {
		:id   => @user.id,
		:name => @user.name
	}
}

You can also create and use view partials in Mold:

# app/controllers/users_controller.rb
class Users < ActionController:Base
  def index
	@users = User.all
  end
end

# app/views/users/_user.json.mold
{
	:user => {
		:id   => user.id,
		:name => user.name
	}
}

# app/views/users/index.json.mold
{
	:users => @users.map{ |user| render(:partial => 'user', :object => user) }
}

Writing code in a Mold view also works:

# app/views/users/show.json.mold
time = Time.now

{
	:user => {
		:time => time
	}
}

Inspiration

This gem has been inspired mostly by ruby_template_handler.

Copyright

Copyright © 2011 Thiago Jackiw. See License.txt for details.