haml 3.2.0.alpha.13 → 3.2.0.alpha.14

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of haml might be problematic. Click here for more details.

@@ -2,6 +2,22 @@
2
2
 
3
3
  ## 3.2.0 (Unreleased)
4
4
 
5
+ * HTML2Haml has been extracted to a separate gem, creatively named "html2haml".
6
+
7
+ * Haml's internals have been refactored to move the parser, compiler and options
8
+ handling into independent classes, rather than including them all in the
9
+ Engine module.
10
+
11
+ * The :sass filter now wraps its output in a script tag, as do the new :less and
12
+ :scss filters. The :coffee filter wraps its output in a script tag.
13
+
14
+ * Haml now supports only Rails 3 and above, and Ruby 1.8.7 and above. If you
15
+ still need support for Rails 2 and Ruby 1.8.6, please use Haml 3.1.x which
16
+ will continue to be maintained for bug fixes.
17
+
18
+ * Added `remove_whitespace` option to always remove all whitespace around Haml
19
+ tags. (thanks to [Tim van der Horst](https://github.com/vdh))
20
+
5
21
  * Haml now flattens deeply nested data attribute hashes. For example:
6
22
 
7
23
  `.foo{:data => {:a => "b", :c => {:d => "e", :f => "g"}}}`
@@ -716,7 +732,7 @@ Thanks to [Josh Peek](http://joshpeek.com/).
716
732
 
717
733
  [Tagged on GitHub](http://github.com/nex3/haml/commit/2.2.21).
718
734
 
719
- * Fix a few bugs in the git-revision-reporting in {Haml::Version#version}.
735
+ * Fix a few bugs in the git-revision-reporting in Haml::Version.
720
736
  In particular, it will still work if `git gc` has been called recently,
721
737
  or if various files are missing.
722
738
 
@@ -1,13 +1,11 @@
1
1
  # Haml (XHTML Abstraction Markup Language)
2
2
 
3
- Haml is a markup language
4
- that's used to cleanly and simply describe the XHTML of any web document,
5
- without the use of inline code.
6
- Haml functions as a replacement
7
- for inline page templating systems such as PHP, ERB, and ASP.
8
- However, Haml avoids the need for explicitly coding XHTML into the template,
9
- because it is actually an abstract description of the XHTML,
10
- with some code to generate dynamic content.
3
+ Haml is a markup language that's used to cleanly and simply describe the XHTML
4
+ of any web document, without the use of inline code. Haml functions as a
5
+ replacement for inline page templating systems such as PHP, ERB, and ASP.
6
+ However, Haml avoids the need for explicitly coding HTML into the template,
7
+ because it is actually an abstract description of the HTML, with some code to
8
+ generate dynamic content.
11
9
 
12
10
  ## Features
13
11
 
@@ -21,9 +19,11 @@ with some code to generate dynamic content.
21
19
  ## Using Haml
22
20
 
23
21
  Haml can be used in three ways:
24
- as a command-line tool,
25
- as a plugin for Ruby on Rails,
26
- and as a standalone Ruby module.
22
+
23
+ * as a command-line tool,
24
+ * as a plugin for Ruby on Rails,
25
+ * and as a standalone Ruby module.
26
+
27
27
  The first step for all of these is to install the Haml gem:
28
28
 
29
29
  gem install haml
@@ -34,25 +34,15 @@ To run Haml from the command line, just use
34
34
 
35
35
  Use `haml --help` for full documentation.
36
36
 
37
- ### Rails/Merb Plugin {#plugin}
38
-
39
- To enable Haml in Rails versions before Rails 3,
40
- add the following line to `environment.rb`:
41
-
42
- config.gem "haml"
43
-
44
- For Rails 3, instead add the following line to the Gemfile:
37
+ To use Haml with Rails, add the following line to the Gemfile:
45
38
 
46
39
  gem "haml"
47
40
 
48
- Once it's installed, all view files with the `".html.haml"` extension
49
- will be compiled using Haml.
50
- Haml is enabled by default in Merb.
41
+ Once it's installed, all view files with the `".html.haml"` extension will be
42
+ compiled using Haml.
51
43
 
52
- You can access instance variables in Haml templates
53
- the same way you do in ERB templates.
54
- Helper methods are also available in Haml templates.
55
- For example (this example uses Rails, but the principle for Merb is the same):
44
+ You can access instance variables in Haml templates the same way you do in ERB
45
+ templates. Helper methods are also available in Haml templates. For example:
56
46
 
57
47
  # file: app/controllers/movies_controller.rb
58
48
 
@@ -62,7 +52,7 @@ For example (this example uses Rails, but the principle for Merb is the same):
62
52
  end
63
53
  end
64
54
 
65
- -# file: app/views/movies/index.haml
55
+ -# file: app/views/movies/index.html.haml
66
56
 
67
57
  #content
68
58
  .title
@@ -78,178 +68,80 @@ may be compiled to:
78
68
  </div>
79
69
  </div>
80
70
 
81
- #### Rails XSS Protection
71
+ ### Rails XSS Protection
82
72
 
83
- Haml supports Rails' XSS protection scheme,
84
- which was introduced in Rails 2.3.5+ and is enabled by default in 3.0.0+.
85
- If it's enabled, Haml's [`:escape_html`](#escape_html-option)
86
- option is set to `true` by default -
87
- like in ERB, all strings printed to a Haml template are escaped by default.
88
- Also like ERB, strings marked as HTML safe are not escaped.
89
- Haml also has [its own syntax for printing a raw string to the template](#unescaping_html).
73
+ Haml supports Rails' XSS protection scheme, which was introduced in Rails 2.3.5+
74
+ and is enabled by default in 3.0.0+. If it's enabled, Haml's
75
+ {Haml::Options#escape_html `:escape_html`} option is set to `true` by default -
76
+ like in ERB, all strings printed to a Haml template are escaped by default. Also
77
+ like ERB, strings marked as HTML safe are not escaped. Haml also has [its own
78
+ syntax for printing a raw string to the template](#unescaping_html).
90
79
 
91
80
  If the `:escape_html` option is set to false when XSS protection is enabled,
92
- Haml doesn't escape Ruby strings by default.
93
- However, if a string marked HTML-safe is passed to [Haml's escaping syntax](#escaping_html),
94
- it won't be escaped.
81
+ Haml doesn't escape Ruby strings by default. However, if a string marked
82
+ HTML-safe is passed to [Haml's escaping syntax](#escaping_html), it won't be
83
+ escaped.
95
84
 
96
- Finally, all the {Haml::Helpers Haml helpers} that return strings
97
- that are known to be HTML safe are marked as such.
98
- In addition, string input is escaped unless it's HTML safe.
85
+ Finally, all the {Haml::Helpers Haml helpers} that return strings that are known
86
+ to be HTML safe are marked as such. In addition, string input is escaped unless
87
+ it's HTML safe.
99
88
 
100
89
  ### Ruby Module
101
90
 
102
- Haml can also be used completely separately from Rails and ActionView.
103
- To do this, install the gem with RubyGems:
91
+ Haml can also be used completely separately from Rails and ActionView. To do
92
+ this, install the gem with RubyGems:
104
93
 
105
94
  gem install haml
106
95
 
107
- You can then use it by including the "haml" gem in Ruby code,
108
- and using {Haml::Engine} like so:
96
+ You can then use it by including the "haml" gem in Ruby code, and using
97
+ {Haml::Engine} like so:
109
98
 
110
99
  engine = Haml::Engine.new("%p Haml code!")
111
100
  engine.render #=> "<p>Haml code!</p>\n"
112
101
 
113
102
  ### Options
114
103
 
115
- Options can be set by setting the {Haml::Template#options Haml::Template.options} hash
116
- in `environment.rb` in Rails...
104
+ Haml understands various configuration options that affect its performance and
105
+ output.
106
+
107
+ In Rails, options can be set by setting the {Haml::Template#options Haml::Template.options}
108
+ hash in an initializer:
117
109
 
110
+ # config/initializers/haml.rb
118
111
  Haml::Template.options[:format] = :html5
119
112
 
120
- ...or by setting the `Merb::Plugin.config[:haml]` hash in `init.rb` in Merb...
121
-
122
- Merb::Plugin.config[:haml][:format] = :html5
123
-
124
- ...or by passing an options hash to {Haml::Engine#initialize}.
125
- Available options are:
126
-
127
- {#format-option} `:format`
128
- : Determines the output format. Normally the default is `:xhtml`,
129
- although under Rails 3 it's `:html5`, since that's the Rails 3's default format.
130
- Other options are `:html4` and `:html5`, which are
131
- identical to `:xhtml` except there are no self-closing tags,
132
- the XML prolog is ignored and correct DOCTYPEs are generated.
133
- <br/><br/> <!-- There's no better way to do a paragraph break in a dl in Maruku -->
134
- If the mime_type of the template being rendered is `text/xml` then
135
- a format of `:xhtml` will be used even if the global output format
136
- is set to `:html4` or `:html5`.
137
-
138
- {#escape_html-option} `:escape_html`
139
- : Sets whether or not to escape HTML-sensitive characters in script.
140
- If this is true, `=` behaves like [`&=`](#escaping_html);
141
- otherwise, it behaves like [`!=`](#unescaping_html).
142
- Note that if this is set, `!=` should be used for yielding to subtemplates
143
- and rendering partials.
144
- See also [Escaping HTML](#escaping_html) and [Unescaping HTML](#unescaping_html)
145
- Defaults to false.
146
-
147
- {#escape_attrs-option} `:escape_attrs`
148
- : Sets whether or not to escape HTML-sensitive characters in attributes.
149
- If this is true, all HTML-sensitive characters in attributes are escaped.
150
- If it's set to false, no HTML-sensitive characters in attributes are escaped.
151
- If it's set to `:once`, existing HTML escape sequences are preserved,
152
- but other HTML-sensitive characters are escaped.
153
- Defaults to `:once`.
154
-
155
- {#ugly-option} `:ugly`
156
- : If set to `true`, Haml makes no attempt to properly
157
- indent or format the HTML output.
158
- This significantly improves rendering performance
159
- but makes viewing the source unpleasant.
160
- Defaults to `true` in Rails production mode, and `false`
161
- everywhere else.
162
-
163
- {#suppress_eval-option} `:suppress_eval`
164
- : Whether or not attribute hashes and Ruby scripts
165
- designated by `=` or `~` should be
166
- evaluated. If this is `true`, said scripts are
167
- rendered as empty strings. Defaults to `false`.
168
-
169
- {#attr_wrapper-option} `:attr_wrapper`
170
- : The character that should wrap element attributes.
171
- This defaults to `'` (an apostrophe). Characters
172
- of this type within the attributes will be escaped
173
- (e.g. by replacing them with `&apos;`) if
174
- the character is an apostrophe or a quotation mark.
175
-
176
- {#hyphenate_data_attrs} `:hyphenate_data_attrs`
177
- : If set to `true`, Haml will convert underscores to hyphens in all
178
- [Custom Data Attributes](#html5_custom_data_attributes)
179
- As of Haml 3.2, this defaults to `true`.
180
-
181
- {#filename-option} `:filename`
182
- : The name of the Haml file being parsed.
183
- This is only used as information when exceptions are raised.
184
- This is automatically assigned when working through ActionView,
185
- so it's really only useful for the user to assign
186
- when dealing with Haml programatically.
187
-
188
- {#line-option} `:line`
189
- : The line offset of the Haml template being parsed.
190
- This is useful for inline templates,
191
- similar to the last argument to `Kernel#eval`.
192
-
193
- {#autoclose-option} `:autoclose`
194
- : A list of tag names that should be automatically self-closed
195
- if they have no content.
196
- This can also contain regular expressions that match tag names
197
- (or any object which responds to `#===`).
198
- Defaults to `['meta', 'img', 'link', 'br', 'hr', 'input', 'area', 'param', 'col', 'base']`.
199
-
200
- {#preserve-option} `:preserve`
201
- : A list of tag names that should automatically have their newlines preserved
202
- using the {Haml::Helpers#preserve} helper.
203
- This means that any content given on the same line as the tag will be preserved.
204
- For example, `%textarea= "Foo\nBar"` compiles to `<textarea>Foo&#x000A;Bar</textarea>`.
205
- Defaults to `['textarea', 'pre']`.
206
- See also [Whitespace Preservation](#whitespace_preservation).
207
-
208
- {#encoding-option} `:encoding`
209
- : The encoding to use for the HTML output.
210
- Only available in Ruby 1.9 or higher.
211
- This can be a string or an `Encoding` Object.
212
- Note that Haml **does not** automatically re-encode Ruby values;
213
- any strings coming from outside the application should be converted
214
- before being passed into the Haml template.
215
- Defaults to `Encoding.default_internal`; if that's not set,
216
- defaults to the encoding of the Haml template;
217
- if that's `us-ascii`, defaults to `"utf-8"`.
218
- <br/><br/> <!-- There's no better way to do a paragraph break in a dl in Maruku -->
219
- Many Ruby database drivers are not yet Ruby 1.9 compatible;
220
- in particular, they return strings marked as ASCII-encoded
221
- even when those strings contain non-ASCII characters (such as UTF-8).
222
- **This will cause encoding errors** if the Haml encoding isn't set to `"ascii-8bit"`.
223
- To solve this, either call `#force_encoding` on all the strings returned from the database,
224
- set `:encoding` to `"ascii-8bit"`, or try to get the authors of the database drivers
225
- to make them Ruby 1.9 compatible.
113
+ Outside Rails, you can set them by configuring them globally in
114
+ Haml::Options.defaults:
115
+
116
+ Haml::Options.defaults[:format] = :html5
117
+
118
+ Finally, you can also set them by passing an options hash to
119
+ {Haml::Engine#initialize}. For the complete list of available options, please
120
+ see {Haml::Options}.
226
121
 
227
122
  ### Encodings
228
123
 
229
- When using Ruby 1.9 or later,
230
- Haml supports the same sorts of encoding-declaration comments that Ruby does.
231
- Although both Ruby and Haml support several different styles,
232
- the easiest it just to add `-# coding: encoding-name`
233
- at the beginning of the Haml template
234
- (it must come before all other lines).
235
- This will tell Haml that the template is encoded using the named encoding.
124
+ When using Ruby 1.9 or later, Haml supports the same sorts of
125
+ encoding-declaration comments that Ruby does. Although both Ruby and Haml
126
+ support several different styles, the easiest it just to add `-# coding:
127
+ encoding-name` at the beginning of the Haml template (it must come before all
128
+ other lines). This will tell Haml that the template is encoded using the named
129
+ encoding.
236
130
 
237
- By default, the HTML generated by Haml has the same encoding as the Haml template.
238
- However, if `Encoding.default_internal` is set, Haml will attempt to use that instead.
239
- In addition, the [`:encoding` option](#encoding-option) can be used
240
- to specify an output encoding manually.
131
+ By default, the HTML generated by Haml has the same encoding as the Haml
132
+ template. However, if `Encoding.default_internal` is set, Haml will attempt to
133
+ use that instead. In addition, the [`:encoding` option](#encoding-option) can be
134
+ used to specify an output encoding manually.
241
135
 
242
- Note that, like Ruby, Haml does not support templates encoded in UTF-16 or UTF-32,
243
- since these encodings are not compatible with ASCII.
244
- It is possible to use these as the output encoding, though.
136
+ Note that, like Ruby, Haml does not support templates encoded in UTF-16 or
137
+ UTF-32, since these encodings are not compatible with ASCII. It is possible to
138
+ use these as the output encoding, though.
245
139
 
246
140
  ## Plain Text
247
141
 
248
- A substantial portion of any HTML document is its content,
249
- which is plain old text.
250
- Any Haml line that's not interpreted as something else
251
- is taken to be plain text, and passed through unmodified.
252
- For example:
142
+ A substantial portion of any HTML document is its content, which is plain old
143
+ text. Any Haml line that's not interpreted as something else is taken to be
144
+ plain text, and passed through unmodified. For example:
253
145
 
254
146
  %gee
255
147
  %whiz
@@ -263,11 +155,9 @@ is compiled to:
263
155
  </whiz>
264
156
  </gee>
265
157
 
266
- Note that HTML tags are passed through unmodified as well.
267
- If you have some HTML you don't want to convert to Haml,
268
- or you're converting a file line-by-line,
269
- you can just include it as-is.
270
- For example:
158
+ Note that HTML tags are passed through unmodified as well. If you have some HTML
159
+ you don't want to convert to Haml, or you're converting a file line-by-line, you
160
+ can just include it as-is. For example:
271
161
 
272
162
  %p
273
163
  <div id="blah">Blah!</div>
@@ -280,9 +170,8 @@ is compiled to:
280
170
 
281
171
  ### Escaping: `\`
282
172
 
283
- The backslash character escapes the first character of a line,
284
- allowing use of otherwise interpreted characters as plain text.
285
- For example:
173
+ The backslash character escapes the first character of a line, allowing use of
174
+ otherwise interpreted characters as plain text. For example:
286
175
 
287
176
  %title
288
177
  = @title
@@ -300,12 +189,10 @@ is compiled to:
300
189
 
301
190
  ### Element Name: `%`
302
191
 
303
- The percent character is placed at the beginning of a line.
304
- It's followed immediately by the name of an element,
305
- then optionally by modifiers (see below), a space,
306
- and text to be rendered inside the element.
307
- It creates an element in the form of `<element></element>`.
308
- For example:
192
+ The percent character is placed at the beginning of a line. It's followed
193
+ immediately by the name of an element, then optionally by modifiers (see below),
194
+ a space, and text to be rendered inside the element. It creates an element in
195
+ the form of `<element></element>`. For example:
309
196
 
310
197
  %one
311
198
  %two
@@ -319,19 +206,16 @@ is compiled to:
319
206
  </two>
320
207
  </one>
321
208
 
322
- Any string is a valid element name;
323
- Haml will automatically generate opening and closing tags for any element.
209
+ Any string is a valid element name; Haml will automatically generate opening and
210
+ closing tags for any element.
324
211
 
325
212
  ### Attributes: `{}` or `()` {#attributes}
326
213
 
327
- Brackets represent a Ruby hash
328
- that is used for specifying the attributes of an element.
329
- It is literally evaluated as a Ruby hash,
330
- so logic will work in it and local variables may be used.
331
- Quote characters within the attribute
332
- will be replaced by appropriate escape sequences.
333
- The hash is placed after the tag is defined.
334
- For example:
214
+ Brackets represent a Ruby hash that is used for specifying the attributes of an
215
+ element. It is literally evaluated as a Ruby hash, so logic will work in it and
216
+ local variables may be used. Quote characters within the attribute will be
217
+ replaced by appropriate escape sequences. The hash is placed after the tag is
218
+ defined. For example:
335
219
 
336
220
  %html{:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", :lang => "en"}
337
221
 
@@ -339,9 +223,8 @@ is compiled to:
339
223
 
340
224
  <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'></html>
341
225
 
342
- Attribute hashes can also be stretched out over multiple lines
343
- to accommodate many attributes.
344
- However, newlines may only be placed immediately after commas.
226
+ Attribute hashes can also be stretched out over multiple lines to accommodate
227
+ many attributes. However, newlines may only be placed immediately after commas.
345
228
  For example:
346
229
 
347
230
  %script{:type => "text/javascript",
@@ -354,11 +237,9 @@ is compiled to:
354
237
  #### `:class` and `:id` Attributes
355
238
  {#class-and-id-attributes}
356
239
 
357
- The `:class` and `:id` attributes can also be specified as a Ruby array
358
- whose elements will be joined together.
359
- A `:class` array is joined with `" "`
360
- and an `:id` array is joined with `"_"`.
361
- For example:
240
+ The `:class` and `:id` attributes can also be specified as a Ruby array whose
241
+ elements will be joined together. A `:class` array is joined with `" "` and an
242
+ `:id` array is joined with `"_"`. For example:
362
243
 
363
244
  %div{:id => [@item.type, @item.number], :class => [@item.type, @item.urgency]}
364
245
 
@@ -366,10 +247,8 @@ is equivalent to:
366
247
 
367
248
  %div{:id => "#{@item.type}_#{@item.number}", :class => "#{@item.type} #{@item.urgency}"}
368
249
 
369
- The array will first be flattened
370
- and any elements that do not test as true will be removed.
371
- The remaining elements will be converted to strings.
372
- For example:
250
+ The array will first be flattened and any elements that do not test as true will
251
+ be removed. The remaining elements will be converted to strings. For example:
373
252
 
374
253
  %div{:class => [@item.type, @item == @sortcol && [:sort, @sortdir]] } Contents
375
254
 
@@ -380,13 +259,11 @@ could render as any of:
380
259
  <div class="sort descending">Contents</div>
381
260
  <div>Contents</div>
382
261
 
383
- depending on whether `@item.type` is `"numeric"` or `nil`,
384
- whether `@item == @sortcol`,
262
+ depending on whether `@item.type` is `"numeric"` or `nil`, whether `@item == @sortcol`,
385
263
  and whether `@sortdir` is `"ascending"` or `"descending"`.
386
264
 
387
265
  If a single value is specified and it evaluates to false it is ignored;
388
- otherwise it gets converted to a string.
389
- For example:
266
+ otherwise it gets converted to a string. For example:
390
267
 
391
268
  .item{:class => @item.is_empty? && "empty"}
392
269
 
@@ -397,15 +274,13 @@ could render as either of:
397
274
 
398
275
  #### HTML-style Attributes: `()`
399
276
 
400
- Haml also supports a terser, less Ruby-specific attribute syntax
401
- based on HTML's attributes.
402
- These are used with parentheses instead of brackets, like so:
277
+ Haml also supports a terser, less Ruby-specific attribute syntax based on HTML's
278
+ attributes. These are used with parentheses instead of brackets, like so:
403
279
 
404
280
  %html(xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en")
405
281
 
406
- Ruby variables can be used by omitting the quotes.
407
- Local variables or instance variables can be used.
408
- For example:
282
+ Ruby variables can be used by omitting the quotes. Local variables or instance
283
+ variables can be used. For example:
409
284
 
410
285
  %a(title=@title href=href) Stuff
411
286
 
@@ -413,28 +288,32 @@ This is the same as:
413
288
 
414
289
  %a{:title => @title, :href => href} Stuff
415
290
 
416
- Because there are no commas separating attributes, though,
417
- more complicated expressions aren't allowed.
418
- For those you'll have to use the `{}` syntax.
419
- You can, however, use both syntaxes together:
291
+ Because there are no commas separating attributes, though, more complicated
292
+ expressions aren't allowed. For those you'll have to use the `{}` syntax. You
293
+ can, however, use both syntaxes together:
420
294
 
421
295
  %a(title=@title){:href => @link.href} Stuff
422
296
 
423
- You can also use `#{}` interpolation to insert complicated expressions
424
- in a HTML-style attribute:
297
+ You can also use `#{}` interpolation to insert complicated expressions in a
298
+ HTML-style attribute:
425
299
 
426
300
  %span(class="widget_#{@widget.number}")
427
301
 
428
- HTML-style attributes can be stretched across multiple lines
429
- just like hash-style attributes:
302
+ HTML-style attributes can be stretched across multiple lines just like
303
+ hash-style attributes:
430
304
 
431
305
  %script(type="text/javascript"
432
306
  src="javascripts/script_#{2 + 7}")
433
307
 
308
+ #### Ruby 1.9-style Hashes
309
+
310
+ On Ruby 1.9, Haml also supports Ruby's new hash syntax:
311
+
312
+ %a{title: @title, href: href} Stuff
313
+
434
314
  #### Attribute Methods
435
315
 
436
- A Ruby method call that returns a hash
437
- can be substituted for the hash contents.
316
+ A Ruby method call that returns a hash can be substituted for the hash contents.
438
317
  For example, {Haml::Helpers} defines the following method:
439
318
 
440
319
  def html_attrs(lang = 'en-US')
@@ -450,11 +329,9 @@ This is compiled to:
450
329
  <html lang='fr-fr' xml:lang='fr-fr' xmlns='http://www.w3.org/1999/xhtml'>
451
330
  </html>
452
331
 
453
- You can use as many such attribute methods as you want
454
- by separating them with commas,
455
- like a Ruby argument list.
456
- All the hashes will me merged together, from left to right.
457
- For example, if you defined
332
+ You can use as many such attribute methods as you want by separating them with
333
+ commas, like a Ruby argument list. All the hashes will me merged together, from
334
+ left to right. For example, if you defined
458
335
 
459
336
  def hash1
460
337
  {:bread => 'white', :filling => 'peanut butter and jelly'}
@@ -479,29 +356,29 @@ Attribute methods aren't supported for HTML-style attributes.
479
356
 
480
357
  #### Boolean Attributes
481
358
 
482
- Some attributes, such as "checked" for `input` tags or "selected" for `option` tags,
483
- are "boolean" in the sense that their values don't matter -
484
- it only matters whether or not they're present.
485
- In HTML (but not XHTML), these attributes can be written as
359
+ Some attributes, such as "checked" for `input` tags or "selected" for `option`
360
+ tags, are "boolean" in the sense that their values don't matter - it only
361
+ matters whether or not they're present. In HTML (but not XHTML), these
362
+ attributes can be written as
486
363
 
487
364
  <input selected>
488
365
 
489
- To do this in Haml using hash-style attributes, just assign a Ruby
490
- `true` value to the attribute:
366
+ To do this in Haml using hash-style attributes, just assign a Ruby `true` value
367
+ to the attribute:
491
368
 
492
369
  %input{:selected => true}
493
370
 
494
371
  In XHTML, the only valid value for these attributes is the name of the
495
- attribute. Thus this will render in XHTML as
372
+ attribute. Thus this will render in XHTML as
496
373
 
497
374
  <input selected='selected'>
498
375
 
499
- To set these attributes to false, simply assign them to a Ruby false value.
500
- In both XHTML and HTML
376
+ To set these attributes to false, simply assign them to a Ruby false value. In
377
+ both XHTML and HTML,
501
378
 
502
379
  %input{:selected => false}
503
380
 
504
- will just render as
381
+ will just render as:
505
382
 
506
383
  <input>
507
384
 
@@ -515,12 +392,12 @@ or using `true` and `false`:
515
392
 
516
393
  #### HTML5 Custom Data Attributes
517
394
 
518
- 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)
519
- to elements using attribute names beginning with `data-`.
520
- Custom data attributes can be used in Haml by using the key `:data` with a Hash value
521
- in an attribute hash.
522
- Each of the key/value pairs in the Hash will be transformed into a custom data attribute.
523
- For example:
395
+ HTML5 allows for adding [custom non-visible data
396
+ attributes](http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data)
397
+ to elements using attribute names beginning with `data-`. Custom data attributes
398
+ can be used in Haml by using the key `:data` with a Hash value in an attribute
399
+ hash. Each of the key/value pairs in the Hash will be transformed into a custom
400
+ data attribute. For example:
524
401
 
525
402
  %a{:href=>"/posts", :data => {:author_id => 123}} Posts By Author
526
403
 
@@ -537,13 +414,11 @@ rendered as:
537
414
 
538
415
  ### Class and ID: `.` and `#`
539
416
 
540
- The period and pound sign are borrowed from CSS.
541
- They are used as shortcuts to specify the `class`
542
- and `id` attributes of an element, respectively.
543
- Multiple class names can be specified in a similar way to CSS,
544
- by chaining the class names together with periods.
545
- They are placed immediately after the tag and before an attributes hash.
546
- For example:
417
+ The period and pound sign are borrowed from CSS. They are used as shortcuts to
418
+ specify the `class` and `id` attributes of an element, respectively. Multiple
419
+ class names can be specified in a similar way to CSS, by chaining the class
420
+ names together with periods. They are placed immediately after the tag and
421
+ before an attributes hash. For example:
547
422
 
548
423
  %div#things
549
424
  %span#rice Chicken Fried
@@ -579,11 +454,10 @@ is compiled to:
579
454
  </div>
580
455
  </div>
581
456
 
582
- These shortcuts can be combined with long-hand attributes;
583
- the two values will be merged together
584
- as though they were all placed in an array
585
- (see [the documentation on `:class` and `:id` attributes](#class-and-id-attributes)).
586
- For example:
457
+ These shortcuts can be combined with long-hand attributes; the two values will
458
+ be merged together as though they were all placed in an array (see [the
459
+ documentation on `:class` and `:id` attributes](#class-and-id-attributes)). For
460
+ example:
587
461
 
588
462
  %div#Article.article.entry{:id => @article.number, :class => @article.visibility}
589
463
 
@@ -597,10 +471,8 @@ and could compile to:
597
471
 
598
472
  #### Implicit Div Elements
599
473
 
600
- Because divs are used so often, they're the default elements.
601
- If you only define a class and/or id using `.` or `#`,
602
- a div is automatically used.
603
- For example:
474
+ Because divs are used so often, they're the default elements. If you only define
475
+ a class and/or id using `.` or `#`, a div is automatically used. For example:
604
476
 
605
477
  #collection
606
478
  .item
@@ -622,9 +494,8 @@ and is compiled to:
622
494
 
623
495
  ### Self-Closing Tags: `/`
624
496
 
625
- The forward slash character, when placed at the end of a tag definition,
626
- causes the tag to be self-closed.
627
- For example:
497
+ The forward slash character, when placed at the end of a tag definition, causes
498
+ the tag to be self-closed. For example:
628
499
 
629
500
  %br/
630
501
  %meta{'http-equiv' => 'Content-Type', :content => 'text/html'}/
@@ -634,10 +505,10 @@ is compiled to:
634
505
  <br />
635
506
  <meta http-equiv='Content-Type' content='text/html' />
636
507
 
637
- Some tags are automatically closed, as long as they have no content.
638
- `meta`, `img`, `link`, `script`, `br`, and `hr` tags are closed by default.
639
- This list can be customized by setting the [`:autoclose`](#autoclose-option) option.
640
- For example:
508
+ Some tags are automatically closed, as long as they have no content. `meta`,
509
+ `img`, `link`, `script`, `br`, and `hr` tags are closed by default. This list
510
+ can be customized by setting the [`:autoclose`](#autoclose-option) option. For
511
+ example:
641
512
 
642
513
  %br
643
514
  %meta{'http-equiv' => 'Content-Type', :content => 'text/html'}
@@ -649,16 +520,13 @@ is also compiled to:
649
520
 
650
521
  ### Whitespace Removal: `>` and `<`
651
522
 
652
- `>` and `<` give you more control over the whitespace near a tag.
653
- `>` will remove all whitespace surrounding a tag,
654
- while `<` will remove all whitespace immediately within a tag.
655
- You can think of them as alligators eating the whitespace:
656
- `>` faces out of the tag and eats the whitespace on the outside,
657
- and `<` faces into the tag and eats the whitespace on the inside.
658
- They're placed at the end of a tag definition,
659
- after class, id, and attribute declarations
660
- but before `/` or `=`.
661
- For example:
523
+ `>` and `<` give you more control over the whitespace near a tag. `>` will
524
+ remove all whitespace surrounding a tag, while `<` will remove all whitespace
525
+ immediately within a tag. You can think of them as alligators eating the
526
+ whitespace: `>` faces out of the tag and eats the whitespace on the outside, and
527
+ `<` faces into the tag and eats the whitespace on the inside. They're placed at
528
+ the end of a tag definition, after class, id, and attribute declarations but
529
+ before `/` or `=`. For example:
662
530
 
663
531
  %blockquote<
664
532
  %div
@@ -704,16 +572,13 @@ is compiled to:
704
572
 
705
573
  ### Object Reference: `[]`
706
574
 
707
- Square brackets follow a tag definition and contain a Ruby object
708
- that is used to set the class and id of that tag.
709
- The class is set to the object's class
710
- (transformed to use underlines rather than camel case)
711
- and the id is set to the object's class, followed by the value
712
- of its `#to_key` or `#id` method (in that order).
713
- This is most useful for elements that represent instances of Models.
714
- Additionally, the second argument (if present) will be used as a prefix for
715
- both the id and class attributes.
716
- For example:
575
+ Square brackets follow a tag definition and contain a Ruby object that is used
576
+ to set the class and id of that tag. The class is set to the object's class
577
+ (transformed to use underlines rather than camel case) and the id is set to the
578
+ object's class, followed by the value of its `#to_key` or `#id` method (in that
579
+ order). This is most useful for elements that represent instances of Active
580
+ Model models. Additionally, the second argument (if present) will be used as a
581
+ prefix for both the id and class attributes. For example:
717
582
 
718
583
  # file: app/controllers/users_controller.rb
719
584
 
@@ -734,8 +599,8 @@ is compiled to:
734
599
  Hello!
735
600
  </div>
736
601
 
737
- If you require that the class be something other than the underscored
738
- object's class, you can implement the `haml_object_ref` method on the object.
602
+ If you require that the class be something other than the underscored object's
603
+ class, you can implement the `haml_object_ref` method on the object.
739
604
 
740
605
  # file: app/models/crazy_user.rb
741
606
 
@@ -759,10 +624,8 @@ is compiled to:
759
624
 
760
625
  ## Doctype: `!!!`
761
626
 
762
- When describing HTML documents with Haml,
763
- you can have a document type or XML prolog generated automatically
764
- by including the characters `!!!`.
765
- For example:
627
+ When describing HTML documents with Haml, you can have a document type or XML
628
+ prolog generated automatically by including the characters `!!!`. For example:
766
629
 
767
630
  !!! XML
768
631
  !!!
@@ -787,8 +650,8 @@ is compiled to:
787
650
  </body>
788
651
  </html>
789
652
 
790
- You can also specify the specific doctype after the `!!!`
791
- When the [`:format`](#format-option) is set to `:xhtml` (the default except in Rails 3),
653
+ You can also specify the specific doctype after the `!!!` When the
654
+ [`:format`](#format-option) is set to `:xhtml` (the default except in Rails 3),
792
655
  the following doctypes are supported:
793
656
 
794
657
  `!!!`
@@ -823,8 +686,8 @@ the following doctypes are supported:
823
686
  : XHTML+RDFa 1.0<br/>
824
687
  `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`
825
688
 
826
- When the [`:format`](#format-option) option is set to `:html4`,
827
- the following doctypes are supported:
689
+ When the [`:format`](#format-option) option is set to `:html4`, the following
690
+ doctypes are supported:
828
691
 
829
692
  `!!!`
830
693
  : HTML 4.01 Transitional<br/>
@@ -841,10 +704,8 @@ the following doctypes are supported:
841
704
  When the [`:format`](#format-option) option is set to `:html5`,
842
705
  `!!!` is always `<!DOCTYPE html>`.
843
706
 
844
- If you're not using the UTF-8 character set for your document,
845
- you can specify which encoding should appear
846
- in the XML prolog in a similar way.
847
- For example:
707
+ If you're not using the UTF-8 character set for your document, you can specify
708
+ which encoding should appear in the XML prolog in a similar way. For example:
848
709
 
849
710
  !!! XML iso-8859-1
850
711
 
@@ -852,21 +713,19 @@ is compiled to:
852
713
 
853
714
  <?xml version='1.0' encoding='iso-8859-1' ?>
854
715
 
855
- If the mime_type of the template being rendered is `text/xml` then
856
- a format of `:xhtml` will be used even if the global output format
857
- is set to `:html4` or `:html5`.
716
+ If the mime_type of the template being rendered is `text/xml` then a format of
717
+ `:xhtml` will be used even if the global output format is set to `:html4` or
718
+ `:html5`.
858
719
 
859
720
  ## Comments
860
721
 
861
- Haml supports two sorts of comments:
862
- those that show up in the HTML output
863
- and those that don't.
722
+ Haml supports two sorts of comments: those that show up in the HTML output and
723
+ those that don't.
864
724
 
865
725
  ### HTML Comments: `/`
866
726
 
867
- The forward slash character, when placed at the beginning of a line,
868
- wraps all text after it in an HTML comment.
869
- For example:
727
+ The forward slash character, when placed at the beginning of a line, wraps all
728
+ text after it in an HTML comment. For example:
870
729
 
871
730
  %peanutbutterjelly
872
731
  / This is the peanutbutterjelly element
@@ -897,9 +756,9 @@ is compiled to:
897
756
 
898
757
  #### Conditional Comments: `/[]`
899
758
 
900
- You can also use [Internet Explorer conditional comments](http://www.quirksmode.org/css/condcom.html)
901
- by enclosing the condition in square brackets after the `/`.
902
- For example:
759
+ You can also use [Internet Explorer conditional
760
+ comments](http://www.quirksmode.org/css/condcom.html) by enclosing the condition
761
+ in square brackets after the `/`. For example:
903
762
 
904
763
  /[if IE]
905
764
  %a{ :href => 'http://www.mozilla.com/en-US/firefox/' }
@@ -915,10 +774,8 @@ is compiled to:
915
774
 
916
775
  ### Haml Comments: `-#`
917
776
 
918
- The hyphen followed immediately by the pound sign
919
- signifies a silent comment.
920
- Any text following this isn't rendered in the resulting document
921
- at all.
777
+ The hyphen followed immediately by the pound sign signifies a silent comment.
778
+ Any text following this isn't rendered in the resulting document at all.
922
779
 
923
780
  For example:
924
781
 
@@ -931,9 +788,8 @@ is compiled to:
931
788
  <p>foo</p>
932
789
  <p>bar</p>
933
790
 
934
- You can also nest text beneath a silent comment.
935
- None of this text will be rendered.
936
- For example:
791
+ You can also nest text beneath a silent comment. None of this text will be
792
+ rendered. For example:
937
793
 
938
794
  %p foo
939
795
  -#
@@ -951,9 +807,8 @@ is compiled to:
951
807
 
952
808
  ### Inserting Ruby: `=`
953
809
 
954
- The equals character is followed by Ruby code.
955
- This code is evaluated and the output is inserted into the document.
956
- For example:
810
+ The equals character is followed by Ruby code. This code is evaluated and the
811
+ output is inserted into the document. For example:
957
812
 
958
813
  %p
959
814
  = ['hi', 'there', 'reader!'].join " "
@@ -966,8 +821,8 @@ is compiled to:
966
821
  yo
967
822
  </p>
968
823
 
969
- If the [`:escape_html`](#escape_html-option) option is set, `=` will sanitize any
970
- HTML-sensitive characters generated by the script. For example:
824
+ If the [`:escape_html`](#escape_html-option) option is set, `=` will sanitize
825
+ any HTML-sensitive characters generated by the script. For example:
971
826
 
972
827
  = '<script>alert("I\'m evil!");</script>'
973
828
 
@@ -980,13 +835,12 @@ For example:
980
835
 
981
836
  %p= "hello"
982
837
 
983
- would be compiled to
838
+ would be compiled to:
984
839
 
985
840
  <p>hello</p>
986
841
 
987
- A line of Ruby code can be stretched over multiple lines
988
- as long as each line but the last ends with a comma.
989
- For example:
842
+ A line of Ruby code can be stretched over multiple lines as long as each line
843
+ but the last ends with a comma. For example:
990
844
 
991
845
  = link_to_remote "Add to cart",
992
846
  :url => { :action => "add", :id => product.id },
@@ -996,12 +850,11 @@ Note that it's illegal to nest code within a tag that ends with `=`.
996
850
 
997
851
  ### Running Ruby: `-`
998
852
 
999
- The hyphen character is also followed by Ruby code.
1000
- This code is evaluated but *not* inserted into the document.
853
+ The hyphen character is also followed by Ruby code. This code is evaluated but
854
+ *not* inserted into the document.
1001
855
 
1002
- **It is not recommended that you use this widely;
1003
- almost all processing code and logic should be restricted
1004
- to the Controller, the Helper, or partials.**
856
+ **It is not recommended that you use this widely; almost all processing code and
857
+ logic should be restricted to Controllers, Helpers, or partials.**
1005
858
 
1006
859
  For example:
1007
860
 
@@ -1016,9 +869,8 @@ is compiled to:
1016
869
  hello there you!
1017
870
  </p>
1018
871
 
1019
- A line of Ruby code can be stretched over multiple lines
1020
- as long as each line but the last ends with a comma.
1021
- For example:
872
+ A line of Ruby code can be stretched over multiple lines as long as each line
873
+ but the last ends with a comma. For example:
1022
874
 
1023
875
  - links = {:home => "/",
1024
876
  :docs => "/docs",
@@ -1027,12 +879,10 @@ For example:
1027
879
  #### Ruby Blocks
1028
880
 
1029
881
  Ruby blocks, like XHTML tags, don't need to be explicitly closed in Haml.
1030
- Rather, they're automatically closed, based on indentation.
1031
- A block begins whenever the indentation is increased
1032
- after a Ruby evaluation command.
1033
- It ends when the indentation decreases
1034
- (as long as it's not an `else` clause or something similar).
1035
- For example:
882
+ Rather, they're automatically closed, based on indentation. A block begins
883
+ whenever the indentation is increased after a Ruby evaluation command. It ends
884
+ when the indentation decreases (as long as it's not an `else` clause or
885
+ something similar). For example:
1036
886
 
1037
887
  - (42...47).each do |i|
1038
888
  %p= i
@@ -1066,8 +916,8 @@ is compiled to:
1066
916
 
1067
917
  ### Whitespace Preservation: `~` {#tilde}
1068
918
 
1069
- `~` works just like `=`, except that it runs {Haml::Helpers#find\_and\_preserve} on its input.
1070
- For example,
919
+ `~` works just like `=`, except that it runs {Haml::Helpers#find\_and\_preserve}
920
+ on its input. For example,
1071
921
 
1072
922
  ~ "Foo\n<pre>Bar\nBaz</pre>"
1073
923
 
@@ -1084,9 +934,8 @@ See also [Whitespace Preservation](#whitespace_preservation).
1084
934
 
1085
935
  ### Ruby Interpolation: `#{}`
1086
936
 
1087
- Ruby code can also be interpolated within plain text using `#{}`,
1088
- similarly to Ruby string interpolation.
1089
- For example,
937
+ Ruby code can also be interpolated within plain text using `#{}`, similarly to
938
+ Ruby string interpolation. For example,
1090
939
 
1091
940
  %p This is #{h quality} cake!
1092
941
 
@@ -1094,34 +943,32 @@ is the same as
1094
943
 
1095
944
  %p= "This is the #{h quality} cake!"
1096
945
 
1097
- and might compile to
946
+ and might compile to:
1098
947
 
1099
948
  <p>This is scrumptious cake!</p>
1100
949
 
1101
- Backslashes can be used to escape `#{}` strings,
1102
- but they don't act as escapes anywhere else in the string.
1103
- For example:
950
+ Backslashes can be used to escape `#{}` strings, but they don't act as escapes
951
+ anywhere else in the string. For example:
1104
952
 
1105
953
  %p
1106
954
  Look at \\#{h word} lack of backslash: \#{foo}
1107
955
  And yon presence thereof: \{foo}
1108
956
 
1109
- might compile to
957
+ might compile to:
1110
958
 
1111
959
  <p>
1112
960
  Look at \yon lack of backslash: #{foo}
1113
961
  And yon presence thereof: \{foo}
1114
962
  </p>
1115
963
 
1116
- Interpolation can also be used within [filters](#filters).
1117
- For example:
964
+ Interpolation can also be used within [filters](#filters). For example:
1118
965
 
1119
966
  :javascript
1120
967
  $(document).ready(function() {
1121
968
  alert(#{@message.to_json});
1122
969
  });
1123
970
 
1124
- might compile to
971
+ might compile to:
1125
972
 
1126
973
  <script type='text/javascript'>
1127
974
  //<![CDATA[
@@ -1133,10 +980,9 @@ might compile to
1133
980
 
1134
981
  ### Escaping HTML: `&=` {#escaping_html}
1135
982
 
1136
- An ampersand followed by one or two equals characters
1137
- evaluates Ruby code just like the equals without the ampersand,
1138
- but sanitizes any HTML-sensitive characters in the result of the code.
1139
- For example:
983
+ An ampersand followed by one or two equals characters evaluates Ruby code just
984
+ like the equals without the ampersand, but sanitizes any HTML-sensitive
985
+ characters in the result of the code. For example:
1140
986
 
1141
987
  &= "I like cheese & crackers"
1142
988
 
@@ -1144,28 +990,26 @@ compiles to
1144
990
 
1145
991
  I like cheese &amp; crackers
1146
992
 
1147
- If the [`:escape_html`](#escape_html-option) option is set,
1148
- `&=` behaves identically to `=`.
993
+ If the {Haml::Options#escape_html `:escape_html`} option is set, `&=` behaves
994
+ identically to `=`.
1149
995
 
1150
- `&` can also be used on its own so that `#{}` interpolation is escaped.
1151
- For example,
996
+ `&` can also be used on its own so that `#{}` interpolation is escaped. For
997
+ example,
1152
998
 
1153
999
  & I like #{"cheese & crackers"}
1154
1000
 
1155
- compiles to
1001
+ compiles to:
1156
1002
 
1157
1003
  I like cheese &amp; crackers
1158
1004
 
1159
1005
  ### Unescaping HTML: `!=` {#unescaping_html}
1160
1006
 
1161
- An exclamation mark followed by one or two equals characters
1162
- evaluates Ruby code just like the equals would,
1163
- but never sanitizes the HTML.
1007
+ An exclamation mark followed by one or two equals characters evaluates Ruby code
1008
+ just like the equals would, but never sanitizes the HTML.
1164
1009
 
1165
- By default, the single equals doesn't sanitize HTML either.
1166
- However, if the [`:escape_html`](#escape_html-option) option is set,
1167
- `=` will sanitize the HTML, but `!=` still won't.
1168
- For example, if `:escape_html` is set:
1010
+ By default, the single equals doesn't sanitize HTML either. However, if the
1011
+ {Haml::Options#escape_html `:escape_html`} option is set, `=` will sanitize the
1012
+ HTML, but `!=` still won't. For example, if `:escape_html` is set:
1169
1013
 
1170
1014
  = "I feel <strong>!"
1171
1015
  != "I feel <strong>!"
@@ -1189,7 +1033,7 @@ compiles to
1189
1033
  The colon character designates a filter. This allows you to pass an indented
1190
1034
  block of text as input to another filtering program and add the result to the
1191
1035
  output of Haml. The syntax is simply a colon followed by the name of the filter.
1192
- For example,
1036
+ For example:
1193
1037
 
1194
1038
  %p
1195
1039
  :markdown
@@ -1197,7 +1041,7 @@ For example,
1197
1041
 
1198
1042
  Hello, *World*
1199
1043
 
1200
- is compiled to
1044
+ is compiled to:
1201
1045
 
1202
1046
  <p>
1203
1047
  <h1>Greetings</h1>
@@ -1205,7 +1049,7 @@ is compiled to
1205
1049
  <p>Hello, <em>World</em></p>
1206
1050
  </p>
1207
1051
 
1208
- Filters can have Ruby code interpolated with `#{}`. For example,
1052
+ Filters can have Ruby code interpolated with `#{}`. For example:
1209
1053
 
1210
1054
  - flavor = "raspberry"
1211
1055
  #content
@@ -1218,16 +1062,16 @@ is compiled to
1218
1062
  <p>I <strong>really</strong> prefer <em>raspberry</em> jam.</p>
1219
1063
  </div>
1220
1064
 
1221
- Currently, filters ignore the [`:escape_html`](#escape_html-option) option. This
1222
- means that `#{}` interpolation within filters is never HTML-escaped.
1065
+ Currently, filters ignore the {Haml::Options#escape_html `:escape_html`} option.
1066
+ This means that `#{}` interpolation within filters is never HTML-escaped.
1223
1067
 
1224
1068
  The functionality of some filters such as Markdown can be provided by many
1225
1069
  different libraries. Usually you don't have to worry about this - you can just
1226
1070
  load the gem of your choice and Haml will automatically use it.
1227
1071
 
1228
- However in some cases you may want to make Haml explicitly use a specific gem
1229
- to be used by a filter. In these cases you can do this via Tilt, the library
1230
- Haml uses to implement many filters:
1072
+ However in some cases you may want to make Haml explicitly use a specific gem to
1073
+ be used by a filter. In these cases you can do this via Tilt, the library Haml
1074
+ uses to implement many of its filters:
1231
1075
 
1232
1076
  Tilt.prefer Tilt::RedCarpetTemplate
1233
1077