/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
— 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> »
<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 "haml"</code></pre>
<p>Once it’s installed, all view files with the <code>".html.haml"</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 < ApplicationController
def index
@title = "Teen Wolf"
end
end
-# file: app/views/movies/index.html.haml
#content
.title
%h1= @title
= link_to 'Home', home_url</code></pre>
<p>may be compiled to:</p>
<pre class="code ruby"><code class="ruby"><div id='content'>
<div class='title'>
<h1>Teen Wolf</h1>
<a href='/'>Home</a>
</div>
</div></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("%p Haml code!")
engine.render #=> "<p>Haml code!</p>\n"</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"><gee>
<whiz>
Wow this is cool!
</whiz>
</gee></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
<div id="blah">Blah!</div></code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><p>
<div id="blah">Blah!</div>
</p></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"><title>
MyPage
= @title
</title></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><element></element></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"><one>
<two>
<three>Hey there</three>
</two>
</one></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 => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", :lang => "en"}</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'></html></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 => "text/javascript",
:src => "javascripts/script_#{2 + 7}"}</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><script src='javascripts/script_9' type='text/javascript'></script></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>" "</code> and an <code>:id</code> array is joined with <code>"_"</code>. For example:</p>
<pre class="code ruby"><code class="ruby">%div{:id => [@item.type, @item.number], :class => [@item.type, @item.urgency]}</code></pre>
<p>is equivalent to:</p>
<pre class="code ruby"><code class="ruby">%div{:id => "#{@item.type}_#{@item.number}", :class => "#{@item.type} #{@item.urgency}"}</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 => [@item.type, @item == @sortcol && [:sort, @sortdir]] } Contents</code></pre>
<p>could render as any of:</p>
<pre class="code ruby"><code class="ruby"><div class="numeric sort ascending">Contents</div>
<div class="numeric">Contents</div>
<div class="sort descending">Contents</div>
<div>Contents</div></code></pre>
<p>depending on whether <code>@item.type</code> is <code>"numeric"</code> or <code>nil</code>, whether <code>@item == @sortcol</code>, and whether <code>@sortdir</code> is <code>"ascending"</code> or <code>"descending"</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 => @item.is_empty? && "empty"}</code></pre>
<p>could render as either of:</p>
<pre class="code ruby"><code class="ruby">class="item"
class="item empty"</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="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en")</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 => @title, :href => 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 => @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="widget_#{@widget.number}")</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="text/javascript"
src="javascripts/script_#{2 + 7}")</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 = 'en-US')
{:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => 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('fr-fr')}</code></pre>
<p>This is compiled to:</p>
<pre class="code ruby"><code class="ruby"><html lang='fr-fr' xml:lang='fr-fr' xmlns='http://www.w3.org/1999/xhtml'>
</html></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 => 'white', :filling => 'peanut butter and jelly'}
end
def hash2
{:bread => 'whole wheat'}
end</code></pre>
<p>then</p>
<pre class="code ruby"><code class="ruby">%sandwich{hash1, hash2, :delicious => 'true'}/</code></pre>
<p>would compile to:</p>
<pre class="code ruby"><code class="ruby"><sandwich bread='whole wheat' delicious='true' filling='peanut butter and jelly' /></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"><input selected></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 => 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"><input selected='selected'></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 => false}</code></pre>
<p>will just render as:</p>
<pre class="code ruby"><code class="ruby"><input></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=>"/posts", :data => {:author_id => 123}} Posts By Author</code></pre>
<p>will render as:</p>
<pre class="code ruby"><code class="ruby"><a data-author-id='123' href='/posts'>Posts By Author</a></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"><a data-author_id='123' href='/posts'>Posts By Author</a></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 => 'true' } The magical fruit
%h1.class.otherclass#id La La La</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><div id='things'>
<span id='rice'>Chicken Fried</span>
<p class='beans' food='true'>The magical fruit</p>
<h1 class='class otherclass' id='id'>La La La</h1>
</div></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"><div id='content'>
<div class='articles'>
<div class='article title'>Doogie Howser Comes Out</div>
<div class='article date'>2006-11-05</div>
<div class='article entry'>
Neil Patrick Harris would like to dispel any rumors that he is straight
</div>
</div>
</div></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 => @article.number, :class => @article.visibility}</code></pre>
<p>is equivalent to</p>
<pre class="code ruby"><code class="ruby">%div{:id => ['Article', @article.number], :class => ['article', 'entry', @article.visibility]} Gabba Hey</code></pre>
<p>and could compile to:</p>
<pre class="code ruby"><code class="ruby"><div class="article entry visible" id="Article_27">Gabba Hey</div></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"><div id='collection'>
<div class='item'>
<div class='description'>What a cool item!</div>
</div>
</div></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{'http-equiv' => 'Content-Type', :content => 'text/html'}/</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><br>
<meta content='text/html' http-equiv='Content-Type'></code></pre>
<p>when the format is <code>:html4</code> or <code>:html5</code>, and to</p>
<pre class="code ruby"><code class="ruby"><br />
<meta content='text/html' http-equiv='Content-Type' /></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>></code> and <code><</code></h3>
<p><code>></code> and <code><</code> give you more control over the whitespace near a tag. <code>></code> will remove all whitespace surrounding a tag, while <code><</code> will remove all whitespace immediately within a tag. You can think of them as alligators eating the whitespace: <code>></code> faces out of the tag and eats the whitespace on the outside, and <code><</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<
%div
Foo!</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><blockquote><div>
Foo!
</div></blockquote></code></pre>
<p>And:</p>
<pre class="code ruby"><code class="ruby">%img
%img>
%img</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><img /><img /><img /></code></pre>
<p>And:</p>
<pre class="code ruby"><code class="ruby">%p<= "Foo\nBar"</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><p>Foo
Bar</p></code></pre>
<p>And finally:</p>
<pre class="code ruby"><code class="ruby">%img
%pre><
foo
bar
%img</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><img /><pre>foo
bar</pre><img /></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"><div class='greeting_crazy_user' id='greeting_crazy_user_15'>
<bar class='fixnum' id='fixnum_581' />
Hello!
</div></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 < ActiveRecord::Base
def haml_object_ref
"a_crazy_user"
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"><div class='a_crazy_user' id='a_crazy_user_15'>
Hello!
</div></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 => 'alpha bravo'}
<p id="user_1" class="alpha bravo user"></p></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"><!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></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><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></code></dd>
<dt><code>!!! Strict</code></dt>
<dd>XHTML 1.0 Strict<br /> <code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></code></dd>
<dt><code>!!! Frameset</code></dt>
<dd>XHTML 1.0 Frameset<br /> <code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"></code></dd>
<dt><code>!!! 5</code></dt>
<dd>XHTML 5<br /> <code><!DOCTYPE html></code><br /></dd>
<dt><code>!!! 1.1</code></dt>
<dd>XHTML 1.1<br /> <code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"></code></dd>
<dt><code>!!! Basic</code></dt>
<dd>XHTML Basic 1.1<br /> <code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"> </code></dd>
<dt><code>!!! Mobile</code></dt>
<dd>XHTML Mobile 1.2<br /> <code><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"></code></dd>
<dt><code>!!! RDFa</code></dt>
<dd>XHTML+RDFa 1.0<br /> <code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"></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><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"></code></dd>
<dt><code>!!! Strict</code></dt>
<dd>HTML 4.01 Strict<br /> <code><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"></code></dd>
<dt><code>!!! Frameset</code></dt>
<dd>HTML 4.01 Frameset<br /> <code><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"></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><!DOCTYPE html></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"><peanutbutterjelly>
<!-- This is the peanutbutterjelly element -->
I like sandwiches!
</peanutbutterjelly></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't render...
%div
%h1 Because it's commented out!</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><!--
<p>This doesn't render...</p>
<div>
<h1>Because it's commented out!</h1>
</div>
--></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 => 'http://www.mozilla.com/en-US/firefox/' }
%h1 Get Firefox</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><!--[if IE]>
<a href='http://www.mozilla.com/en-US/firefox/'>
<h1>Get Firefox</h1>
</a>
<![endif]--></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"><p>foo</p>
<p>bar</p></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'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"><p>foo</p>
<p>bar</p></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
= ['hi', 'there', 'reader!'].join " "
= "yo"</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><p>
hi there reader!
yo
</p></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">= '<script>alert("I\'m evil!");</script>'</code></pre>
<p>would be compiled to</p>
<pre class="code ruby"><code class="ruby">&lt;script&gt;alert(&quot;I'm evil!&quot;);&lt;/script&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= "hello"</code></pre>
<p>would be compiled to:</p>
<pre class="code ruby"><code class="ruby"><p>hello</p></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 "Add to cart",
:url => { :action => "add", :id => product.id },
:update => { :success => "cart", :failure => "error" }</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 = "hello"
- foo << " there"
- foo << " you!"
%p= foo</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><p>
hello there you!
</p></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 => "/",
:docs => "/docs",
:about => "/about"}</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"><p>42</p>
<p>43</p>
<p>44</p>
<p>45</p>
<p>46</p>
<p>See, I can count!</p></code></pre>
<p>Another example:</p>
<pre class="code ruby"><code class="ruby">%p
- case 2
- when 1
= "1!"
- when 2
= "2?"
- when 3
= "3."</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><p>
2?
</p></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">~ "Foo\n<pre>Bar\nBaz</pre>"</code></pre>
<p>is the same as:</p>
<pre class="code ruby"><code class="ruby">= find_and_preserve("Foo\n<pre>Bar\nBaz</pre>")</code></pre>
<p>and is compiled to:</p>
<pre class="code ruby"><code class="ruby">Foo
<pre>Bar&#x000A;Baz</pre></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= "This is #{h quality} cake!"</code></pre>
<p>and might compile to:</p>
<pre class="code ruby"><code class="ruby"><p>This is scrumptious cake!</p></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"><p>
Look at \yon lack of backslash: #{foo}
And yon presence thereof: \{foo}
</p></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"><script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
alert("Hi there!");
});
//]]>
</script></code></pre>
<h3 id="escaping_html">Escaping HTML: <code>&=</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">&= "I like cheese & crackers"</code></pre>
<p>compiles to</p>
<pre class="code ruby"><code class="ruby">I like cheese &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>&=</code> behaves identically to <code>=</code>.</p>
<p><code>&</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">& I like #{"cheese & crackers"}</code></pre>
<p>compiles to:</p>
<pre class="code ruby"><code class="ruby">I like cheese &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">= "I feel <strong>!"
!= "I feel <strong>!"</code></pre>
<p>compiles to</p>
<pre class="code ruby"><code class="ruby">I feel &lt;strong&gt;!
I feel <strong>!</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 #{"<strong>"}!</code></pre>
<p>compiles to</p>
<pre class="code ruby"><code class="ruby">I feel <strong>!</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"><p>
<h1>Greetings</h1>
<p>Hello, <em>World</em></p>
</p></code></pre>
<p>Filters can have Ruby code interpolated with <code>#{}</code>. For example:</p>
<pre class="code ruby"><code class="ruby">- flavor = "raspberry"
#content
:textile
I *really* prefer _#{flavor}_ jam.</code></pre>
<p>is compiled to</p>
<pre class="code ruby"><code class="ruby"><div id='content'>
<p>I <strong>really</strong> prefer <em>raspberry</em> jam.</p>
</div></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><style></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><script></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 "(", ")" do
= link_to "learn more", "#"</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 "*" 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 "," do
= link_to "filling out your profile", "#"
= succeed "," do
= link_to "adding a bio", "#"
and
= succeed "." do
= link_to "inviting friends", "#"</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( |
"I think this might get " + |
"pretty long so I should " + |
"probably make it " + |
"multiline so it doesn't " + |
"look awful.") |
%p This is short.</code></pre>
<p>is compiled to:</p>
<pre class="code ruby"><code class="ruby"><whoo>
<hoo>I think this might get pretty long so I should probably make it multiline so it doesn't look awful.</hoo>
<p>This is short</p>
</whoo></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>&#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>
|