Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
/ mold Public archive

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

License

Notifications You must be signed in to change notification settings

tjackiw/mold

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages