The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Markaby for Rails

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Markaby for Rails Posted: Jan 12, 2006 12:12 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Markaby for Rails
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

You know that WebPage code I’ve been playing with lately? Tim Fletcher e-mailed me a revamped version, a plugin for Rails. We’ve gone back and forth on this and found a very satisfying resting point, which I’m calling Markaby. Markup as Ruby.

To install in your Rails app:

 script/plugin install http://code.whytheluckystiff.net/svn/markaby/trunk

To use it, add templates with a .mab extension.

Examples

For example, an app/views/layout/application.mab could look like:

 html do
   head do
     title action_name
     stylesheet_link_tag 'scaffold'
   end

   body do
     p flash[:notice], :style => "color: green" 
     self << @content_for_layout
   end
 end

As you can see all the normal helpers and variables are present. There is one caveat: in default Markaby, helper methods are automatically output when called. So, in the above, the stylesheet_link_tag gets output. (To turn that off, use @output_helpers = false.)

A scaffolding page could look like this (app/views/products/list.mab):

 h1 'Listing products'

 table.editor.classic do 
   tr do
     for column in Product.content_columns
       th column.human_name
     end
   end 

   for product in @products
     tr do
       for column in Product.content_columns
         td product.send(column.name)
       end
       td { link_to 'Show', :action => 'show', :id => product }
       td { link_to 'Edit', :action => 'edit', :id => product }
       td { link_to 'Destroy', { :action => 'destroy', :id => product }, :confirm => 'Are you sure?' }
     end
   end
 end

 link_to 'Previous page', { :page => @product_pages.current.previous } if @product_pages.current.previous
 link_to 'Next page', { :page => @product_pages.current.next } if @product_pages.current.next

 br

 link_to 'New product', :action => 'new'

As you can see classes can be assigned just like in previous experiments. The table.editor.classic gets output as

.

One really wonderfully trippy thing about Markaby is that you can use capture to treat elements as strings, if you like. This satisfies one of Dgtized’s feature requests from a few days ago.

 div.menu do
   %w[5.gets bits inspect cult -h].map do |m|
     capture { link_to m, "/#{m}" }
   end.join(" | ")
 end

So, in the above example, the output of link_to is captured for each element of the array and then patched together with pipes between. But how does it get output to div.menu?

The rule is: each element with a block opens a new buffer. You can fill up that buffer by printing elements to it. Or by returning a string from the block.

Markup Shortcuts

Really, there’s not much more to this at present. For example, RedCloth support isn’t built in, nor is the auto-header and auto-body stuff from WebPage.

Just a few shortcuts:

  1. img tags will default to :alt => '', :border => 0.
  2. head automatically includes tag!(:meta, 'http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8').
  3. html defaults to an xml instruction, the XHTML 1.0 Transitional doctype and :xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", :lang => "en" on the html element.
  4. xhtml_transitional is an alias for html.
  5. xhtml_strict does the same, but with the proper XHTML 1.0 Strict doctype and so on.

If you don’t like any of this, override or undef the methods. So far so good?

Lastly, importantly: Rails 1.0 doesn’t support using Markaby for layouts unless you explicitly state the name of your layout in the controller. But I think Marcel has fixed this in Edge Rails.

Read: Markaby for Rails

Topic: Did you hire a person or a product? Previous Topic   Next Topic Topic: Taught Io by Steve Dekorte at Ritual Today

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use