Partial HAML templating in Ruby without Rails

后端 未结 5 709
Happy的楠姐
Happy的楠姐 2020-12-24 08:23

I really don’t need the overhead of Rails for my very small project, so I’m trying to achieve this just using just plain Ruby and HAML.

I want to include another HAM

5条回答
  •  庸人自扰
    2020-12-24 08:36

    I totally recommend the Tilt gem for these things. It provides a standard interface for rendering many different template langages with the same API, lets you set custom scope and locals, lets you use yield, and is robust and fast. Sinatra is using it for templates.

    Example:

    require 'haml'
    require 'tilt'
    
    template = Tilt.new('path/to/file.haml')
    # => #
    layout   = Tilt.new('path/to/layout.haml')
    
    output = layout.render { template.render }
    

    This lets you yield inside the layout to get the rendered template, just like Rails. As for partials, David already described a simple and nice way to go.

    But actually, if what you're writing is going to be served over HTTP, i suggest you take a look at Sinatra, which already provides templating, and has the simplest request routing you could imagine.

提交回复
热议问题