2012-06-28 17:23:53 -04:00
|
|
|
# Haml (HTML Abstraction Markup Language)
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-26 10:33:31 -04:00
|
|
|
Haml is a markup language that's used to cleanly and simply describe the HTML of
|
|
|
|
any web document, without the use of inline code. Haml functions as a
|
2012-06-08 12:00:07 -04:00
|
|
|
replacement for inline page templating systems such as PHP, ERB, and ASP.
|
|
|
|
However, Haml avoids the need for explicitly coding HTML into the template,
|
|
|
|
because it is actually an abstract description of the HTML, with some code to
|
|
|
|
generate dynamic content.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
* Whitespace active
|
|
|
|
* Well-formatted markup
|
|
|
|
* DRY
|
|
|
|
* Follows CSS conventions
|
|
|
|
* Integrates Ruby code
|
|
|
|
* Implements Rails templates with the .haml extension
|
|
|
|
|
|
|
|
## Using Haml
|
|
|
|
|
|
|
|
Haml can be used in three ways:
|
2012-06-08 12:00:07 -04:00
|
|
|
|
|
|
|
* as a command-line tool,
|
|
|
|
* as a plugin for Ruby on Rails,
|
|
|
|
* and as a standalone Ruby module.
|
|
|
|
|
2009-06-18 16:40:57 -04:00
|
|
|
The first step for all of these is to install the Haml gem:
|
|
|
|
|
|
|
|
gem install haml
|
|
|
|
|
|
|
|
To run Haml from the command line, just use
|
|
|
|
|
|
|
|
haml input.haml output.html
|
|
|
|
|
|
|
|
Use `haml --help` for full documentation.
|
|
|
|
|
2012-06-08 11:33:47 -04:00
|
|
|
To use Haml with Rails, add the following line to the Gemfile:
|
2010-05-10 23:26:25 -04:00
|
|
|
|
|
|
|
gem "haml"
|
2009-06-25 03:28:49 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Once it's installed, all view files with the `".html.haml"` extension will be
|
|
|
|
compiled using Haml.
|
2009-06-25 03:28:49 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
You can access instance variables in Haml templates the same way you do in ERB
|
|
|
|
templates. Helper methods are also available in Haml templates. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
# file: app/controllers/movies_controller.rb
|
|
|
|
|
|
|
|
class MoviesController < ApplicationController
|
|
|
|
def index
|
|
|
|
@title = "Teen Wolf"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-08 11:33:47 -04:00
|
|
|
-# file: app/views/movies/index.html.haml
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
#content
|
|
|
|
.title
|
|
|
|
%h1= @title
|
|
|
|
= link_to 'Home', home_url
|
|
|
|
|
|
|
|
may be compiled to:
|
|
|
|
|
|
|
|
<div id='content'>
|
|
|
|
<div class='title'>
|
|
|
|
<h1>Teen Wolf</h1>
|
|
|
|
<a href='/'>Home</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
### Rails XSS Protection
|
2009-10-17 18:13:35 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Haml supports Rails' XSS protection scheme, which was introduced in Rails 2.3.5+
|
|
|
|
and is enabled by default in 3.0.0+. If it's enabled, Haml's
|
|
|
|
{Haml::Options#escape_html `:escape_html`} option is set to `true` by default -
|
|
|
|
like in ERB, all strings printed to a Haml template are escaped by default. Also
|
|
|
|
like ERB, strings marked as HTML safe are not escaped. Haml also has [its own
|
|
|
|
syntax for printing a raw string to the template](#unescaping_html).
|
2009-10-17 18:13:35 -04:00
|
|
|
|
|
|
|
If the `:escape_html` option is set to false when XSS protection is enabled,
|
2012-06-08 12:00:07 -04:00
|
|
|
Haml doesn't escape Ruby strings by default. However, if a string marked
|
|
|
|
HTML-safe is passed to [Haml's escaping syntax](#escaping_html), it won't be
|
|
|
|
escaped.
|
2009-10-17 18:13:35 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Finally, all the {Haml::Helpers Haml helpers} that return strings that are known
|
|
|
|
to be HTML safe are marked as such. In addition, string input is escaped unless
|
|
|
|
it's HTML safe.
|
2009-10-17 18:13:35 -04:00
|
|
|
|
2009-06-18 16:40:57 -04:00
|
|
|
### Ruby Module
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Haml can also be used completely separately from Rails and ActionView. To do
|
|
|
|
this, install the gem with RubyGems:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
gem install haml
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
You can then use it by including the "haml" gem in Ruby code, and using
|
|
|
|
{Haml::Engine} like so:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
engine = Haml::Engine.new("%p Haml code!")
|
|
|
|
engine.render #=> "<p>Haml code!</p>\n"
|
|
|
|
|
2009-06-19 05:20:00 -04:00
|
|
|
### Options
|
|
|
|
|
2012-06-08 11:33:47 -04:00
|
|
|
Haml understands various configuration options that affect its performance and
|
|
|
|
output.
|
2009-06-19 05:20:00 -04:00
|
|
|
|
2012-06-11 16:51:38 -04:00
|
|
|
In Rails, options can be set by setting the {Haml::Template#options Haml::Template.options}
|
2012-06-08 12:00:07 -04:00
|
|
|
hash in an initializer:
|
2012-06-08 11:33:47 -04:00
|
|
|
|
|
|
|
# config/initializers/haml.rb
|
2009-06-19 05:20:00 -04:00
|
|
|
Haml::Template.options[:format] = :html5
|
|
|
|
|
2012-06-11 16:51:38 -04:00
|
|
|
Outside Rails, you can set them by configuring them globally in
|
|
|
|
Haml::Options.defaults:
|
2012-06-08 11:33:47 -04:00
|
|
|
|
|
|
|
Haml::Options.defaults[:format] = :html5
|
|
|
|
|
|
|
|
Finally, you can also set them by passing an options hash to
|
|
|
|
{Haml::Engine#initialize}. For the complete list of available options, please
|
|
|
|
see {Haml::Options}.
|
[Haml] Add support for a workaround for fake ASCII input strings.
Closes gh-3
This is a complicated issue, but I'll do my best to explain it here.
By default, Haml encodes its templates as Encoding.default_internal,
which is usually UTF-8. This means that strings printed to the
template should be either UTF-8 or UTF-8-compatible ASCII. So far, all
well and good.
Now, it's possible to have strings that are marked as ASCII-8bit, but
which aren't UTF-8 compatible. This includes valid UTF-8 strings that
are forced into an ASCII-8bit encoding. If one of these strings is
concatenated to a UTF-8 string, Ruby says "I don't know what to do
with these non-ASCII characters!" and throws an encoding error. I call
this sort of string "fake ASCII."
This is what was happening in the referenced GitHub issue (or at least
in the sample app Adam Salter created at
http://github.com/adamsalter/test-project/tree/haml_utf8). The
template was UTF-8 encoded, and it was being passed a fake ASCII
string, marked as ASCII-8bit but with UTF-8 byte sequences in it, and
it was choking.
The issue now becomes: where is this fake ASCII string coming from?
From the database. The database drivers used by Rails aren't Ruby 1.9
compatible. Despite storing UTF-8 strings in the database, the drivers
return fake ASCII strings.
The best solution to this is clearly to fix the database drivers, but
that will probably take some time. One stop-gap would be to call
`force_encoding("utf-8")` on all the database values somewhere, which
is still a little annoying. Finally, the solution provided in this
commit is to set `:encoding => "ascii-8bit"` for Haml. This makes the
Haml template itself fake ASCII, which is wrong but will help prevent
encoding errors.
2009-11-08 18:59:52 -05:00
|
|
|
|
2010-05-29 19:51:36 -04:00
|
|
|
### Encodings
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
When using Ruby 1.9 or later, Haml supports the same sorts of
|
|
|
|
encoding-declaration comments that Ruby does. Although both Ruby and Haml
|
|
|
|
support several different styles, the easiest it just to add `-# coding:
|
|
|
|
encoding-name` at the beginning of the Haml template (it must come before all
|
|
|
|
other lines). This will tell Haml that the template is encoded using the named
|
|
|
|
encoding.
|
2010-05-29 19:51:36 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
By default, the HTML generated by Haml has the same encoding as the Haml
|
|
|
|
template. However, if `Encoding.default_internal` is set, Haml will attempt to
|
|
|
|
use that instead. In addition, the [`:encoding` option](#encoding-option) can be
|
|
|
|
used to specify an output encoding manually.
|
2010-05-29 19:51:36 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Note that, like Ruby, Haml does not support templates encoded in UTF-16 or
|
|
|
|
UTF-32, since these encodings are not compatible with ASCII. It is possible to
|
|
|
|
use these as the output encoding, though.
|
2010-05-29 19:51:36 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
## Plain Text
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
A substantial portion of any HTML document is its content, which is plain old
|
|
|
|
text. Any Haml line that's not interpreted as something else is taken to be
|
|
|
|
plain text, and passed through unmodified. For example:
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
%gee
|
|
|
|
%whiz
|
|
|
|
Wow this is cool!
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<gee>
|
|
|
|
<whiz>
|
|
|
|
Wow this is cool!
|
|
|
|
</whiz>
|
|
|
|
</gee>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Note that HTML tags are passed through unmodified as well. If you have some HTML
|
|
|
|
you don't want to convert to Haml, or you're converting a file line-by-line, you
|
|
|
|
can just include it as-is. For example:
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
%p
|
|
|
|
<div id="blah">Blah!</div>
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<div id="blah">Blah!</div>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
### Escaping: `\`
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The backslash character escapes the first character of a line, allowing use of
|
|
|
|
otherwise interpreted characters as plain text. For example:
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
%title
|
|
|
|
= @title
|
|
|
|
\= @title
|
|
|
|
|
|
|
|
is compiled to:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<title>
|
|
|
|
MyPage
|
|
|
|
= @title
|
|
|
|
</title>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
## HTML Elements
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
### Element Name: `%`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The percent character is placed at the beginning of a line. It's followed
|
|
|
|
immediately by the name of an element, then optionally by modifiers (see below),
|
|
|
|
a space, and text to be rendered inside the element. It creates an element in
|
|
|
|
the form of `<element></element>`. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%one
|
|
|
|
%two
|
|
|
|
%three Hey there
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<one>
|
|
|
|
<two>
|
|
|
|
<three>Hey there</three>
|
|
|
|
</two>
|
|
|
|
</one>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Any string is a valid element name; Haml will automatically generate opening and
|
|
|
|
closing tags for any element.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-07-04 18:40:23 -04:00
|
|
|
### Attributes: `{}` or `()` {#attributes}
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Brackets represent a Ruby hash that is used for specifying the attributes of an
|
|
|
|
element. It is literally evaluated as a Ruby hash, so logic will work in it and
|
|
|
|
local variables may be used. Quote characters within the attribute will be
|
|
|
|
replaced by appropriate escape sequences. The hash is placed after the tag is
|
|
|
|
defined. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%html{:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", :lang => "en"}
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'></html>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Attribute hashes can also be stretched out over multiple lines to accommodate
|
|
|
|
many attributes. However, newlines may only be placed immediately after commas.
|
2009-06-18 16:40:57 -04:00
|
|
|
For example:
|
|
|
|
|
|
|
|
%script{:type => "text/javascript",
|
|
|
|
:src => "javascripts/script_#{2 + 7}"}
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<script src='javascripts/script_9' type='text/javascript'></script>
|
|
|
|
|
2010-02-27 08:52:09 -05:00
|
|
|
#### `:class` and `:id` Attributes
|
|
|
|
{#class-and-id-attributes}
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The `:class` and `:id` attributes can also be specified as a Ruby array whose
|
|
|
|
elements will be joined together. A `:class` array is joined with `" "` and an
|
|
|
|
`:id` array is joined with `"_"`. For example:
|
2010-02-27 08:52:09 -05:00
|
|
|
|
|
|
|
%div{:id => [@item.type, @item.number], :class => [@item.type, @item.urgency]}
|
|
|
|
|
|
|
|
is equivalent to:
|
|
|
|
|
|
|
|
%div{:id => "#{@item.type}_#{@item.number}", :class => "#{@item.type} #{@item.urgency}"}
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The array will first be flattened and any elements that do not test as true will
|
|
|
|
be removed. The remaining elements will be converted to strings. For example:
|
2010-02-27 08:52:09 -05:00
|
|
|
|
|
|
|
%div{:class => [@item.type, @item == @sortcol && [:sort, @sortdir]] } Contents
|
|
|
|
|
|
|
|
could render as any of:
|
|
|
|
|
2010-07-20 00:00:40 -04:00
|
|
|
<div class="numeric sort ascending">Contents</div>
|
|
|
|
<div class="numeric">Contents</div>
|
|
|
|
<div class="sort descending">Contents</div>
|
|
|
|
<div>Contents</div>
|
2010-02-27 08:52:09 -05:00
|
|
|
|
2012-06-11 16:51:38 -04:00
|
|
|
depending on whether `@item.type` is `"numeric"` or `nil`, whether `@item == @sortcol`,
|
2010-02-27 08:52:09 -05:00
|
|
|
and whether `@sortdir` is `"ascending"` or `"descending"`.
|
|
|
|
|
|
|
|
If a single value is specified and it evaluates to false it is ignored;
|
2012-06-08 12:00:07 -04:00
|
|
|
otherwise it gets converted to a string. For example:
|
2010-02-27 08:52:09 -05:00
|
|
|
|
|
|
|
.item{:class => @item.is_empty? && "empty"}
|
|
|
|
|
|
|
|
could render as either of:
|
|
|
|
|
|
|
|
class="item"
|
|
|
|
class="item empty"
|
|
|
|
|
2009-07-04 18:40:23 -04:00
|
|
|
#### HTML-style Attributes: `()`
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Haml also supports a terser, less Ruby-specific attribute syntax based on HTML's
|
|
|
|
attributes. These are used with parentheses instead of brackets, like so:
|
2009-07-04 18:40:23 -04:00
|
|
|
|
|
|
|
%html(xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en")
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Ruby variables can be used by omitting the quotes. Local variables or instance
|
|
|
|
variables can be used. For example:
|
2009-07-04 18:40:23 -04:00
|
|
|
|
|
|
|
%a(title=@title href=href) Stuff
|
|
|
|
|
|
|
|
This is the same as:
|
|
|
|
|
|
|
|
%a{:title => @title, :href => href} Stuff
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Because there are no commas separating attributes, though, more complicated
|
|
|
|
expressions aren't allowed. For those you'll have to use the `{}` syntax. You
|
|
|
|
can, however, use both syntaxes together:
|
2009-07-04 18:40:23 -04:00
|
|
|
|
|
|
|
%a(title=@title){:href => @link.href} Stuff
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
You can also use `#{}` interpolation to insert complicated expressions in a
|
|
|
|
HTML-style attribute:
|
2009-07-05 06:07:06 -04:00
|
|
|
|
|
|
|
%span(class="widget_#{@widget.number}")
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
HTML-style attributes can be stretched across multiple lines just like
|
|
|
|
hash-style attributes:
|
2009-07-05 04:51:26 -04:00
|
|
|
|
|
|
|
%script(type="text/javascript"
|
|
|
|
src="javascripts/script_#{2 + 7}")
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
#### Ruby 1.9-style Hashes
|
|
|
|
|
|
|
|
On Ruby 1.9, Haml also supports Ruby's new hash syntax:
|
|
|
|
|
|
|
|
%a{title: @title, href: href} Stuff
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
#### Attribute Methods
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
A Ruby method call that returns a hash can be substituted for the hash contents.
|
2009-06-18 16:40:57 -04:00
|
|
|
For example, {Haml::Helpers} defines the following method:
|
|
|
|
|
|
|
|
def html_attrs(lang = 'en-US')
|
|
|
|
{:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang}
|
|
|
|
end
|
|
|
|
|
|
|
|
This can then be used in Haml, like so:
|
|
|
|
|
|
|
|
%html{html_attrs('fr-fr')}
|
|
|
|
|
|
|
|
This is compiled to:
|
|
|
|
|
|
|
|
<html lang='fr-fr' xml:lang='fr-fr' xmlns='http://www.w3.org/1999/xhtml'>
|
|
|
|
</html>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
You can use as many such attribute methods as you want by separating them with
|
|
|
|
commas, like a Ruby argument list. All the hashes will me merged together, from
|
|
|
|
left to right. For example, if you defined
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
def hash1
|
|
|
|
{:bread => 'white', :filling => 'peanut butter and jelly'}
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash2
|
|
|
|
{:bread => 'whole wheat'}
|
|
|
|
end
|
|
|
|
|
|
|
|
then
|
|
|
|
|
2012-11-21 15:27:39 -05:00
|
|
|
%sandwich{hash1, hash2, :delicious => 'true'}/
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
would compile to:
|
|
|
|
|
|
|
|
<sandwich bread='whole wheat' delicious='true' filling='peanut butter and jelly' />
|
|
|
|
|
|
|
|
Note that the Haml attributes list has the same syntax as a Ruby method call.
|
|
|
|
This means that any attribute methods must come before the hash literal.
|
|
|
|
|
2009-07-04 18:40:23 -04:00
|
|
|
Attribute methods aren't supported for HTML-style attributes.
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
#### Boolean Attributes
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Some attributes, such as "checked" for `input` tags or "selected" for `option`
|
|
|
|
tags, are "boolean" in the sense that their values don't matter - it only
|
|
|
|
matters whether or not they're present. In HTML (but not XHTML), these
|
|
|
|
attributes can be written as
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
<input selected>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
To do this in Haml using hash-style attributes, just assign a Ruby `true` value
|
|
|
|
to the attribute:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%input{:selected => true}
|
|
|
|
|
2009-07-04 18:40:23 -04:00
|
|
|
In XHTML, the only valid value for these attributes is the name of the
|
2012-06-08 12:00:07 -04:00
|
|
|
attribute. Thus this will render in XHTML as
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
<input selected='selected'>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
To set these attributes to false, simply assign them to a Ruby false value. In
|
|
|
|
both XHTML and HTML,
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%input{:selected => false}
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
will just render as:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
<input>
|
|
|
|
|
2009-07-04 18:40:23 -04:00
|
|
|
HTML-style boolean attributes can be written just like HTML:
|
|
|
|
|
|
|
|
%input(selected)
|
|
|
|
|
|
|
|
or using `true` and `false`:
|
|
|
|
|
|
|
|
%input(selected=true)
|
|
|
|
|
2010-03-14 19:13:53 -04:00
|
|
|
#### HTML5 Custom Data Attributes
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
HTML5 allows for adding [custom non-visible data
|
|
|
|
attributes](http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data)
|
|
|
|
to elements using attribute names beginning with `data-`. Custom data attributes
|
|
|
|
can be used in Haml by using the key `:data` with a Hash value in an attribute
|
|
|
|
hash. Each of the key/value pairs in the Hash will be transformed into a custom
|
|
|
|
data attribute. For example:
|
2010-03-14 19:13:53 -04:00
|
|
|
|
|
|
|
%a{:href=>"/posts", :data => {:author_id => 123}} Posts By Author
|
|
|
|
|
|
|
|
will render as:
|
|
|
|
|
2012-04-29 11:17:05 -04:00
|
|
|
<a data-author-id='123' href='/posts'>Posts By Author</a>
|
2010-03-14 19:13:53 -04:00
|
|
|
|
2012-04-29 11:17:05 -04:00
|
|
|
Notice that the underscore in `author_id` was replaced by a hyphen. If you wish
|
|
|
|
to suppress this behavior, you can set Haml's [`:hyphenate_data_attrs`
|
|
|
|
option](#hyphenate_data_attrs-option) to `false`, and the output will be
|
|
|
|
rendered as:
|
2012-01-28 03:30:12 -05:00
|
|
|
|
2012-04-29 11:17:05 -04:00
|
|
|
<a data-author_id='123' href='/posts'>Posts By Author</a>
|
2012-01-28 03:30:12 -05:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
### Class and ID: `.` and `#`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The period and pound sign are borrowed from CSS. They are used as shortcuts to
|
|
|
|
specify the `class` and `id` attributes of an element, respectively. Multiple
|
|
|
|
class names can be specified in a similar way to CSS, by chaining the class
|
|
|
|
names together with periods. They are placed immediately after the tag and
|
|
|
|
before an attributes hash. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%div#things
|
|
|
|
%span#rice Chicken Fried
|
|
|
|
%p.beans{ :food => 'true' } The magical fruit
|
|
|
|
%h1.class.otherclass#id La La La
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<div id='things'>
|
|
|
|
<span id='rice'>Chicken Fried</span>
|
|
|
|
<p class='beans' food='true'>The magical fruit</p>
|
|
|
|
<h1 class='class otherclass' id='id'>La La La</h1>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
And,
|
|
|
|
|
2010-03-01 03:55:32 -05:00
|
|
|
%div#content
|
|
|
|
%div.articles
|
|
|
|
%div.article.title Doogie Howser Comes Out
|
|
|
|
%div.article.date 2006-11-05
|
|
|
|
%div.article.entry
|
2009-06-18 16:40:57 -04:00
|
|
|
Neil Patrick Harris would like to dispel any rumors that he is straight
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<div id='content'>
|
|
|
|
<div class='articles'>
|
|
|
|
<div class='article title'>Doogie Howser Comes Out</div>
|
|
|
|
<div class='article date'>2006-11-05</div>
|
|
|
|
<div class='article entry'>
|
|
|
|
Neil Patrick Harris would like to dispel any rumors that he is straight
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
These shortcuts can be combined with long-hand attributes; the two values will
|
|
|
|
be merged together as though they were all placed in an array (see [the
|
|
|
|
documentation on `:class` and `:id` attributes](#class-and-id-attributes)). For
|
|
|
|
example:
|
2010-02-27 08:52:09 -05:00
|
|
|
|
|
|
|
%div#Article.article.entry{:id => @article.number, :class => @article.visibility}
|
|
|
|
|
|
|
|
is equivalent to
|
|
|
|
|
|
|
|
%div{:id => ['Article', @article.number], :class => ['article', 'entry', @article.visibility]} Gabba Hey
|
|
|
|
|
|
|
|
and could compile to:
|
|
|
|
|
|
|
|
<div class="article entry visible" id="Article_27">Gabba Hey</div>
|
|
|
|
|
2009-06-18 16:40:57 -04:00
|
|
|
#### Implicit Div Elements
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Because divs are used so often, they're the default elements. If you only define
|
|
|
|
a class and/or id using `.` or `#`, a div is automatically used. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
#collection
|
|
|
|
.item
|
|
|
|
.description What a cool item!
|
|
|
|
|
|
|
|
is the same as:
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
%div#collection
|
|
|
|
%div.item
|
|
|
|
%div.description What a cool item!
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
and is compiled to:
|
|
|
|
|
|
|
|
<div id='collection'>
|
|
|
|
<div class='item'>
|
|
|
|
<div class='description'>What a cool item!</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
### Self-Closing Tags: `/`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The forward slash character, when placed at the end of a tag definition, causes
|
|
|
|
the tag to be self-closed. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%br/
|
|
|
|
%meta{'http-equiv' => 'Content-Type', :content => 'text/html'}/
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<br />
|
|
|
|
<meta http-equiv='Content-Type' content='text/html' />
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Some tags are automatically closed, as long as they have no content. `meta`,
|
|
|
|
`img`, `link`, `script`, `br`, and `hr` tags are closed by default. This list
|
|
|
|
can be customized by setting the [`:autoclose`](#autoclose-option) option. For
|
|
|
|
example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%br
|
|
|
|
%meta{'http-equiv' => 'Content-Type', :content => 'text/html'}
|
|
|
|
|
|
|
|
is also compiled to:
|
|
|
|
|
|
|
|
<br />
|
|
|
|
<meta http-equiv='Content-Type' content='text/html' />
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
### Whitespace Removal: `>` and `<`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
`>` and `<` give you more control over the whitespace near a tag. `>` will
|
|
|
|
remove all whitespace surrounding a tag, while `<` will remove all whitespace
|
|
|
|
immediately within a tag. You can think of them as alligators eating the
|
|
|
|
whitespace: `>` faces out of the tag and eats the whitespace on the outside, and
|
|
|
|
`<` faces into the tag and eats the whitespace on the inside. They're placed at
|
|
|
|
the end of a tag definition, after class, id, and attribute declarations but
|
|
|
|
before `/` or `=`. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%blockquote<
|
|
|
|
%div
|
|
|
|
Foo!
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<blockquote><div>
|
|
|
|
Foo!
|
|
|
|
</div></blockquote>
|
|
|
|
|
|
|
|
And:
|
|
|
|
|
|
|
|
%img
|
|
|
|
%img>
|
|
|
|
%img
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<img /><img /><img />
|
|
|
|
|
|
|
|
And:
|
|
|
|
|
|
|
|
%p<= "Foo\nBar"
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<p>Foo
|
|
|
|
Bar</p>
|
|
|
|
|
|
|
|
And finally:
|
|
|
|
|
|
|
|
%img
|
|
|
|
%pre><
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
%img
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<img /><pre>foo
|
|
|
|
bar</pre><img />
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
### Object Reference: `[]`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Square brackets follow a tag definition and contain a Ruby object that is used
|
|
|
|
to set the class and id of that tag. The class is set to the object's class
|
|
|
|
(transformed to use underlines rather than camel case) and the id is set to the
|
|
|
|
object's class, followed by the value of its `#to_key` or `#id` method (in that
|
|
|
|
order). This is most useful for elements that represent instances of Active
|
|
|
|
Model models. Additionally, the second argument (if present) will be used as a
|
|
|
|
prefix for both the id and class attributes. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
# file: app/controllers/users_controller.rb
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
def show
|
|
|
|
@user = CrazyUser.find(15)
|
|
|
|
end
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
-# file: app/views/users/show.haml
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
%div[@user, :greeting]
|
|
|
|
%bar[290]/
|
|
|
|
Hello!
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<div class='greeting_crazy_user' id='greeting_crazy_user_15'>
|
|
|
|
<bar class='fixnum' id='fixnum_581' />
|
|
|
|
Hello!
|
|
|
|
</div>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
If you require that the class be something other than the underscored object's
|
|
|
|
class, you can implement the `haml_object_ref` method on the object.
|
2009-07-03 00:32:31 -04:00
|
|
|
|
|
|
|
# file: app/models/crazy_user.rb
|
|
|
|
|
|
|
|
class CrazyUser < ActiveRecord::Base
|
|
|
|
def haml_object_ref
|
|
|
|
"a_crazy_user"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-# file: app/views/users/show.haml
|
|
|
|
|
|
|
|
%div[@user]
|
|
|
|
Hello!
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<div class='a_crazy_user' id='a_crazy_user_15'>
|
|
|
|
Hello!
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
## Doctype: `!!!`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
When describing HTML documents with Haml, you can have a document type or XML
|
|
|
|
prolog generated automatically by including the characters `!!!`. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
!!! XML
|
|
|
|
!!!
|
|
|
|
%html
|
|
|
|
%head
|
|
|
|
%title Myspace
|
|
|
|
%body
|
|
|
|
%h1 I am the international space station
|
|
|
|
%p Sign my guestbook
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<?xml version='1.0' encoding='utf-8' ?>
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Myspace</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>I am the international space station</h1>
|
|
|
|
<p>Sign my guestbook</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
You can also specify the specific doctype after the `!!!` When the
|
2012-06-26 10:33:31 -04:00
|
|
|
[`:format`](#format-option) is set to `:xhtml`. The following doctypes are
|
|
|
|
supported:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!!`
|
|
|
|
: XHTML 1.0 Transitional<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!! Strict`
|
|
|
|
: XHTML 1.0 Strict<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!! Frameset`
|
|
|
|
: XHTML 1.0 Frameset<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 06:07:21 -05:00
|
|
|
`!!! 5`
|
|
|
|
: XHTML 5<br/>
|
|
|
|
`<!DOCTYPE html>`<br/>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!! 1.1`
|
|
|
|
: XHTML 1.1<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!! Basic`
|
|
|
|
: XHTML Basic 1.1<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"> `
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!! Mobile`
|
|
|
|
: XHTML Mobile 1.2<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2010-04-11 19:59:30 -04:00
|
|
|
`!!! RDFa`
|
|
|
|
: XHTML+RDFa 1.0<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
When the [`:format`](#format-option) option is set to `:html4`, the following
|
|
|
|
doctypes are supported:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!!`
|
|
|
|
: HTML 4.01 Transitional<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!! Strict`
|
|
|
|
: HTML 4.01 Strict<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!! Frameset`
|
|
|
|
: HTML 4.01 Frameset<br/>
|
|
|
|
`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2010-05-23 07:08:46 -04:00
|
|
|
When the [`:format`](#format-option) option is set to `:html5`,
|
2009-11-20 05:54:25 -05:00
|
|
|
`!!!` is always `<!DOCTYPE html>`.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
If you're not using the UTF-8 character set for your document, you can specify
|
|
|
|
which encoding should appear in the XML prolog in a similar way. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
!!! XML iso-8859-1
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<?xml version='1.0' encoding='iso-8859-1' ?>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
If the mime_type of the template being rendered is `text/xml` then a format of
|
|
|
|
`:xhtml` will be used even if the global output format is set to `:html4` or
|
|
|
|
`:html5`.
|
2011-09-09 00:44:40 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
## Comments
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Haml supports two sorts of comments: those that show up in the HTML output and
|
|
|
|
those that don't.
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
### HTML Comments: `/`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The forward slash character, when placed at the beginning of a line, wraps all
|
|
|
|
text after it in an HTML comment. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%peanutbutterjelly
|
|
|
|
/ This is the peanutbutterjelly element
|
|
|
|
I like sandwiches!
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<peanutbutterjelly>
|
|
|
|
<!-- This is the peanutbutterjelly element -->
|
|
|
|
I like sandwiches!
|
|
|
|
</peanutbutterjelly>
|
|
|
|
|
|
|
|
The forward slash can also wrap indented sections of code. For example:
|
|
|
|
|
|
|
|
/
|
|
|
|
%p This doesn't render...
|
|
|
|
%div
|
|
|
|
%h1 Because it's commented out!
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<!--
|
|
|
|
<p>This doesn't render...</p>
|
|
|
|
<div>
|
|
|
|
<h1>Because it's commented out!</h1>
|
|
|
|
</div>
|
|
|
|
-->
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
#### Conditional Comments: `/[]`
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
You can also use [Internet Explorer conditional
|
|
|
|
comments](http://www.quirksmode.org/css/condcom.html) by enclosing the condition
|
|
|
|
in square brackets after the `/`. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
/[if IE]
|
|
|
|
%a{ :href => 'http://www.mozilla.com/en-US/firefox/' }
|
|
|
|
%h1 Get Firefox
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<!--[if IE]>
|
|
|
|
<a href='http://www.mozilla.com/en-US/firefox/'>
|
|
|
|
<h1>Get Firefox</h1>
|
|
|
|
</a>
|
|
|
|
<![endif]-->
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
### Haml Comments: `-#`
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The hyphen followed immediately by the pound sign signifies a silent comment.
|
|
|
|
Any text following this isn't rendered in the resulting document at all.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
%p foo
|
|
|
|
-# This is a comment
|
|
|
|
%p bar
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<p>foo</p>
|
|
|
|
<p>bar</p>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
You can also nest text beneath a silent comment. None of this text will be
|
|
|
|
rendered. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
%p foo
|
|
|
|
-#
|
|
|
|
This won't be displayed
|
|
|
|
Nor will this
|
2012-04-30 16:01:50 -04:00
|
|
|
Nor will this.
|
2009-06-19 03:03:06 -04:00
|
|
|
%p bar
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<p>foo</p>
|
|
|
|
<p>bar</p>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
## Ruby Evaluation
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
### Inserting Ruby: `=`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The equals character is followed by Ruby code. This code is evaluated and the
|
|
|
|
output is inserted into the document. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
%p
|
|
|
|
= ['hi', 'there', 'reader!'].join " "
|
|
|
|
= "yo"
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
is compiled to:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
<p>
|
2009-06-19 03:03:06 -04:00
|
|
|
hi there reader!
|
|
|
|
yo
|
2009-06-18 16:40:57 -04:00
|
|
|
</p>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
If the [`:escape_html`](#escape_html-option) option is set, `=` will sanitize
|
|
|
|
any HTML-sensitive characters generated by the script. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
= '<script>alert("I\'m evil!");</script>'
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
would be compiled to
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<script>alert("I'm evil!");</script>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
`=` can also be used at the end of a tag to insert Ruby code within that tag.
|
|
|
|
For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
%p= "hello"
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
would be compiled to:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<p>hello</p>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
A line of Ruby code can be stretched over multiple lines as long as each line
|
|
|
|
but the last ends with a comma. For example:
|
2010-04-24 04:09:51 -04:00
|
|
|
|
|
|
|
= link_to_remote "Add to cart",
|
|
|
|
:url => { :action => "add", :id => product.id },
|
|
|
|
:update => { :success => "cart", :failure => "error" }
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
Note that it's illegal to nest code within a tag that ends with `=`.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
### Running Ruby: `-`
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
The hyphen character is also followed by Ruby code. This code is evaluated but
|
|
|
|
*not* inserted into the document.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
**It is not recommended that you use this widely; almost all processing code and
|
|
|
|
logic should be restricted to Controllers, Helpers, or partials.**
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
- foo = "hello"
|
|
|
|
- foo << " there"
|
|
|
|
- foo << " you!"
|
|
|
|
%p= foo
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
is compiled to:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<p>
|
|
|
|
hello there you!
|
|
|
|
</p>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
A line of Ruby code can be stretched over multiple lines as long as each line
|
|
|
|
but the last ends with a comma. For example:
|
2010-04-24 04:09:51 -04:00
|
|
|
|
|
|
|
- links = {:home => "/",
|
|
|
|
:docs => "/docs",
|
|
|
|
:about => "/about"}
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
#### Ruby Blocks
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
Ruby blocks, like XHTML tags, don't need to be explicitly closed in Haml.
|
2012-06-08 12:00:07 -04:00
|
|
|
Rather, they're automatically closed, based on indentation. A block begins
|
|
|
|
whenever the indentation is increased after a Ruby evaluation command. It ends
|
|
|
|
when the indentation decreases (as long as it's not an `else` clause or
|
|
|
|
something similar). For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
- (42...47).each do |i|
|
|
|
|
%p= i
|
|
|
|
%p See, I can count!
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
is compiled to:
|
|
|
|
|
2010-01-16 17:27:02 -05:00
|
|
|
<p>42</p>
|
|
|
|
<p>43</p>
|
|
|
|
<p>44</p>
|
|
|
|
<p>45</p>
|
|
|
|
<p>46</p>
|
|
|
|
<p>See, I can count!</p>
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
Another example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
%p
|
2009-06-19 03:03:06 -04:00
|
|
|
- case 2
|
|
|
|
- when 1
|
|
|
|
= "1!"
|
|
|
|
- when 2
|
|
|
|
= "2?"
|
|
|
|
- when 3
|
|
|
|
= "3."
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<p>
|
2009-06-19 03:03:06 -04:00
|
|
|
2?
|
2009-06-18 16:40:57 -04:00
|
|
|
</p>
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
### Whitespace Preservation: `~` {#tilde}
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
`~` works just like `=`, except that it runs {Haml::Helpers#find\_and\_preserve}
|
|
|
|
on its input. For example,
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
~ "Foo\n<pre>Bar\nBaz</pre>"
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
is the same as:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
= find_and_preserve("Foo\n<pre>Bar\nBaz</pre>")
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
and is compiled to:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
Foo
|
|
|
|
<pre>Bar
Baz</pre>
|
|
|
|
|
|
|
|
See also [Whitespace Preservation](#whitespace_preservation).
|
|
|
|
|
|
|
|
### Ruby Interpolation: `#{}`
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Ruby code can also be interpolated within plain text using `#{}`, similarly to
|
|
|
|
Ruby string interpolation. For example,
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
%p This is #{h quality} cake!
|
|
|
|
|
|
|
|
is the same as
|
|
|
|
|
|
|
|
%p= "This is the #{h quality} cake!"
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
and might compile to:
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
<p>This is scrumptious cake!</p>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Backslashes can be used to escape `#{}` strings, but they don't act as escapes
|
|
|
|
anywhere else in the string. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
%p
|
|
|
|
Look at \\#{h word} lack of backslash: \#{foo}
|
|
|
|
And yon presence thereof: \{foo}
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
might compile to:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
<p>
|
2009-06-19 03:03:06 -04:00
|
|
|
Look at \yon lack of backslash: #{foo}
|
|
|
|
And yon presence thereof: \{foo}
|
2009-06-18 16:40:57 -04:00
|
|
|
</p>
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Interpolation can also be used within [filters](#filters). For example:
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
:javascript
|
|
|
|
$(document).ready(function() {
|
|
|
|
alert(#{@message.to_json});
|
|
|
|
});
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
might compile to:
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
<script type='text/javascript'>
|
|
|
|
//<![CDATA[
|
|
|
|
$(document).ready(function() {
|
|
|
|
alert("Hi there!");
|
|
|
|
});
|
|
|
|
//]]>
|
|
|
|
</script>
|
|
|
|
|
2009-07-05 06:07:06 -04:00
|
|
|
### Escaping HTML: `&=` {#escaping_html}
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
An ampersand followed by one or two equals characters evaluates Ruby code just
|
|
|
|
like the equals without the ampersand, but sanitizes any HTML-sensitive
|
|
|
|
characters in the result of the code. For example:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
&= "I like cheese & crackers"
|
|
|
|
|
|
|
|
compiles to
|
|
|
|
|
|
|
|
I like cheese & crackers
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
If the {Haml::Options#escape_html `:escape_html`} option is set, `&=` behaves
|
|
|
|
identically to `=`.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
`&` can also be used on its own so that `#{}` interpolation is escaped. For
|
|
|
|
example,
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
& I like #{"cheese & crackers"}
|
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
compiles to:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
I like cheese & crackers
|
|
|
|
|
2009-07-05 06:07:06 -04:00
|
|
|
### Unescaping HTML: `!=` {#unescaping_html}
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
An exclamation mark followed by one or two equals characters evaluates Ruby code
|
|
|
|
just like the equals would, but never sanitizes the HTML.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
By default, the single equals doesn't sanitize HTML either. However, if the
|
|
|
|
{Haml::Options#escape_html `:escape_html`} option is set, `=` will sanitize the
|
|
|
|
HTML, but `!=` still won't. For example, if `:escape_html` is set:
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
= "I feel <strong>!"
|
|
|
|
!= "I feel <strong>!"
|
|
|
|
|
|
|
|
compiles to
|
|
|
|
|
|
|
|
I feel <strong>!
|
|
|
|
I feel <strong>!
|
|
|
|
|
|
|
|
`!` can also be used on its own so that `#{}` interpolation is unescaped.
|
|
|
|
For example,
|
|
|
|
|
|
|
|
! I feel #{"<strong>"}!
|
|
|
|
|
|
|
|
compiles to
|
|
|
|
|
|
|
|
I feel <strong>!
|
|
|
|
|
2009-10-23 04:06:40 -04:00
|
|
|
## Filters {#filters}
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2012-05-22 22:34:18 -04:00
|
|
|
The colon character designates a filter. This allows you to pass an indented
|
|
|
|
block of text as input to another filtering program and add the result to the
|
|
|
|
output of Haml. The syntax is simply a colon followed by the name of the filter.
|
2012-06-08 12:00:07 -04:00
|
|
|
For example:
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
%p
|
|
|
|
:markdown
|
2012-05-22 22:34:18 -04:00
|
|
|
# Greetings
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
Hello, *World*
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
is compiled to:
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<p>
|
2012-05-22 22:34:18 -04:00
|
|
|
<h1>Greetings</h1>
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<p>Hello, <em>World</em></p>
|
|
|
|
</p>
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Filters can have Ruby code interpolated with `#{}`. For example:
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
- flavor = "raspberry"
|
|
|
|
#content
|
|
|
|
:textile
|
2012-05-22 22:34:18 -04:00
|
|
|
I *really* prefer _#{flavor}_ jam.
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
is compiled to
|
2009-06-18 18:01:58 -04:00
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
<div id='content'>
|
|
|
|
<p>I <strong>really</strong> prefer <em>raspberry</em> jam.</p>
|
|
|
|
</div>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
Currently, filters ignore the {Haml::Options#escape_html `:escape_html`} option.
|
|
|
|
This means that `#{}` interpolation within filters is never HTML-escaped.
|
2009-07-19 19:26:43 -04:00
|
|
|
|
2012-05-22 22:34:18 -04:00
|
|
|
The functionality of some filters such as Markdown can be provided by many
|
|
|
|
different libraries. Usually you don't have to worry about this - you can just
|
|
|
|
load the gem of your choice and Haml will automatically use it.
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-06-08 12:00:07 -04:00
|
|
|
However in some cases you may want to make Haml explicitly use a specific gem to
|
|
|
|
be used by a filter. In these cases you can do this via Tilt, the library Haml
|
|
|
|
uses to implement many of its filters:
|
2009-10-23 04:06:40 -04:00
|
|
|
|
2012-05-22 22:34:18 -04:00
|
|
|
Tilt.prefer Tilt::RedCarpetTemplate
|
2009-10-23 04:06:40 -04:00
|
|
|
|
2012-05-22 22:34:18 -04:00
|
|
|
See the [Tilt documentation](https://github.com/rtomayko/tilt#fallback-mode) for
|
|
|
|
more info.
|
|
|
|
|
|
|
|
Haml comes with the following filters defined:
|
2009-11-09 22:46:36 -05:00
|
|
|
|
2009-10-23 04:06:40 -04:00
|
|
|
{#cdata-filter}
|
|
|
|
### `:cdata`
|
|
|
|
Surrounds the filtered text with CDATA tags.
|
|
|
|
|
2012-05-22 22:34:18 -04:00
|
|
|
{#coffee-filter}
|
|
|
|
### `:coffee`
|
|
|
|
Compiles the filtered text to Javascript using Cofeescript. You can also
|
|
|
|
reference this filter as `:coffeescript`. This filter is implemented using
|
|
|
|
Tilt.
|
|
|
|
|
|
|
|
{#css-filter}
|
|
|
|
### `:css`
|
2012-06-25 16:31:01 -04:00
|
|
|
Surrounds the filtered text with `<style>` and (optionally) CDATA tags. Useful
|
|
|
|
for including inline CSS. Use the {Haml::Options#cdata `:cdata` option} to
|
|
|
|
control when CDATA tags are added.
|
2012-05-22 22:34:18 -04:00
|
|
|
|
|
|
|
{#erb-filter}
|
|
|
|
### `:erb`
|
|
|
|
Parses the filtered text with ERb, like an RHTML template. Not available if the
|
|
|
|
[`:suppress_eval`](#suppress_eval-option) option is set to true. Embedded Ruby
|
|
|
|
code is evaluated in the same context as the Haml template. This filter is
|
|
|
|
implemented using Tilt.
|
|
|
|
|
2009-10-23 04:06:40 -04:00
|
|
|
{#escaped-filter}
|
|
|
|
### `:escaped`
|
|
|
|
Works the same as plain, but HTML-escapes the text
|
|
|
|
before placing it in the document.
|
|
|
|
|
2012-05-22 22:34:18 -04:00
|
|
|
{#javascript-filter}
|
|
|
|
### `:javascript`
|
2012-06-25 16:31:01 -04:00
|
|
|
Surrounds the filtered text with `<script>` and (optionally) CDATA tags.
|
|
|
|
Useful for including inline Javascript. Use the {Haml::Options#cdata `:cdata`
|
|
|
|
option} to control when CDATA tags are added.
|
2012-05-22 22:34:18 -04:00
|
|
|
|
|
|
|
{#less-filter}
|
|
|
|
### `:less`
|
|
|
|
Parses the filtered text with [Less](http://lesscss.org/) to produce CSS output.
|
|
|
|
This filter is implemented using Tilt.
|
|
|
|
|
|
|
|
|
|
|
|
{#markdown-filter}
|
|
|
|
### `:markdown`
|
|
|
|
Parses the filtered text with
|
|
|
|
[Markdown](http://daringfireball.net/projects/markdown). This filter is
|
|
|
|
implemented using Tilt.
|
|
|
|
|
|
|
|
{#maruku-filter}
|
|
|
|
### `:maruku`
|
|
|
|
Parses the filtered text with [Maruku](https://github.com/nex3/maruku), which
|
|
|
|
has some non-standard extensions to Markdown.
|
|
|
|
|
2013-02-12 21:26:30 -05:00
|
|
|
As of Haml 4.0, this filter is defined in [Haml
|
2012-05-22 22:34:18 -04:00
|
|
|
contrib](https://github.com/haml/haml-contrib) but is loaded automatically for
|
|
|
|
historical reasons. In future versions of Haml it will likely not be loaded by
|
|
|
|
default. This filter is implemented using Tilt.
|
|
|
|
|
|
|
|
{#plain-filter}
|
|
|
|
### `:plain`
|
|
|
|
Does not parse the filtered text. This is useful for large blocks of text
|
|
|
|
without HTML tags, when you don't want lines starting with `.` or `-` to be
|
|
|
|
parsed.
|
2009-10-23 04:06:40 -04:00
|
|
|
|
|
|
|
{#preserve-filter}
|
|
|
|
### `:preserve`
|
|
|
|
Inserts the filtered text into the template with whitespace preserved.
|
2012-05-22 22:34:18 -04:00
|
|
|
`preserve`d blocks of text aren't indented, and newlines are replaced with the
|
|
|
|
HTML escape code for newlines, to preserve nice-looking output. See also
|
|
|
|
[Whitespace Preservation](#whitespace_preservation).
|
2009-06-19 03:03:06 -04:00
|
|
|
|
2012-05-22 22:34:18 -04:00
|
|
|
{#ruby-filter}
|
|
|
|
### `:ruby`
|
|
|
|
Parses the filtered text with the normal Ruby interpreter. All output sent to
|
|
|
|
`$stdout`, like with `puts`, is output into the Haml document. Not available if
|
|
|
|
the [`:suppress_eval`](#suppress_eval-option) option is set to true. The Ruby
|
|
|
|
code is evaluated in the same context as the Haml template.
|
2009-10-23 04:06:40 -04:00
|
|
|
|
|
|
|
{#sass-filter}
|
|
|
|
### `:sass`
|
2012-05-22 22:34:18 -04:00
|
|
|
Parses the filtered text with [Sass](http://sass-lang.com/) to produce CSS
|
|
|
|
output. This filter is implemented using Tilt.
|
2009-10-23 04:06:40 -04:00
|
|
|
|
2012-01-22 22:26:49 -05:00
|
|
|
{#scss-filter}
|
|
|
|
### `:scss`
|
2012-05-22 22:34:18 -04:00
|
|
|
Parses the filtered text with Sass like the `:sass` filter, but uses the newer
|
|
|
|
SCSS syntax to produce CSS output. This filter is implemented using Tilt.
|
2012-01-22 22:26:49 -05:00
|
|
|
|
2009-10-23 04:06:40 -04:00
|
|
|
{#textile-filter}
|
|
|
|
### `:textile`
|
|
|
|
Parses the filtered text with [Textile](http://www.textism.com/tools/textile).
|
|
|
|
Only works if [RedCloth](http://redcloth.org) is installed.
|
|
|
|
|
2013-02-12 21:26:30 -05:00
|
|
|
As of Haml 4.0, this filter is defined in [Haml
|
2012-05-22 22:34:18 -04:00
|
|
|
contrib](https://github.com/haml/haml-contrib) but is loaded automatically for
|
|
|
|
historical reasons. In future versions of Haml it will likely not be loaded by
|
|
|
|
default. This filter is implemented using Tilt.
|
2009-10-23 04:06:40 -04:00
|
|
|
|
|
|
|
### Custom Filters
|
|
|
|
|
|
|
|
You can also define your own filters. See {Haml::Filters} for details.
|
2009-06-19 03:03:06 -04:00
|
|
|
|
2009-07-05 04:57:02 -04:00
|
|
|
## Multiline: `|` {#multiline}
|
2009-06-19 03:03:06 -04:00
|
|
|
|
|
|
|
The pipe character designates a multiline string.
|
|
|
|
It's placed at the end of a line (after some whitespace)
|
|
|
|
and means that all following lines that end with `|`
|
|
|
|
will be evaluated as though they were on the same line.
|
2009-06-28 16:09:28 -04:00
|
|
|
**Note that even the last line in the multiline block
|
2010-08-01 21:10:25 -04:00
|
|
|
should end with `|`.**
|
2009-06-19 03:03:06 -04:00
|
|
|
For example:
|
|
|
|
|
|
|
|
%whoo
|
2010-01-21 23:18:46 -05:00
|
|
|
%hoo= h( |
|
|
|
|
"I think this might get " + |
|
|
|
|
"pretty long so I should " + |
|
|
|
|
"probably make it " + |
|
|
|
|
"multiline so it doesn't " + |
|
|
|
|
"look awful.") |
|
2009-06-19 03:03:06 -04:00
|
|
|
%p This is short.
|
|
|
|
|
|
|
|
is compiled to:
|
|
|
|
|
|
|
|
<whoo>
|
2010-01-21 23:18:46 -05:00
|
|
|
<hoo>I think this might get pretty long so I should probably make it multiline so it doesn't look awful.</hoo>
|
2009-06-19 03:03:06 -04:00
|
|
|
<p>This is short</p>
|
|
|
|
</whoo>
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2009-06-28 16:09:28 -04:00
|
|
|
Using multiline declarations in Haml is intentionally awkward.
|
|
|
|
This is designed to discourage people from putting lots and lots of Ruby code
|
|
|
|
in their Haml templates.
|
|
|
|
If you find yourself using multiline declarations, stop and think:
|
|
|
|
could I do this better with a helper?
|
|
|
|
|
2010-04-24 04:09:51 -04:00
|
|
|
Note that there are a few cases where it's useful to allow
|
|
|
|
something to flow over onto multiple lines in a non-awkward manner.
|
|
|
|
One of these is HTML attributes.
|
2009-06-28 16:09:28 -04:00
|
|
|
Some elements just have lots of attributes,
|
|
|
|
so you can wrap attributes without using `|` (see [Attributes](#attributes)).
|
|
|
|
|
2010-04-24 04:09:51 -04:00
|
|
|
In addition, sometimes you need to call Ruby methods or declare data structures
|
|
|
|
that just need a lot of template information.
|
|
|
|
So data structures and functions that require lots of arguments
|
|
|
|
can be wrapped over multiple lines,
|
|
|
|
as long as each line but the last ends in a comma
|
|
|
|
(see [Inserting Ruby](#inserting_ruby_)).
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
## Whitespace Preservation
|
2009-06-18 16:40:57 -04:00
|
|
|
|
|
|
|
Sometimes you don't want Haml to indent all your text.
|
|
|
|
For example, tags like `pre` and `textarea` are whitespace-sensitive;
|
|
|
|
indenting the text makes them render wrong.
|
|
|
|
|
|
|
|
Haml deals with this by "preserving" newlines before they're put into the document --
|
2012-06-26 10:33:31 -04:00
|
|
|
converting them to the HTML whitespace escape code, `
`.
|
2009-06-18 16:40:57 -04:00
|
|
|
Then Haml won't try to re-format the indentation.
|
|
|
|
|
2009-10-29 02:10:56 -04:00
|
|
|
Literal `textarea` and `pre` tags automatically preserve content given through `=`.
|
|
|
|
Dynamically-generated `textarea`s and `pre`s can't be preserved automatically,
|
2009-06-18 16:40:57 -04:00
|
|
|
and so should be passed through {Haml::Helpers#find\_and\_preserve} or the [`~` command](#tilde),
|
|
|
|
which has the same effect.
|
|
|
|
|
|
|
|
Blocks of literal text can be preserved using the [`:preserve` filter](#preserve-filter).
|
|
|
|
|
2009-06-19 03:03:06 -04:00
|
|
|
## Helpers
|
2009-06-18 16:40:57 -04:00
|
|
|
|
2012-05-22 22:34:18 -04:00
|
|
|
Haml offers a bunch of helpers that are useful for doing stuff like preserving
|
|
|
|
whitespace, creating nicely indented output for user-defined helpers, and other
|
|
|
|
useful things. The helpers are all documented in the {Haml::Helpers} and
|
|
|
|
{Haml::Helpers::ActionViewExtensions} modules.
|