k0kubun / hamlit

High Performance Haml Implementation
https://rubygems.org/gems/hamlit
Other
981 stars 59 forks source link

Show basic console render example in README/similar #151

Closed 0xdevalias closed 4 years ago

0xdevalias commented 4 years ago

I had to dig a bunch to actually find how to do this, but found my snippet in:

def render_hamlit(haml, options = {})
  options = options.dup
  locals  = options.delete(:locals) || {}
  Hamlit::Template.new(options) { haml }.render(Object.new, locals)
end

So a super minimal example would be:

Hamlit::Template.new { "%strong Yay for HAML!" }.render

Even including this in https://github.com/k0kubun/hamlit/blob/master/REFERENCE.md would be good.

It seemed a lot easier to find the documentation for the equivalent haml API (linked from the README):

You can then use it by including the “haml” gem in Ruby code, and using Haml::Engine like so:

engine = Haml::Engine.new("%p Haml code!")
engine.render #=> "<p>Haml code!</p>\n"

It seems hamlit doesn't support the same sort of API (which was my initial assumption/attempt):

[38] pry(main)> engine = Hamlit::Engine.new("%strong Yay for HAML!")
NoMethodError: undefined method `to_hash' for "%strong Yay for HAML!":String
Did you mean?  to_s
from /Users/devalias/dev/foo/vendor/bundle/ruby/2.4.0/gems/temple-0.8.2/lib/temple/map.rb:87:in `validate_map!'
k0kubun commented 4 years ago

I adopted your idea in https://github.com/k0kubun/hamlit/commit/8783cef8ad11a572c688430d5b7319721052bdcc.

Hamlit::Engine implements haml's Haml::TempleEngine which can just generate a compiled Ruby code. Hamlit::Template implements tilt's Tilt::HamlTemplate.

I think Tilt is a good standard of rendering interface, and you can use it for your Ruby module usage. So I'm not going to implement Haml's original interface Haml::Engine in Hamlit.

0xdevalias commented 4 years ago

That works for me :) Thanks!