Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial HAML templating in Ruby without Rails

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 HAML file inside my HAML template. But I haven’t found a good—or really usable—way of doing this.

For example, I have these two HAML files:

documents.haml

%html
 %body
  = include(menu.haml) body
  %article …

menu.haml

%ul
 %li
  %a whatever …

Include is obviously not the way to go here. But it does a nice job describing what I’m trying to achieve in this example.

like image 981
Daniel Avatar asked Mar 25 '11 18:03

Daniel


People also ask

How do you evaluate Ruby code in Haml?

Some of the basic syntax that you can use in HAML include: Use “=” to evaluate and display Ruby code. Use “-“ to evaluate and NOT display Ruby code. Use “%” to create a tag of a particular tag.

Why do we use partial templates in rails?

This allows the standardized templates and logic to be easily shared by many individual views. The default layout in Rails is usually ‘application.html.haml’ or ‘application.html.erb’. Partial templates, also known as partials, allow you to break the rendering template into smaller, more manageable sections.

What are the different Templating engines in Ruby?

Ruby Templating Engines: ERB, HAML & Slim. ERB is a templating engine. A templating engine allows you to mix HTML & Ruby. This helps you generate your pages dynamically using data from your database. ERB is Rails default engine for rendering views.

Where can I use partial form in Ruby?

Keep in mind that it's pure Ruby, so you can use it almost everywhere. For example, we can use it to DRY up form layout definitions for several similar resources: For content that is shared among all pages in your application, you can use partials directly from layouts.


2 Answers

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')
# => #<Tilt::HAMLTemplate @file="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.

like image 167
Jostein Avatar answered Oct 06 '22 03:10

Jostein


I've done this before, just for a quick-and-dirty template producer. The easiest way is to just render the HAML inside the parent object:

%p some haml that's interesting
= Haml::Engine.new('%p this is a test').render
%p some more template

You'll more than likely want to build some methods to make this easier--a couple of helper methods. Maybe you write one called render_file that takes a filename as an argument. That method might look something like:

def render_file(filename)
  contents = File.read(filename)
  Haml::Engine.new(contents).render
end

Then, your template would look more like:

%p some template
= render_file('some_filename.haml')
%p more template

Note, you will probably need to pass self to the original Haml::Engine render so that it knows how to find your render_file method.

like image 24
David Richards Avatar answered Oct 06 '22 04:10

David Richards