paggio icon indicating copy to clipboard operation
paggio copied to clipboard

No, I actually hate instance_eval, but it can be done better

Open czaks opened this issue 10 years ago • 3 comments

How about supporting a theoretical code like this?

class Page
  include Paggio::HTML

  def initialize(title, content)
    @title   = title
    @content = content
  end

  def to_html
    html do
      head do
        title @title
      end

      body do
        @content
      end
    end
  end
end

puts Page.new("foo", "bar").to_html

What I done here, is include a mixin, which moves all HTML tag methods into the class context. Sure, we would need to be extra careful not to override those. This way, it would encourage object-oriented programming with no problems related to instance_eval use.

This is how https://github.com/erector/erector works, afaik.

czaks avatar Mar 31 '15 02:03 czaks

instance_eval is optional, if you pass a parameter to the first block it will call it instead of using instance_eval.

a = "hue"

Paggio.html do |_|
  _.div a
end

The above will work.

meh avatar Mar 31 '15 17:03 meh

I know, I proposed an alternative syntax, which is cleaner and doesn't need instance_eval at all.

I am recently searching for a robust markaby-like template library that would work both in opal and ruby for my bigger project. Your code seems slick and clean and is already used by opal-browser, that's why I am trying to start talk here. 31 mar 2015 19:04 "meh." [email protected] napisał(a):

Closed #5 https://github.com/meh/paggio/issues/5.

— Reply to this email directly or view it on GitHub https://github.com/meh/paggio/issues/5#event-269790163.

czaks avatar Apr 01 '15 02:04 czaks

The problem is that there are no HTML methods, it's all method_missing based, which would end up making the class very hard to debug if the mixin defined a method_missing.

meh avatar Apr 01 '15 10:04 meh