Halloween
Tuesday, October 31, 2006
Happy Halloween, glorious readers! I'm going to a party this evening... I'm very excited.
History
Haml was originally introduced by Hampton Catlin with its initial release in 2006 and his work was taken ahead by a few other people. His motive was to makeVersion history
Version 2.2.0 was released in July 2009 with support for Ruby 1.9 and Rails 2.0 or above. Version 3.0.0 was released in May 2010, adding support for Rails 3 and some performance improvements. The fourth major version broke compatibility with previous versions, only supporting Rails 3 and Ruby 1.8.7 or above, and marked the switch to semantic versioning. Several amendments like increasing the performance, fixing a few warnings, compatibility with latest versions of Rails, fixes in the documentation and many more were made in the Haml 4 series. Version 5.0.0 was released in April 2017. It supports Ruby 2.0.0 or above and drops compatibility with Rails 3. A ' trace' option, which helps users to perform tracing on Haml template, has been added.Features
Four principles were involved in development of Haml.User-friendly markup
DRY
Markup language should adhere to the Don't repeat yourself (Well-indented
Markup language with good indentation improves appearance, makes it easy to read for readers and also to determine where a given element starts and ends.Clear structure
Markup language with a clear structure will help in code maintenance and logical understanding of final result. It is unclear whether Haml offers any differential advantage in this regard.Examples
Haml markup is similar to CSS in syntax. For example, Haml has the same dot.
representation for classes as CSS does, making it easy for developers to use this markup.
"Hello, World!"
Haml as a
Hello, World!
Haml as an add-on for
Gemfile
should include this line:
gem 'haml'
Similar to app/controllers/messages_controller.rb
app/views/messages/index.html.haml
Hello, World!
Haml as a Ruby module
To use Haml independent of Rails anhaml
Gemfile
and simply import sage: require 'haml'/code> it in Ruby script or invoke Ruby interpreter with -rubygems
flag.
welcome = Haml::Engine.new("%p Hello, World!")
welcome.render
Output:
Hello, World!
Haml::Engine
is a Haml class.
Basic example
Haml uses whitespace indentation (two spaces) for tag nesting and scope. This acts as a replacement for the open-end tag pairs, making it DRY
Dry or dryness most often refers to:
* Lack of rainfall, which may refer to
** Arid regions
** Drought
* Dry or dry area, relating to legal prohibition of selling, serving, or imbibing alcoholic beverages
* Dry humor, deadpan
* Dryness (medica ...
and cleaner. The following example compares the syntaxes of Haml and eRuby
Embedded Ruby (also shortened as ERB) is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP and JSP, and PHP and other server-side scripting languages. The templ ...
(Embedded Ruby), alongside the HTML output.
Key differences are:
* Haml doesn't have both start and end for each element like eRuby
Embedded Ruby (also shortened as ERB) is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP and JSP, and PHP and other server-side scripting languages. The templ ...
* eRuby syntax looks a lot like HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
and is thereby more HTML-like while Haml is more CSS-like.
* Haml uses indentation to nest tag elements whereas eRuby uses the same HTML representation
* In Haml properties like class
, id
can be represented by .
, #
respectively instead of regular class
and id
keywords. Haml also uses %
to indicate a HTML element instead of <>
as in eRuby.
Example with embedded Ruby code
Note: This is a simple preview example and may not reflect the current version of the language.
!!!
%html
%head
%title BoBlog
%meta
%link
%body
#header
%h1 BoBlog
%h2 Bob's Blog
#content
- @entries.each do , entry,
.entry
%h3.title= entry.title
%p.date= entry.posted.strftime("%A, %B %d, %Y")
%p.body= entry.body
#footer
%p
All content copyright © Bob
The above Haml would produce this XHTML
Extensible HyperText Markup Language (XHTML) is part of the family of XML markup languages. It mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated.
While HTML, prior ...
:
BoBlog
BoBlog
Bob's Blog
Halloween
Tuesday, October 31, 2006
Happy Halloween, glorious readers! I'm going to a party this evening... I'm very excited.
New Rails Templating Engine
Friday, August 11, 2006
There's a very cool new Templating Engine out for Ruby on Rails. It's called Haml.
Implementations
The official implementation of Haml has been built for Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
with plugins for Ruby on Rails
Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...
and Merb, but the Ruby implementation also functions independently. Haml can be easily used along with other languages. Below is a list of languages in which Haml has implementations:
* Ruby
hamlit
* PHP
Fammel
pHAML
phamlp
phpHaml
(PHP 5)
HAML-TO-PHP
(PHP 5)
Multi target HAML
(PHP 5.3)
* Javascript
haml-js
* Python
HamlPy
* Common Lisp
cl-haml
* Dart
Hart
* Java
JHaml
* Lua
LuaHaml
* .NET
NHaml
** ASP.NET
MonoRail NHaml
* Perl
Text::Haml
* Scala
See also
* BBCode
BBCode ("Bulletin Board Code") is a lightweight markup language used to format messages in much Internet forum software, first introduced in 1998. The available "tags" of BBCode are usually indicated by square brackets ( and ">/code> and /code> ...
* eRuby
Embedded Ruby (also shortened as ERB) is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP and JSP, and PHP and other server-side scripting languages. The templ ...
* Markaby
* Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
* Ruby on Rails
Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...
* YAML
YAML ( and ) (''see '') is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Ext ...
* Sass
Sass, Saß or SASS may refer to:
SASS
* M110 Semi-Automatic Sniper System (or M110 SASS)
* Safe Amplification Site Society, a non-profit organization that promotes music and the arts within Vancouver, British Columbia, Canada
* Shanghai Academ ...
– a similar system for CSS, also designed by Catlin.
* Website Meta Language – another template language with similar functionalities
* Web template – general concept of template to HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
expansion
References
External links
*
*
Haml tutorial
Learn Haml (basic)
* {{GitHub, haml/haml, Haml
phpHaml
Haml implementation for PHP
PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
Haml Google Group
Haml 1.0
announcement on the official Ruby on Rails weblog
Ruby (programming language)
Template engines
Free computer libraries
Software using the MIT license
Lightweight markup languages