This file is indexed.

/usr/share/doc/ruby-haml/yardoc/file.REFERENCE.html is in ruby-haml 4.0.7-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>
  File: REFERENCE
  
    &mdash; Haml Documentation
  
</title>

  <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />

  <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  hasFrames = window.top.frames.main ? true : false;
  relpath = '';
  framesUrl = "frames.html#!file.REFERENCE.html";
</script>


  <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>

  <script type="text/javascript" charset="utf-8" src="js/app.js"></script>


  </head>
  <body>
    <div id="header">
      <div id="menu">
  
    <a href="_index.html">Index</a> &raquo; 
    <span class="title">File: REFERENCE</span>
  

  <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>

      <div id="search">
  
    <a class="full_list_link" id="class_list_link"
        href="class_list.html">
      Class List
    </a>
  
    <a class="full_list_link" id="method_list_link"
        href="method_list.html">
      Method List
    </a>
  
    <a class="full_list_link" id="file_list_link"
        href="file_list.html">
      File List
    </a>
  
</div>
      <div class="clear"></div>
    </div>

    <iframe id="search_frame"></iframe>

    <div id="content"><div id='filecontents'>
<h1 id="haml_html_abstraction_markup_language">Haml (HTML Abstraction Markup Language)</h1>

<p>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 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.</p>

<h2 id="features">Features</h2>

<ul>
<li>Whitespace active</li>

<li>Well-formatted markup</li>

<li>DRY</li>

<li>Follows CSS conventions</li>

<li>Integrates Ruby code</li>

<li>Implements Rails templates with the .haml extension</li>
</ul>

<h2 id="using_haml">Using Haml</h2>

<p>Haml can be used in three ways:</p>

<ul>
<li>as a command-line tool,</li>

<li>as a plugin for Ruby on Rails,</li>

<li>and as a standalone Ruby module.</li>
</ul>

<p>The first step for all of these is to install the Haml gem:</p>

<pre class="code ruby"><code class="ruby">gem install haml</code></pre>

<p>To run Haml from the command line, just use</p>

<pre class="code ruby"><code class="ruby">haml input.haml output.html</code></pre>

<p>Use <code>haml --help</code> for full documentation.</p>

<p>To use Haml with Rails, add the following line to the Gemfile:</p>

<pre class="code ruby"><code class="ruby">gem &quot;haml&quot;</code></pre>

<p>Once it’s installed, all view files with the <code>&quot;.html.haml&quot;</code> extension will be compiled using Haml.</p>

<p>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:</p>

<pre class="code ruby"><code class="ruby"># file: app/controllers/movies_controller.rb

class MoviesController &lt; ApplicationController
  def index
    @title = &quot;Teen Wolf&quot;
  end
end

-# file: app/views/movies/index.html.haml

#content
 .title
   %h1= @title
   = link_to &#39;Home&#39;, home_url</code></pre>

<p>may be compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;div id=&#39;content&#39;&gt;
  &lt;div class=&#39;title&#39;&gt;
    &lt;h1&gt;Teen Wolf&lt;/h1&gt;
    &lt;a href=&#39;/&#39;&gt;Home&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;</code></pre>

<h3 id="rails_xss_protection">Rails XSS Protection</h3>

<p>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 <span class='object_link'><a href="Haml/Options.html#escape_html-instance_method" title="Haml::Options#escape_html (method)"><code>:escape_html</code></a></span> option is set to <code>true</code> 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 <a href="#unescaping_html">its own syntax for printing a raw string to the template</a>.</p>

<p>If the <code>:escape_html</code> option is set to false when XSS protection is enabled, Haml doesn’t escape Ruby strings by default. However, if a string marked HTML-safe is passed to <a href="#escaping_html">Haml’s escaping syntax</a>, it won’t be escaped.</p>

<p>Finally, all the <span class='object_link'><a href="Haml/Helpers.html" title="Haml::Helpers (module)">Haml helpers</a></span> 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.</p>

<h3 id="ruby_module">Ruby Module</h3>

<p>Haml can also be used completely separately from Rails and ActionView. To do this, install the gem with RubyGems:</p>

<pre class="code ruby"><code class="ruby">gem install haml</code></pre>

<p>You can then use it by including the “haml” gem in Ruby code, and using <span class='object_link'><a href="Haml/Engine.html" title="Haml::Engine (class)">Haml::Engine</a></span> like so:</p>

<pre class="code ruby"><code class="ruby">engine = Haml::Engine.new(&quot;%p Haml code!&quot;)
engine.render #=&gt; &quot;&lt;p&gt;Haml code!&lt;/p&gt;\n&quot;</code></pre>

<h3 id="options">Options</h3>

<p>Haml understands various configuration options that affect its performance and output.</p>

<p>In Rails, options can be set by setting the <span class='object_link'><a href="Haml/Template.html#options-instance_method" title="Haml::Template#options (method)">Haml::Template.options</a></span> hash in an initializer:</p>

<pre class="code ruby"><code class="ruby"># config/initializers/haml.rb
Haml::Template.options[:format] = :html5</code></pre>

<p>Outside Rails, you can set them by configuring them globally in Haml::Options.defaults:</p>

<pre class="code ruby"><code class="ruby">Haml::Options.defaults[:format] = :html5</code></pre>

<p>Finally, you can also set them by passing an options hash to <span class='object_link'><a href="Haml/Engine.html#initialize-instance_method" title="Haml::Engine#initialize (method)">Haml::Engine#initialize</a></span>. For the complete list of available options, please see <span class='object_link'><a href="Haml/Options.html" title="Haml::Options (class)">Haml::Options</a></span>.</p>

<h3 id="encodings">Encodings</h3>

<p>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 <code>-# coding:
encoding-name</code> 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.</p>

<p>By default, the HTML generated by Haml has the same encoding as the Haml template. However, if <code>Encoding.default_internal</code> is set, Haml will attempt to use that instead. In addition, the <span class='object_link'><a href="Haml/Options.html#encoding-instance_method" title="Haml::Options#encoding (method)"><code>:encoding</code> option</a></span> can be used to specify an output encoding manually.</p>

<p>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.</p>

<h2 id="plain_text">Plain Text</h2>

<p>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:</p>

<pre class="code ruby"><code class="ruby">%gee
  %whiz
    Wow this is cool!</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;gee&gt;
  &lt;whiz&gt;
    Wow this is cool!
  &lt;/whiz&gt;
&lt;/gee&gt;</code></pre>

<p>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:</p>

<pre class="code ruby"><code class="ruby">%p
  &lt;div id=&quot;blah&quot;&gt;Blah!&lt;/div&gt;</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;
  &lt;div id=&quot;blah&quot;&gt;Blah!&lt;/div&gt;
&lt;/p&gt;</code></pre>

<h3 id="escaping_">Escaping: <code>\</code></h3>

<p>The backslash character escapes the first character of a line, allowing use of otherwise interpreted characters as plain text. For example:</p>

<pre class="code ruby"><code class="ruby">%title
  = @title
  \= @title</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;title&gt;
  MyPage
  = @title
&lt;/title&gt;</code></pre>

<h2 id="html_elements">HTML Elements</h2>

<h3 id="element_name_">Element Name: <code>%</code></h3>

<p>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 <code>&lt;element&gt;&lt;/element&gt;</code>. For example:</p>

<pre class="code ruby"><code class="ruby">%one
  %two
    %three Hey there</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;one&gt;
  &lt;two&gt;
    &lt;three&gt;Hey there&lt;/three&gt;
  &lt;/two&gt;
&lt;/one&gt;</code></pre>

<p>Any string is a valid element name; Haml will automatically generate opening and closing tags for any element.</p>

<h3 id="attributes_">Attributes: `</h3>

<p>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:</p>

<pre class="code ruby"><code class="ruby">%html{:xmlns =&gt; &quot;http://www.w3.org/1999/xhtml&quot;, &quot;xml:lang&quot; =&gt; &quot;en&quot;, :lang =&gt; &quot;en&quot;}</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;html xmlns=&#39;http://www.w3.org/1999/xhtml&#39; xml:lang=&#39;en&#39; lang=&#39;en&#39;&gt;&lt;/html&gt;</code></pre>

<p>Attribute hashes can also be stretched out over multiple lines to accommodate many attributes. However, newlines may only be placed immediately after commas. For example:</p>

<pre class="code ruby"><code class="ruby">%script{:type =&gt; &quot;text/javascript&quot;,
        :src  =&gt; &quot;javascripts/script_#{2 + 7}&quot;}</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;script src=&#39;javascripts/script_9&#39; type=&#39;text/javascript&#39;&gt;&lt;/script&gt;</code></pre>

<h4 id="class-and-id-attributes"><code>:class</code> and <code>:id</code> Attributes</h4>

<p>The <code>:class</code> and <code>:id</code> attributes can also be specified as a Ruby array whose elements will be joined together. A <code>:class</code> array is joined with <code>&quot; &quot;</code> and an <code>:id</code> array is joined with <code>&quot;_&quot;</code>. For example:</p>

<pre class="code ruby"><code class="ruby">%div{:id =&gt; [@item.type, @item.number], :class =&gt; [@item.type, @item.urgency]}</code></pre>

<p>is equivalent to:</p>

<pre class="code ruby"><code class="ruby">%div{:id =&gt; &quot;#{@item.type}_#{@item.number}&quot;, :class =&gt; &quot;#{@item.type} #{@item.urgency}&quot;}</code></pre>

<p>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:</p>

<pre class="code ruby"><code class="ruby">%div{:class =&gt; [@item.type, @item == @sortcol &amp;&amp; [:sort, @sortdir]] } Contents</code></pre>

<p>could render as any of:</p>

<pre class="code ruby"><code class="ruby">&lt;div class=&quot;numeric sort ascending&quot;&gt;Contents&lt;/div&gt;
&lt;div class=&quot;numeric&quot;&gt;Contents&lt;/div&gt;
&lt;div class=&quot;sort descending&quot;&gt;Contents&lt;/div&gt;
&lt;div&gt;Contents&lt;/div&gt;</code></pre>

<p>depending on whether <code>@item.type</code> is <code>&quot;numeric&quot;</code> or <code>nil</code>, whether <code>@item == @sortcol</code>, and whether <code>@sortdir</code> is <code>&quot;ascending&quot;</code> or <code>&quot;descending&quot;</code>.</p>

<p>If a single value is specified and it evaluates to false it is ignored; otherwise it gets converted to a string. For example:</p>

<pre class="code ruby"><code class="ruby">.item{:class =&gt; @item.is_empty? &amp;&amp; &quot;empty&quot;}</code></pre>

<p>could render as either of:</p>

<pre class="code ruby"><code class="ruby">class=&quot;item&quot;
class=&quot;item empty&quot;</code></pre>

<h4 id="htmlstyle_attributes_">HTML-style Attributes: <code>()</code></h4>

<p>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:</p>

<pre class="code ruby"><code class="ruby">%html(xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;)</code></pre>

<p>Ruby variables can be used by omitting the quotes. Local variables or instance variables can be used. For example:</p>

<pre class="code ruby"><code class="ruby">%a(title=@title href=href) Stuff</code></pre>

<p>This is the same as:</p>

<pre class="code ruby"><code class="ruby">%a{:title =&gt; @title, :href =&gt; href} Stuff</code></pre>

<p>Because there are no commas separating attributes, though, more complicated expressions aren’t allowed. For those you’ll have to use the <code>{}</code> syntax. You can, however, use both syntaxes together:</p>

<pre class="code ruby"><code class="ruby">%a(title=@title){:href =&gt; @link.href} Stuff</code></pre>

<p>You can also use <code>#{}</code> interpolation to insert complicated expressions in a HTML-style attribute:</p>

<pre class="code ruby"><code class="ruby">%span(class=&quot;widget_#{@widget.number}&quot;)</code></pre>

<p>HTML-style attributes can be stretched across multiple lines just like hash-style attributes:</p>

<pre class="code ruby"><code class="ruby">%script(type=&quot;text/javascript&quot;
        src=&quot;javascripts/script_#{2 + 7}&quot;)</code></pre>

<h4 id="ruby_19style_hashes">Ruby 1.9-style Hashes</h4>

<p>On Ruby 1.9, Haml also supports Ruby’s new hash syntax:</p>

<pre class="code ruby"><code class="ruby">%a{title: @title, href: href} Stuff</code></pre>

<h4 id="attribute_methods">Attribute Methods</h4>

<p>A Ruby method call that returns a hash can be substituted for the hash contents. For example, <span class='object_link'><a href="Haml/Helpers.html" title="Haml::Helpers (module)">Haml::Helpers</a></span> defines the following method:</p>

<pre class="code ruby"><code class="ruby">def html_attrs(lang = &#39;en-US&#39;)
  {:xmlns =&gt; &quot;http://www.w3.org/1999/xhtml&quot;, &#39;xml:lang&#39; =&gt; lang, :lang =&gt; lang}
end</code></pre>

<p>This can then be used in Haml, like so:</p>

<pre class="code ruby"><code class="ruby">%html{html_attrs(&#39;fr-fr&#39;)}</code></pre>

<p>This is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;html lang=&#39;fr-fr&#39; xml:lang=&#39;fr-fr&#39; xmlns=&#39;http://www.w3.org/1999/xhtml&#39;&gt;
&lt;/html&gt;</code></pre>

<p>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 be merged together, from left to right. For example, if you defined</p>

<pre class="code ruby"><code class="ruby">def hash1
  {:bread =&gt; &#39;white&#39;, :filling =&gt; &#39;peanut butter and jelly&#39;}
end

def hash2
  {:bread =&gt; &#39;whole wheat&#39;}
end</code></pre>

<p>then</p>

<pre class="code ruby"><code class="ruby">%sandwich{hash1, hash2, :delicious =&gt; &#39;true&#39;}/</code></pre>

<p>would compile to:</p>

<pre class="code ruby"><code class="ruby">&lt;sandwich bread=&#39;whole wheat&#39; delicious=&#39;true&#39; filling=&#39;peanut butter and jelly&#39; /&gt;</code></pre>

<p>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.</p>

<p>Attribute methods aren’t supported for HTML-style attributes.</p>

<h4 id="boolean_attributes">Boolean Attributes</h4>

<p>Some attributes, such as “checked” for <code>input</code> tags or “selected” for <code>option</code> 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</p>

<pre class="code ruby"><code class="ruby">&lt;input selected&gt;</code></pre>

<p>To do this in Haml using hash-style attributes, just assign a Ruby <code>true</code> value to the attribute:</p>

<pre class="code ruby"><code class="ruby">%input{:selected =&gt; true}</code></pre>

<p>In XHTML, the only valid value for these attributes is the name of the attribute. Thus this will render in XHTML as</p>

<pre class="code ruby"><code class="ruby">&lt;input selected=&#39;selected&#39;&gt;</code></pre>

<p>To set these attributes to false, simply assign them to a Ruby false value. In both XHTML and HTML,</p>

<pre class="code ruby"><code class="ruby">%input{:selected =&gt; false}</code></pre>

<p>will just render as:</p>

<pre class="code ruby"><code class="ruby">&lt;input&gt;</code></pre>

<p>HTML-style boolean attributes can be written just like HTML:</p>

<pre class="code ruby"><code class="ruby">%input(selected)</code></pre>

<p>or using <code>true</code> and <code>false</code>:</p>

<pre class="code ruby"><code class="ruby">%input(selected=true)</code></pre>

<h4 id="html5_custom_data_attributes">HTML5 Custom Data Attributes</h4>

<p>HTML5 allows for adding <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data">custom non-visible data attributes</a> to elements using attribute names beginning with <code>data-</code>. Custom data attributes can be used in Haml by using the key <code>:data</code> 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:</p>

<pre class="code ruby"><code class="ruby">%a{:href=&gt;&quot;/posts&quot;, :data =&gt; {:author_id =&gt; 123}} Posts By Author</code></pre>

<p>will render as:</p>

<pre class="code ruby"><code class="ruby">&lt;a data-author-id=&#39;123&#39; href=&#39;/posts&#39;&gt;Posts By Author&lt;/a&gt;</code></pre>

<p>Notice that the underscore in <code>author_id</code> was replaced by a hyphen. If you wish to suppress this behavior, you can set Haml’s <span class='object_link'><a href="Haml/Options.html#hyphenate_data_attrs-instance_method" title="Haml::Options#hyphenate_data_attrs (method)"><code>:hyphenate_data_attrs</code> option</a></span> to <code>false</code>, and the output will be rendered as:</p>

<pre class="code ruby"><code class="ruby">&lt;a data-author_id=&#39;123&#39; href=&#39;/posts&#39;&gt;Posts By Author&lt;/a&gt;</code></pre>

<h3 id="class_and_id__and_">Class and ID: <code>.</code> and <code>#</code></h3>

<p>The period and pound sign are borrowed from CSS. They are used as shortcuts to specify the <code>class</code> and <code>id</code> 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:</p>

<pre class="code ruby"><code class="ruby">%div#things
  %span#rice Chicken Fried
  %p.beans{ :food =&gt; &#39;true&#39; } The magical fruit
  %h1.class.otherclass#id La La La</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;div id=&#39;things&#39;&gt;
  &lt;span id=&#39;rice&#39;&gt;Chicken Fried&lt;/span&gt;
  &lt;p class=&#39;beans&#39; food=&#39;true&#39;&gt;The magical fruit&lt;/p&gt;
  &lt;h1 class=&#39;class otherclass&#39; id=&#39;id&#39;&gt;La La La&lt;/h1&gt;
&lt;/div&gt;</code></pre>

<p>And,</p>

<pre class="code ruby"><code class="ruby">%div#content
  %div.articles
    %div.article.title Doogie Howser Comes Out
    %div.article.date 2006-11-05
    %div.article.entry
      Neil Patrick Harris would like to dispel any rumors that he is straight</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;div id=&#39;content&#39;&gt;
  &lt;div class=&#39;articles&#39;&gt;
    &lt;div class=&#39;article title&#39;&gt;Doogie Howser Comes Out&lt;/div&gt;
    &lt;div class=&#39;article date&#39;&gt;2006-11-05&lt;/div&gt;
    &lt;div class=&#39;article entry&#39;&gt;
      Neil Patrick Harris would like to dispel any rumors that he is straight
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;</code></pre>

<p>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 <a href="#class-and-id-attributes">the documentation on <code>:class</code> and <code>:id</code> attributes</a>). For example:</p>

<pre class="code ruby"><code class="ruby">%div#Article.article.entry{:id =&gt; @article.number, :class =&gt; @article.visibility}</code></pre>

<p>is equivalent to</p>

<pre class="code ruby"><code class="ruby">%div{:id =&gt; [&#39;Article&#39;, @article.number], :class =&gt; [&#39;article&#39;, &#39;entry&#39;, @article.visibility]} Gabba Hey</code></pre>

<p>and could compile to:</p>

<pre class="code ruby"><code class="ruby">&lt;div class=&quot;article entry visible&quot; id=&quot;Article_27&quot;&gt;Gabba Hey&lt;/div&gt;</code></pre>

<h4 id="implicit_div_elements">Implicit Div Elements</h4>

<p>Because divs are used so often, they’re the default elements. If you only define a class and/or id using <code>.</code> or <code>#</code>, a div is automatically used. For example:</p>

<pre class="code ruby"><code class="ruby">#collection
  .item
    .description What a cool item!</code></pre>

<p>is the same as:</p>

<pre class="code ruby"><code class="ruby">%div#collection
  %div.item
    %div.description What a cool item!</code></pre>

<p>and is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;div id=&#39;collection&#39;&gt;
  &lt;div class=&#39;item&#39;&gt;
    &lt;div class=&#39;description&#39;&gt;What a cool item!&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;</code></pre>

<h3 id="empty_void_tags_">Empty (void) Tags: <code>/</code></h3>

<p>The forward slash character, when placed at the end of a tag definition, causes Haml to treat it as being an empty (or void) element. Depending on the format, the tag will be rendered either without a closing tag (<code>:html4</code> or <code>:html5</code>), or as a self-closing tag (<code>:xhtml</code>). For example:</p>

<pre class="code ruby"><code class="ruby">%br/
%meta{&#39;http-equiv&#39; =&gt; &#39;Content-Type&#39;, :content =&gt; &#39;text/html&#39;}/</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;br&gt;
&lt;meta content=&#39;text/html&#39; http-equiv=&#39;Content-Type&#39;&gt;</code></pre>

<p>when the format is <code>:html4</code> or <code>:html5</code>, and to</p>

<pre class="code ruby"><code class="ruby">&lt;br /&gt;
&lt;meta content=&#39;text/html&#39; http-equiv=&#39;Content-Type&#39; /&gt;</code></pre>

<p>when the format is <code>:xhtml</code>.</p>

<p>Some tags are automatically treated as being empty, as long as they have no content in the Haml source. <code>meta</code>, <code>img</code>, <code>link</code>, <code>br</code>, <code>hr</code>, <code>input</code>, <code>area</code>, <code>param</code>, <code>col</code> and <code>base</code> tags are treated as empty by default. This list can be customized by setting the <span class='object_link'><a href="Haml/Options.html#autoclose-instance_method" title="Haml::Options#autoclose (method)"><code>:autoclose</code></a></span> option.</p>

<h3 id="whitespace_removal__and_">Whitespace Removal: <code>&gt;</code> and <code>&lt;</code></h3>

<p><code>&gt;</code> and <code>&lt;</code> give you more control over the whitespace near a tag. <code>&gt;</code> will remove all whitespace surrounding a tag, while <code>&lt;</code> will remove all whitespace immediately within a tag. You can think of them as alligators eating the whitespace: <code>&gt;</code> faces out of the tag and eats the whitespace on the outside, and <code>&lt;</code> 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 <code>/</code> or <code>=</code>. For example:</p>

<pre class="code ruby"><code class="ruby">%blockquote&lt;
  %div
    Foo!</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;blockquote&gt;&lt;div&gt;
  Foo!
&lt;/div&gt;&lt;/blockquote&gt;</code></pre>

<p>And:</p>

<pre class="code ruby"><code class="ruby">%img
%img&gt;
%img</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;img /&gt;&lt;img /&gt;&lt;img /&gt;</code></pre>

<p>And:</p>

<pre class="code ruby"><code class="ruby">%p&lt;= &quot;Foo\nBar&quot;</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;Foo
Bar&lt;/p&gt;</code></pre>

<p>And finally:</p>

<pre class="code ruby"><code class="ruby">%img
%pre&gt;&lt;
  foo
  bar
%img</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;img /&gt;&lt;pre&gt;foo
bar&lt;/pre&gt;&lt;img /&gt;</code></pre>

<h3 id="object_reference_">Object Reference: <code>[]</code></h3>

<p>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 <code>#to_key</code> or <code>#id</code> 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:</p>

<pre class="code ruby"><code class="ruby"># file: app/controllers/users_controller.rb

def show
  @user = CrazyUser.find(15)
end

-# file: app/views/users/show.haml

%div[@user, :greeting]
  %bar[290]/
  Hello!</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;div class=&#39;greeting_crazy_user&#39; id=&#39;greeting_crazy_user_15&#39;&gt;
  &lt;bar class=&#39;fixnum&#39; id=&#39;fixnum_581&#39; /&gt;
  Hello!
&lt;/div&gt;</code></pre>

<p>If you require that the class be something other than the underscored object’s class, you can implement the <code>haml_object_ref</code> method on the object.</p>

<pre class="code ruby"><code class="ruby"># file: app/models/crazy_user.rb

class CrazyUser &lt; ActiveRecord::Base
  def haml_object_ref
    &quot;a_crazy_user&quot;
  end
end

-# file: app/views/users/show.haml

%div[@user]
  Hello!</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;div class=&#39;a_crazy_user&#39; id=&#39;a_crazy_user_15&#39;&gt;
  Hello!
&lt;/div&gt;</code></pre>

<p>The <code>:class</code> attribute may be used in conjunction with an object reference. The compiled element will have the union of all classes.</p>

<pre class="code ruby"><code class="ruby">- user = User.find(1)
%p[user]{:class =&gt; &#39;alpha bravo&#39;}
&lt;p id=&quot;user_1&quot; class=&quot;alpha bravo user&quot;&gt;&lt;/p&gt;</code></pre>

<h2 id="doctype_">Doctype: <code>!!!</code></h2>

<p>When describing HTML documents with Haml, you can have a document type or XML prolog generated automatically by including the characters <code>!!!</code>. For example:</p>

<pre class="code ruby"><code class="ruby">!!! XML
!!!
%html
  %head
    %title Myspace
  %body
    %h1 I am the international space station
    %p Sign my guestbook</code></pre>

<p>is compiled to:</p>
<?xml version='1.0' encoding='utf-8' ?>
<pre class="code ruby"><code class="ruby">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Myspace&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;I am the international space station&lt;/h1&gt;
    &lt;p&gt;Sign my guestbook&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>

<p>You can also specify the specific doctype after the <code>!!!</code> When the <span class='object_link'><a href="Haml/Options.html#format-instance_method" title="Haml::Options#format (method)"><code>:format</code></a></span> is set to <code>:xhtml</code>. The following doctypes are supported:</p>

<dl>
<dt><code>!!!</code></dt>

<dd>XHTML 1.0 Transitional<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</code></dd>

<dt><code>!!! Strict</code></dt>

<dd>XHTML 1.0 Strict<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</code></dd>

<dt><code>!!! Frameset</code></dt>

<dd>XHTML 1.0 Frameset<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Frameset//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd&quot;&gt;</code></dd>

<dt><code>!!! 5</code></dt>

<dd>XHTML 5<br /> <code>&lt;!DOCTYPE html&gt;</code><br /></dd>

<dt><code>!!! 1.1</code></dt>

<dd>XHTML 1.1<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot; &quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;</code></dd>

<dt><code>!!! Basic</code></dt>

<dd>XHTML Basic 1.1<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML Basic 1.1//EN&quot; &quot;http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd&quot;&gt; </code></dd>

<dt><code>!!! Mobile</code></dt>

<dd>XHTML Mobile 1.2<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//WAPFORUM//DTD XHTML Mobile 1.2//EN&quot; &quot;http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd&quot;&gt;</code></dd>

<dt><code>!!! RDFa</code></dt>

<dd>XHTML+RDFa 1.0<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML+RDFa 1.0//EN&quot; &quot;http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd&quot;&gt;</code></dd>
</dl>

<p>When the <span class='object_link'><a href="Haml/Options.html#format-instance_method" title="Haml::Options#format (method)"><code>:format</code></a></span> option is set to <code>:html4</code>, the following doctypes are supported:</p>

<dl>
<dt><code>!!!</code></dt>

<dd>HTML 4.01 Transitional<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</code></dd>

<dt><code>!!! Strict</code></dt>

<dd>HTML 4.01 Strict<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</code></dd>

<dt><code>!!! Frameset</code></dt>

<dd>HTML 4.01 Frameset<br /> <code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Frameset//EN&quot; &quot;http://www.w3.org/TR/html4/frameset.dtd&quot;&gt;</code></dd>
</dl>

<p>When the <span class='object_link'><a href="Haml/Options.html#format-instance_method" title="Haml::Options#format (method)"><code>:format</code></a></span> option is set to <code>:html5</code>, <code>!!!</code> is always <code>&lt;!DOCTYPE html&gt;</code>.</p>

<p>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:</p>

<pre class="code ruby"><code class="ruby">!!! XML iso-8859-1</code></pre>

<p>is compiled to:</p>
<?xml version='1.0' encoding='iso-8859-1' ?>
<p>If the mime_type of the template being rendered is <code>text/xml</code> then a format of <code>:xhtml</code> will be used even if the global output format is set to <code>:html4</code> or <code>:html5</code>.</p>

<h2 id="comments">Comments</h2>

<p>Haml supports two sorts of comments: those that show up in the HTML output and those that don’t.</p>

<h3 id="html_comments_">HTML Comments: <code>/</code></h3>

<p>The forward slash character, when placed at the beginning of a line, wraps all text after it in an HTML comment. For example:</p>

<pre class="code ruby"><code class="ruby">%peanutbutterjelly
  / This is the peanutbutterjelly element
  I like sandwiches!</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;peanutbutterjelly&gt;
  &lt;!-- This is the peanutbutterjelly element --&gt;
  I like sandwiches!
&lt;/peanutbutterjelly&gt;</code></pre>

<p>The forward slash can also wrap indented sections of code. For example:</p>

<pre class="code ruby"><code class="ruby">/
  %p This doesn&#39;t render...
  %div
    %h1 Because it&#39;s commented out!</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;!--
  &lt;p&gt;This doesn&#39;t render...&lt;/p&gt;
  &lt;div&gt;
    &lt;h1&gt;Because it&#39;s commented out!&lt;/h1&gt;
  &lt;/div&gt;
--&gt;</code></pre>

<h4 id="conditional_comments_">Conditional Comments: <code>/[]</code></h4>

<p>You can also use <a href="http://www.quirksmode.org/css/condcom.html">Internet Explorer conditional comments</a> by enclosing the condition in square brackets after the <code>/</code>. For example:</p>

<pre class="code ruby"><code class="ruby">/[if IE]
  %a{ :href =&gt; &#39;http://www.mozilla.com/en-US/firefox/&#39; }
    %h1 Get Firefox</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;!--[if IE]&gt;
  &lt;a href=&#39;http://www.mozilla.com/en-US/firefox/&#39;&gt;
    &lt;h1&gt;Get Firefox&lt;/h1&gt;
  &lt;/a&gt;
&lt;![endif]--&gt;</code></pre>

<h3 id="haml_comments_">Haml Comments: <code>-#</code></h3>

<p>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.</p>

<p>For example:</p>

<pre class="code ruby"><code class="ruby">%p foo
-# This is a comment
%p bar</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;foo&lt;/p&gt;
&lt;p&gt;bar&lt;/p&gt;</code></pre>

<p>You can also nest text beneath a silent comment. None of this text will be rendered. For example:</p>

<pre class="code ruby"><code class="ruby">%p foo
-#
  This won&#39;t be displayed
    Nor will this
                   Nor will this.
%p bar</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;foo&lt;/p&gt;
&lt;p&gt;bar&lt;/p&gt;</code></pre>

<h2 id="ruby_evaluation">Ruby Evaluation</h2>

<h3 id="inserting_ruby_">Inserting Ruby: <code>=</code></h3>

<p>The equals character is followed by Ruby code. This code is evaluated and the output is inserted into the document. For example:</p>

<pre class="code ruby"><code class="ruby">%p
  = [&#39;hi&#39;, &#39;there&#39;, &#39;reader!&#39;].join &quot; &quot;
  = &quot;yo&quot;</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;
  hi there reader!
  yo
&lt;/p&gt;</code></pre>

<p>If the <span class='object_link'><a href="Haml/Options.html#escape_html-instance_method" title="Haml::Options#escape_html (method)"><code>:escape_html</code></a></span> option is set, <code>=</code> will sanitize any HTML-sensitive characters generated by the script. For example:</p>

<pre class="code ruby"><code class="ruby">= &#39;&lt;script&gt;alert(&quot;I\&#39;m evil!&quot;);&lt;/script&gt;&#39;</code></pre>

<p>would be compiled to</p>

<pre class="code ruby"><code class="ruby">&amp;lt;script&amp;gt;alert(&amp;quot;I&#39;m evil!&amp;quot;);&amp;lt;/script&amp;gt;</code></pre>

<p><code>=</code> can also be used at the end of a tag to insert Ruby code within that tag. For example:</p>

<pre class="code ruby"><code class="ruby">%p= &quot;hello&quot;</code></pre>

<p>would be compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;hello&lt;/p&gt;</code></pre>

<p>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:</p>

<pre class="code ruby"><code class="ruby">= link_to_remote &quot;Add to cart&quot;,
    :url =&gt; { :action =&gt; &quot;add&quot;, :id =&gt; product.id },
    :update =&gt; { :success =&gt; &quot;cart&quot;, :failure =&gt; &quot;error&quot; }</code></pre>

<p>Note that it’s illegal to nest code within a tag that ends with <code>=</code>.</p>

<h3 id="running_ruby_">Running Ruby: <code>-</code></h3>

<p>The hyphen character is also followed by Ruby code. This code is evaluated but <em>not</em> inserted into the document.</p>

<p><strong>It is not recommended that you use this widely; almost all processing code and logic should be restricted to Controllers, Helpers, or partials.</strong></p>

<p>For example:</p>

<pre class="code ruby"><code class="ruby">- foo = &quot;hello&quot;
- foo &lt;&lt; &quot; there&quot;
- foo &lt;&lt; &quot; you!&quot;
%p= foo</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;
  hello there you!
&lt;/p&gt;</code></pre>

<p>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:</p>

<pre class="code ruby"><code class="ruby">- links = {:home =&gt; &quot;/&quot;,
    :docs =&gt; &quot;/docs&quot;,
    :about =&gt; &quot;/about&quot;}</code></pre>

<h4 id="ruby_blocks">Ruby Blocks</h4>

<p>Ruby blocks, like XHTML tags, don’t need to be explicitly closed in Haml. 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 <code>else</code> clause or something similar). For example:</p>

<pre class="code ruby"><code class="ruby">- (42...47).each do |i|
  %p= i
%p See, I can count!</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;42&lt;/p&gt;
&lt;p&gt;43&lt;/p&gt;
&lt;p&gt;44&lt;/p&gt;
&lt;p&gt;45&lt;/p&gt;
&lt;p&gt;46&lt;/p&gt;
&lt;p&gt;See, I can count!&lt;/p&gt;</code></pre>

<p>Another example:</p>

<pre class="code ruby"><code class="ruby">%p
  - case 2
  - when 1
    = &quot;1!&quot;
  - when 2
    = &quot;2?&quot;
  - when 3
    = &quot;3.&quot;</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;
  2?
&lt;/p&gt;</code></pre>

<h3 id="tilde">Whitespace Preservation: <code>~</code></h3>

<p><code>~</code> works just like <code>=</code>, except that it runs <span class='object_link'><a href="Haml/Helpers.html#find_and_preserve-instance_method" title="Haml::Helpers#find_and_preserve (method)">Haml::Helpers#find_and_preserve</a></span> on its input. For example,</p>

<pre class="code ruby"><code class="ruby">~ &quot;Foo\n&lt;pre&gt;Bar\nBaz&lt;/pre&gt;&quot;</code></pre>

<p>is the same as:</p>

<pre class="code ruby"><code class="ruby">= find_and_preserve(&quot;Foo\n&lt;pre&gt;Bar\nBaz&lt;/pre&gt;&quot;)</code></pre>

<p>and is compiled to:</p>

<pre class="code ruby"><code class="ruby">Foo
&lt;pre&gt;Bar&amp;#x000A;Baz&lt;/pre&gt;</code></pre>

<p>See also <a href="#whitespace_preservation">Whitespace Preservation</a>.</p>

<h3 id="ruby_interpolation_">Ruby Interpolation: <code>#{}</code></h3>

<p>Ruby code can also be interpolated within plain text using <code>#{}</code>, similarly to Ruby string interpolation. For example,</p>

<pre class="code ruby"><code class="ruby">%p This is #{h quality} cake!</code></pre>

<p>is the same as</p>

<pre class="code ruby"><code class="ruby">%p= &quot;This is #{h quality} cake!&quot;</code></pre>

<p>and might compile to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;This is scrumptious cake!&lt;/p&gt;</code></pre>

<p>Backslashes can be used to escape <code>#{}</code> strings, but they don’t act as escapes anywhere else in the string. For example:</p>

<pre class="code ruby"><code class="ruby">%p
  Look at \\#{h word} lack of backslash: \#{foo}
  And yon presence thereof: \{foo}</code></pre>

<p>might compile to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;
  Look at \yon lack of backslash: #{foo}
  And yon presence thereof: \{foo}
&lt;/p&gt;</code></pre>

<p>Interpolation can also be used within <a href="#filters">filters</a>. For example:</p>

<pre class="code ruby"><code class="ruby">:javascript
  $(document).ready(function() {
    alert(#{@message.to_json});
  });</code></pre>

<p>might compile to:</p>

<pre class="code ruby"><code class="ruby">&lt;script type=&#39;text/javascript&#39;&gt;
  //&lt;![CDATA[
    $(document).ready(function() {
      alert(&quot;Hi there!&quot;);
    });
  //]]&gt;
&lt;/script&gt;</code></pre>

<h3 id="escaping_html">Escaping HTML: <code>&amp;=</code></h3>

<p>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:</p>

<pre class="code ruby"><code class="ruby">&amp;= &quot;I like cheese &amp; crackers&quot;</code></pre>

<p>compiles to</p>

<pre class="code ruby"><code class="ruby">I like cheese &amp;amp; crackers</code></pre>

<p>If the <span class='object_link'><a href="Haml/Options.html#escape_html-instance_method" title="Haml::Options#escape_html (method)"><code>:escape_html</code></a></span> option is set, <code>&amp;=</code> behaves identically to <code>=</code>.</p>

<p><code>&amp;</code> can also be used on its own so that <code>#{}</code> interpolation is escaped. For example,</p>

<pre class="code ruby"><code class="ruby">&amp; I like #{&quot;cheese &amp; crackers&quot;}</code></pre>

<p>compiles to:</p>

<pre class="code ruby"><code class="ruby">I like cheese &amp;amp; crackers</code></pre>

<h3 id="unescaping_html">Unescaping HTML: <code>!=</code></h3>

<p>An exclamation mark followed by one or two equals characters evaluates Ruby code just like the equals would, but never sanitizes the HTML.</p>

<p>By default, the single equals doesn’t sanitize HTML either. However, if the <span class='object_link'><a href="Haml/Options.html#escape_html-instance_method" title="Haml::Options#escape_html (method)"><code>:escape_html</code></a></span> option is set, <code>=</code> will sanitize the HTML, but <code>!=</code> still won’t. For example, if <code>:escape_html</code> is set:</p>

<pre class="code ruby"><code class="ruby">= &quot;I feel &lt;strong&gt;!&quot;
!= &quot;I feel &lt;strong&gt;!&quot;</code></pre>

<p>compiles to</p>

<pre class="code ruby"><code class="ruby">I feel &amp;lt;strong&amp;gt;!
I feel &lt;strong&gt;!</code></pre>

<p><code>!</code> can also be used on its own so that <code>#{}</code> interpolation is unescaped. For example,</p>

<pre class="code ruby"><code class="ruby">! I feel #{&quot;&lt;strong&gt;&quot;}!</code></pre>

<p>compiles to</p>

<pre class="code ruby"><code class="ruby">I feel &lt;strong&gt;!</code></pre>

<h2 id="filters">Filters</h2>

<p>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. For example:</p>

<pre class="code ruby"><code class="ruby">%p
  :markdown
    # Greetings

    Hello, *World*</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;p&gt;
  &lt;h1&gt;Greetings&lt;/h1&gt;

  &lt;p&gt;Hello, &lt;em&gt;World&lt;/em&gt;&lt;/p&gt;
&lt;/p&gt;</code></pre>

<p>Filters can have Ruby code interpolated with <code>#{}</code>. For example:</p>

<pre class="code ruby"><code class="ruby">- flavor = &quot;raspberry&quot;
#content
  :textile
    I *really* prefer _#{flavor}_ jam.</code></pre>

<p>is compiled to</p>

<pre class="code ruby"><code class="ruby">&lt;div id=&#39;content&#39;&gt;
  &lt;p&gt;I &lt;strong&gt;really&lt;/strong&gt; prefer &lt;em&gt;raspberry&lt;/em&gt; jam.&lt;/p&gt;
&lt;/div&gt;</code></pre>

<p>Currently, filters ignore the <span class='object_link'><a href="Haml/Options.html#escape_html-instance_method" title="Haml::Options#escape_html (method)"><code>:escape_html</code></a></span> option. This means that <code>#{}</code> interpolation within filters is never HTML-escaped.</p>

<p>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.</p>

<p>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:</p>

<pre class="code ruby"><code class="ruby">Tilt.prefer Tilt::RedCarpetTemplate</code></pre>

<p>See the <a href="https://github.com/rtomayko/tilt#fallback-mode">Tilt documentation</a> for more info.</p>

<p>Haml comes with the following filters defined:</p>

<h3 id="cdata-filter"><code>:cdata</code></h3>

<p>Surrounds the filtered text with CDATA tags.</p>

<h3 id="coffee-filter"><code>:coffee</code></h3>

<p>Compiles the filtered text to Javascript using Cofeescript. You can also reference this filter as <code>:coffeescript</code>. This filter is implemented using Tilt.</p>

<h3 id="css-filter"><code>:css</code></h3>

<p>Surrounds the filtered text with <code>&lt;style&gt;</code> and (optionally) CDATA tags. Useful for including inline CSS. Use the <span class='object_link'><a href="Haml/Options.html#cdata-instance_method" title="Haml::Options#cdata (method)"><code>:cdata</code> option</a></span> to control when CDATA tags are added.</p>

<h3 id="erb-filter"><code>:erb</code></h3>

<p>Parses the filtered text with ERb, like an RHTML template. Not available if the <span class='object_link'><a href="Haml/Options.html#suppress_eval-instance_method" title="Haml::Options#suppress_eval (method)"><code>:suppress_eval</code></a></span> option is set to true. Embedded Ruby code is evaluated in the same context as the Haml template. This filter is implemented using Tilt.</p>

<h3 id="escaped-filter"><code>:escaped</code></h3>

<p>Works the same as plain, but HTML-escapes the text before placing it in the document.</p>

<h3 id="javascript-filter"><code>:javascript</code></h3>

<p>Surrounds the filtered text with <code>&lt;script&gt;</code> and (optionally) CDATA tags. Useful for including inline Javascript. Use the <span class='object_link'><a href="Haml/Options.html#cdata-instance_method" title="Haml::Options#cdata (method)"><code>:cdata</code> option</a></span> to control when CDATA tags are added.</p>

<h3 id="less-filter"><code>:less</code></h3>

<p>Parses the filtered text with <a href="http://lesscss.org/">Less</a> to produce CSS output. This filter is implemented using Tilt.</p>

<h3 id="markdown-filter"><code>:markdown</code></h3>

<p>Parses the filtered text with <a href="http://daringfireball.net/projects/markdown">Markdown</a>. This filter is implemented using Tilt.</p>

<h3 id="maruku-filter"><code>:maruku</code></h3>

<p>Parses the filtered text with <a href="https://github.com/nex3/maruku">Maruku</a>, which has some non-standard extensions to Markdown.</p>

<p>As of Haml 4.0, this filter is defined in <a href="https://github.com/haml/haml-contrib">Haml contrib</a> 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.</p>

<h3 id="plain-filter"><code>:plain</code></h3>

<p>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 <code>.</code> or <code>-</code> to be parsed.</p>

<h3 id="preserve-filter"><code>:preserve</code></h3>

<p>Inserts the filtered text into the template with whitespace preserved. <code>preserve</code>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 <a href="#whitespace_preservation">Whitespace Preservation</a>.</p>

<h3 id="ruby-filter"><code>:ruby</code></h3>

<p>Parses the filtered text with the normal Ruby interpreter. Creates an <code>IO</code> object named <code>haml_io</code>, anything written to it is output into the Haml document. Not available if the <span class='object_link'><a href="Haml/Options.html#suppress_eval-instance_method" title="Haml::Options#suppress_eval (method)"><code>:suppress_eval</code></a></span> option is set to true. The Ruby code is evaluated in the same context as the Haml template.</p>

<h3 id="sass-filter"><code>:sass</code></h3>

<p>Parses the filtered text with <a href="http://sass-lang.com/">Sass</a> to produce CSS output. This filter is implemented using Tilt.</p>

<h3 id="scss-filter"><code>:scss</code></h3>

<p>Parses the filtered text with Sass like the <code>:sass</code> filter, but uses the newer SCSS syntax to produce CSS output. This filter is implemented using Tilt.</p>

<h3 id="textile-filter"><code>:textile</code></h3>

<p>Parses the filtered text with <a href="http://www.textism.com/tools/textile">Textile</a>. Only works if <a href="http://redcloth.org">RedCloth</a> is installed.</p>

<p>As of Haml 4.0, this filter is defined in <a href="https://github.com/haml/haml-contrib">Haml contrib</a> 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.</p>

<h3 id="custom_filters">Custom Filters</h3>

<p>You can also define your own filters. See <span class='object_link'><a href="Haml/Filters.html" title="Haml::Filters (module)">Haml::Filters</a></span> for details.</p>

<h2 id="helper-methods">Helper Methods</h2>

<p>Sometimes you need to manipulate whitespace in a more precise fashion than what the whitespace removal methods allow. There are a few helper methods that are useful when dealing with inline content. All these methods take a Haml block to modify.</p>

<h3 id="surround">surround</h3>

<p>Surrounds a Haml block with text. Expects 1 or 2 string arguments used to surround the Haml block. If a second argument is not provided, the first argument is used as the second.</p>

<pre class="code ruby"><code class="ruby">= surround &quot;(&quot;, &quot;)&quot; do
  = link_to &quot;learn more&quot;, &quot;#&quot;</code></pre>

<h3 id="precede">precede</h3>

<p>Prepends a Haml block with text. Expects 1 argument.</p>

<pre class="code ruby"><code class="ruby">= precede &quot;*&quot; do
  %span Required</code></pre>

<h3 id="succeed">succeed</h3>

<p>Appends a Haml block with text. Expects 1 argument.</p>

<pre class="code ruby"><code class="ruby">Begin by
= succeed &quot;,&quot; do
  = link_to &quot;filling out your profile&quot;, &quot;#&quot;
= succeed &quot;,&quot; do
  = link_to &quot;adding a bio&quot;, &quot;#&quot;
and
= succeed &quot;.&quot; do
  = link_to &quot;inviting friends&quot;, &quot;#&quot;</code></pre>

<h2 id="multiline">Multiline: <code>|</code></h2>

<p>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 <code>|</code> will be evaluated as though they were on the same line. <strong>Note that even the last line in the multiline block should end with <code>|</code>.</strong> For example:</p>

<pre class="code ruby"><code class="ruby">%whoo
  %hoo= h(                       |
    &quot;I think this might get &quot; +  |
    &quot;pretty long so I should &quot; + |
    &quot;probably make it &quot; +        |
    &quot;multiline so it doesn&#39;t &quot; + |
    &quot;look awful.&quot;)               |
  %p This is short.</code></pre>

<p>is compiled to:</p>

<pre class="code ruby"><code class="ruby">&lt;whoo&gt;
  &lt;hoo&gt;I think this might get pretty long so I should probably make it multiline so it doesn&#39;t look awful.&lt;/hoo&gt;
  &lt;p&gt;This is short&lt;/p&gt;
&lt;/whoo&gt;</code></pre>

<p>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?</p>

<p>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. Some elements just have lots of attributes, so you can wrap attributes without using <code>|</code> (see <a href="#attributes">Attributes</a>).</p>

<p>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 <a href="#inserting_ruby_">Inserting Ruby</a>).</p>

<h2 id="whitespace_preservation">Whitespace Preservation</h2>

<p>Sometimes you don’t want Haml to indent all your text. For example, tags like <code>pre</code> and <code>textarea</code> are whitespace-sensitive; indenting the text makes them render wrong.</p>

<p>Haml deals with this by “preserving” newlines before they’re put into the document – converting them to the HTML whitespace escape code, <code>&amp;#x000A;</code>. Then Haml won’t try to re-format the indentation.</p>

<p>Literal <code>textarea</code> and <code>pre</code> tags automatically preserve content given through <code>=</code>. Dynamically-generated <code>textarea</code>s and <code>pre</code>s can’t be preserved automatically, and so should be passed through <span class='object_link'><a href="Haml/Helpers.html#find_and_preserve-instance_method" title="Haml::Helpers#find_and_preserve (method)">Haml::Helpers#find_and_preserve</a></span> or the <a href="#tilde"><code>~</code> command</a>, which has the same effect.</p>

<p>Blocks of literal text can be preserved using the <a href="#preserve-filter"><code>:preserve</code> filter</a>.</p>

<h2 id="helpers">Helpers</h2>

<p>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 <span class='object_link'><a href="Haml/Helpers.html" title="Haml::Helpers (module)">Haml::Helpers</a></span> and <span class='object_link'><a href="Haml/Helpers/ActionViewExtensions.html" title="Haml::Helpers::ActionViewExtensions (module)">Haml::Helpers::ActionViewExtensions</a></span> modules.</p>
</div></div>

    <div id="footer">
  Generated by
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.8.7.6 (ruby-2.1.2).
</div>

  </body>
</html>