I really like Markaby for HTML templates. I think it’s especially good if Ruby programmers are creating and updating the views. If you have to get a designer involved (i.e. non Rubyist) maybe ERB or Haml would be better, but, otherwise it’s great, because everything is Ruby.

So, I started out trying to get Markaby working with Rails 2.1.0. With some hours of confusion as to why it didn’t work and digging into Rails and Markaby code, I came up with the following two hacks which patch up Markaby to work with changes in the Rails rendering code.

In vendor/plugins/markaby/init.rb, change

ActionView::Base::register_template_handler 'mab',  Markaby::Rails::ActionViewTemplateHandler

to

ActionView::Template::register_template_handler 'mab',  Markaby::Rails::ActionViewTemplateHandler

Create config/initializers/markaby_fixup.rb:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# see http://onemanswalk.com/work/2007/12/13/using-markaby-w-rails-2-0-1/
ActionView::Base
class ActionView::Base
  def string_path(string)
    string
  end
end

Markaby::Rails::ActionViewTemplateHandler
class Markaby::Rails::ActionViewTemplateHandler
  def compilable?; false; end

  def render(template)
    assigns = @action_view.instance_vars.to_h.merge(template.locals)
    builder = Markaby::Rails::Builder.new(assigns, @action_view)
    eval_args = [template.source, template.filename].compact
    builder.capture { instance_eval(*eval_args) }
  end
end

So this was working fine until I updated to Rails 2.1.1, at which time I got several errors like the following:

undefined method `/people/1320/edit_path' for #<ActionView::Base:0x3d0a090>
On line #26 of people/show.html.mab

    23:   text h(@person.maiden_name)
    24: end
    25: 
    26: link_to 'Edit', edit_person_path(@person)
    27: link_to 'Back', people_path

    vendor/plugins/markaby/lib/markaby/builder.rb:166:in `send'
    config/initializers/markaby_fixup.rb:26:in `method_missing'
    app/views/addresses/new.html.mab:30:in `render'
    vendor/plugins/markaby/lib/markaby/builder.rb:105:in `instance_eval'
    vendor/plugins/markaby/lib/markaby/builder.rb:105:in `capture'
    config/initializers/markaby_fixup.rb:17:in `render'
    spec/views/addresses/new.html.mab_spec.rb:18

I’ll keep digging to figure out how to fix my fix so I can continue to use Markaby, but really what I’m wondering is — am I the only one trying to us Markaby with recent versions of Rails? I found a relevant bug report or two, but no apparent action to fix them. I’m a little worried about the health of Markaby in relation to Rails.

Anyway, I’ll keep plugging and report back here if I work out anything.

2 Responses to “Markaby and Rails — 2.1.x woes”

  1. David Anderson Says:

    There was also some discussion of this on the rails trac: http://dev.rubyonrails.org/ticket/10543

    If you use the patch in ticket 58, does the problem you see in 2.1.1 still occur?

    Good luck getting this figured out. It is unfortunate that no one seems to be maintaining markaby right now.

  2. Jason Says:

    There is a git project for it:

    http://github.com/dinsley/markaby/tree/master

    I haven’t rolled this in yet as we’re still on 2.0.2. Probably try in the next week or so.

Sorry, comments are closed for this article.