We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
hcatlin (author)
Sat Oct 14 15:44:46 -0700 2006
commit  6fdffe1d7888e3db9b7d0dae67420ac17f94334f
tree    83bf3f8c221a76df69f326bff29b886d032a96dc
parent  4266688e30051e9649bb491e9b248b2dbb8e35d3
haml / README
100644 472 lines (348 sloc) 10.753 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
= Haml (XHTML Abstraction Markup Language)
 
HAML is a markup language that's used to cleanly and simply describe the XHTML
of any web document without the use of inline code. Haml functions as a
replacement for inline page templating systems such PHP, RHTML, and ASP.
However, Haml avoids the need for explicitly coding XHTML into the template,
because it iself is a description of the XHTML, with some code to generate
dynamic content.
 
== Features
 
* Whitespace active
* Well-formatted markup
* DRY
* Follows CSS conventions
* Interpolates Ruby code
* Implements Rails templates with the .haml extension
 
== Authors
 
HAML was originally created by Hampton Catlin (hcatlin). Help with the
Ruby On Rails implementation and much of the documentation by
Jeff Hardy (packagethief).
 
Nathan Weizenbaum (Nex3) contribued the buffered-engine code along with many
other enhancements including the silent-line syntax ("-").
 
If you use this software, you must pay Hampton a compliment. Say something
nice about it. Beyond that, the implementation is licensed under the MIT
License. Ok, fine, I guess that means compliments aren't *required*.
 
== Formatting
 
Haml is sensitive to spacing and indentation; it uses nesting to convey
structure. When you want an element to have children, indent the lines below it
using two spaces. Remember, spaces are not the same as tabs. Example:
 
  #contact
    %h1 Eugene Mumbai
    %ul.info
      %li.login eugene
      %li.email eugene@example.com
 
is compiled to:
 
  <div id='contact'>
    <h1>Eugene Mumbai</h1>
    <ul class='info'>
      <li class='login'>eugene</li>
      <li class='email'>eugene@example.com</li>
    </ul>
  </div>
  
== Characters with meaning to Haml
 
Various characters, when placed at a certain point in a line, instruct HAML
to render different types of things.
 
=== XHTML Tags
 
These characters render XHTML tags.
 
==== %
 
This element 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
<tt><element></element></tt>. For example:
 
  %one
    %two
      %three Hey there
      
is compiled to:
 
  <one>
    <two>
      <three>Hey there</three>
    </two>
  </one>
 
Any string is a valid element name; Haml will automatically generate opening and
closing tags for any element.
 
==== {}
 
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. At
the moment, though, it doesn't see local variables. The hash is placed after
the tag is defined. For example:
 
  %head{ :name => "doc_head" }
    %script{ 'type' => "text/" + "javascript", :src => "javascripts/script_#{2 + 7}" }
 
is compiled to:
 
  <head name="doc_head">
    <script src='javascripts/script_9' type='text/javascript'>
    </script>
  </head>
  
==== []
 
Square brackets follow a tag definiton 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 its id. Because the id of an object is normally an
obscure implementation detail, this is most useful for elements that represent
instances of Models. For example:
 
  # file: app/controllers/users_controller.rb
  
  def show
    @user = CrazyUser.find(15)
  end
  
  # file: app/views/users/show.haml
 
  %div[@user]
    %bar[290]/
    Hello!
 
is compiled to:
 
  <div class="crazy_user" id="crazy_user_15">
    <bar class="fixnum" id="fixnum_581" />
    Hello!
  </div>
 
This is based off of DHH's SimplyHelpful syntax as presented at RailsConf Europe 2006.
 
==== /
 
The forward slash character, when placed at the end of a tag definition, causes
the tag to be self-closed. For example:
 
  %br/
  %meta{:http-equiv => 'Content-Type', :content => 'text/html'}/
 
is compiled to:
 
  <br />
  <meta http-equiv='Content-Type' content='text/html' />
  
==== . and #
 
The period and pound sign are borrowed from CSS and used as shortcuts to specify the
<tt>class</tt> and <tt>id</tt> attributes of an element, respectively. They are
placed immediately after the tag, and before an attributes hash. For example:
 
  div#things
    %span#rice Chicken Fried
    %p.beans{ :food => 'true' } The magical fruit
    %h1.class#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' id='id'>La La La</h1>
  </div>
  
==== Assumed Divs
 
Because the div element is used so often, it is the default element. If you only
define a class and/or id using the <tt>.</tt> or <tt>#</tt> syntax, a div element
is automatically used. For example:
 
  #collection
    .item
      .description What a cool item!
 
is the same as:
 
  %div{:id => collection}
    %div{:class => 'item'}
      %div{:class => 'description'} What a cool item!
 
and is compiled to:
 
  <div id='collection'>
    <div class='item'>Broken record album</div>
    <div class='description'>What a cool item!</div>
  </div>
  
==== = and ~
 
<tt>=</tt> and <tt>~</tt> are placed at the end of a tag definition, after class,
id, and attribute declarations. They're just shortcuts for inserting Ruby code
into an element. They work the same as <tt>=</tt> and <tt>~</tt> without a tag;
see below for documentation of those. For example:
 
  %p= "hello"
  %h1~ 1 + 2
  
is the same as:
 
  %p
    = "hello"
  %h1
    ~ 1 + 2
 
and is compiled to:
 
  <p>
    hello
  </p>
  <h1>
    3
  </h1>
  
=== XHTML Helpers
 
==== No Special Character
 
If no special character appears at the beginning of a line, it is rendered as plain
text. For example:
 
  %gee
    %whiz
      Wow this is cool!
 
is compiled to:
 
  <gee>
    <whiz>
      Wow this is cool!
    </whiz>
  </gee>
  
==== !!!
 
When describing XHTML documents with Haml, you can have a document type
generated automatically by including the characters <tt>!!!</tt> as the first
line in your document. Example:
 
  !!!
  %html
    %head
      %title Myspace
    %body
      %h1 I am the international space station
      %p Sign my guestbook
 
is compiled to:
 
  <!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>
  
==== /
 
The forward slash character, when placed at the beginning of a line, wraps all
text after it in an HTML comment. For example:
 
  %billabong
    / This is the billabong element
    I like billabongs!
 
is compiled to:
 
  <billabong>
    <!-- This is the billabong element -->
    I like billabongs!
  </billabong>
  
==== |
 
The pipe character designates a multiline string. It's placed at the end of a line,
and means that all following lines that end with <tt>|</tt> will be evaluated as
though they were on the same line. For example:
 
  %whoo
    %hoo I think this might get |
      pretty long so I should |
      probably make it |
      multiline so it doesn't |
      look awful. |
    %p This is short.
 
is compiled to:
 
  %hoo I think this might get |
    pretty long so I should |
    probably make it |
    multiline so it doesn't |
    look awful. |
  
=== Ruby evaluators
 
==== =
 
The equals character is followed by Ruby code, which is evaluated and the output
inserted into the document as plain text. For example:
 
  %p
    = ['hi', 'there', 'reader!'].join " "
    = "yo"
 
is compiled to:
 
  <p>
    hi there reader!
    yo
  </p>
  
==== ~
 
The tilde character works the same as the equals character, but the output is
modified in such a way that newlines in whitespace-sensitive elements work
properly. For example:
 
  %foo
    = "Woah <pre> this is \n</pre> crazy"
  %foo2
    ~ "Woah <pre> this is \n</pre> crazy"
      
is compiled to:
 
  <foo>
    Woah <pre> this is
    </pre> crazy
  </foo>
  <foo2>
    Woah <pre> this is &#x000A;</pre> crazy
  </foo2>
  
==== -
 
The hyphen character makes the text following it into "silent script", or
Ruby script that is evaluated, but not output.
 
<b>It is not reccomended that you use this widely; almost all processing
code and logic should be kept to the Controller, the Helper, or partials.</b>
 
For example:
 
  - foo = "hello"
  - foo << " there"
  - foo << " you!"
  %p= foo
  
is compiled to:
 
  <p>
    hello there you!
  </p>
  
===== Blocks
 
Like XHTML tags, you don't need to explicity close your Ruby blocks in
HAML. Rather, they're automatically closed based on tabs. A block begins
whenever the indentation is increased after a silent script command, and
ends when the indentation decreases (as long as it's not an +else+ clause
or something similar). For example:
 
  - (42...47).each do |i|
    %p= i
  %p See, I can count!
  
is compiled to:
 
  <p>
    42
  </p>
  <p>
    43
  </p>
  <p>
    44
  </p>
  <p>
    45
  </p>
  <p>
    46
  </p>
  
Another example:
 
  %p
    - case 2
    - when 1
      = "1!"
    - when 2
      = "2?"
    - when 3
      = "3."
      
is compiled to:
 
  <p>
    2?
  </p>
 
== Using Haml as a Rails plugin
 
Write Rails templates with the .haml extension. Example:
 
  # file: app/views/movies/teen_wolf.haml
  
  %html
    %head
      %title= "Teen Wolf (1985)"
    %body
      #contents
        %h1 "A highschooler discovers that he is a werewolf"
        %ul.cast
          %li "Scott Howard"
          %li "Rupert 'Stiles' Stilinski"
          %li "Lisa 'Boof' Marconi"
          %li "Lewis"
 
is compiled to:
 
  <html>
    <head>
      <title>Teen Wolf (1985)</title>
    </head>
    <body>
      <div id='contents'>
        <h1>A highschooler discovers that he is a werewolf</h1>
        <ul class='cast'>
          <li>Scott Howard</li>
          <li>Rupert 'Stiles' Stilinski</li>
          <li>Lisa 'Boof' Marconi</li>
          <li>Lewis</li>
        </ul>
      </div>
    </body>
  </html>
 
You can access instance variables in Haml templates the same way you do in ERb
templates. Helper methods are also available in Haml templates. Example:
 
  # file: app/controllers/movies_controller.rb
 
  class MoviesController < ApplicationController
    def index
      @title = "Teen Wolf"
    end
  end
 
  # file: app/views/movies/index.haml
 
  #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>
 
 
---
Copyright (c) 2006 Hampton Catlin
Licensed under the MIT License