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 byVersion history
Version 2.2.0 was released in July 2009 with support for Ruby 1.9 andFeatures
Four principles were involved in development of Haml.User-friendly markup
DRY
Markup language should adhere to the Don't repeat yourself ( DRY) principle. It should: * Avoid unnecessary repetitions * Focus on clean codeWell-indented
Markup language with goodClear 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.
representation for classes as CSS does, making it easy for developers to use this markup.
"Hello, World!"
Haml as a command-line tool
The following are equivalent as HAML recognises CSS selectors: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 ofhaml
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 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 tem ...
(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 tem ...
* 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
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone techno ...
-like.
* Haml uses indentation
__FORCETOC__
In the written form of many languages, an indentation or indent is an empty space at the beginning of a line to signal the start of a new paragraph. Many computer languages have adopted this technique to designate "paragraphs" or o ...
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 sa ...
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 we ...
and Merb
Merb is a discontinued model–view–controller web framework in Ruby, notable as a precursor to Rails 3. It brought increased focus on speed and modularity to Rails 3. The name Merb is a contraction of "Mongrel" and "Erb".
Precursor to and m ...
, 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_.html" ;"title="/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 tem ...
* Markaby
Markaby is a small Ruby library for writing HTML code in pure Ruby. It is an alternative to templating languages such as ERb and HAML which combine Ruby code with some form of markup. It was developed by the computer programmer " why the lucky ...
* 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 sa ...
* 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 we ...
* 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 Exte ...
* Sass – a similar system for CSS
Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone techno ...
, also designed by Catlin.
* Website Meta Language
Website Meta Language (WML) and its associated command wmk are together a free and extensible web designer's off-line HTML generation toolkit for Unix, distributed under the GNU General Public License (GPL v2). It works as an off-line content ...
– another template language with similar functionalities
* Web template
A web template system in web publishing lets web designers and developers work with ''web templates'' to automatically generate custom web pages, such as the results from a search. This reuses static web page elements while defining dynamic el ...
– 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 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 implementation is now produced by The PHP Group. ...
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