Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Rails plugin for binding html to models
tree: e4afa0bf3a

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
tasks
README.textile
Rakefile
init.rb
install.rb
uninstall.rb

README.textile

ModelBasedHtml

This plugin enables the binding of other html elements to models, comparable
with form_for. The idea is that you can bind common html-elements to an object
or collection and that you can easily view the data. At the moment, it includes
definition lists and tables. For any suggestions, please send me a message.

This plugin works best with Haml,
which I strongly recommend. This plugin works fine with ERB though.

This plugin requires Rails 2.2 or higher!

Definition Lists

I’ll start with an example, which explains the basic methods you can use:

- definition_list_for @user do |dl| - dl.dt_and_dd :login do |login| = link_to(login, @user) - dl.dt_and_dd_h :name - dl.dt :email - dl.dd :email do |mail_address| = mail_to mail_address - dl.dt ‘some other value’ - dl.dd_h :signature

The dt method will automatically display the human_attribute_name of this
attribute (and thus translate it), the dd will automatically show the value
of this attribute. You can pass a block to these methods to play with the value
even more, like making it a link. Because these values may be harmful, a dd_h
method is also available, sanitizing html automatically. You can limit it to
one line, using dt_and_dd or dt_and_dd_h.

Tables

A similar option is available for tables.

- table_for @users do |t| - t.head do - t.th :login - t.th :email - t.th :signature - t.body :sanitize => true - t.when_empty do |message| %em= message - t.when_not_empty do - t.tr do - t.td(nil, :colspan => t.width) do %em An extra row

By not specifying a block to t.body, it automatically loops all records,
using the columns specified in the t.head. Setting :sanitize to true will
automatically escape all html in the tds, but only when no block has been
specified.

With t.when_empty you can specify what will be placed in stead of the table
when the collection was empty. If you don’t specify anything, not even a block
it will render a default text, like “no entries found”. This is a translate
call, trying these scopes for the type:

  1. entries.controller_name
  2. entries.common

And these scopes for the rest of the method (enabling interpolation of the
above):

  1. tables.nil_class.empty_table

If the collection was empty, it doesn’t render a table at all. You can still
force a table to render, so you can fill it with your own data. Fill the :force
option with the class you’re using.

- table_for @users, :force => User do |t| - t.head do - t.th :login - t.when_empty

Now the when_empty renders a td with a colspan to match the width of the table.
The default translation scope of the message will be:

  1. tables.user.empty_table

The model interpolation will be the human_name (thus translated) of the class.

If you do specify a block to t.body, you can define each cell, just as you
would do with the definition list. It automatically loops your data. The tr
will get a odd or even class automatically.

- table_for @users do |t| - t.head do - t.th :login - t.th :email - t.th :signature - t.th ‘Actions’ - t.body do |user| - t.tr do - t.td :login - t.td :email do |email| = mail_to(email) - t.td_h :signature - t.td do = link_to(‘delete’, user, :method => :delete)

Nota bene

This plugin is still in development and is in now way considered to be used in
production. Please take care! If you’d like to contribute, please contact me!

Copyright © 2008 Iain Hecker, released under the MIT license

Something went wrong with that request. Please try again.