Fork me on GitHub

Erector API Cheatsheet

code output
element('foo') <foo></foo>
empty_element('foo') <foo />
html <html></html>   …and likewise for all non-deprecated elements from the HTML 4.0.1 spec
b 'foo' <b>foo</b>
div { b 'foo' } <div><b>foo</b></div>
text 'foo' foo
text '&<>' &amp;&lt;&gt;   …all normal text is HTML escaped, which is what you generally want, especially if the text came from the user or a database
text raw('&<>') &<>   …raw text escapes being escaped
rawtext('&<>') &<>   …alias for text(raw())
text!('&<>') &<>   …another alias for text(raw())
div { text 'foo' } <div>foo</div>
div 'foo' <div>foo</div>
foo = 'bar'
div foo
a(:href => 'foo.div') <a href="foo.div"></a>
a(:href => 'q?a&b') <a href="q?a&amp;b"></a>   …attributes are escaped like text is
a(:href => raw('&amp;')) <a href="&amp;"></a>   …raw strings are never escaped, even in attributes
a 'foo', :href => "bar" <a href="bar">foo</a>
text nbsp('Save Doc') Save&#160;Doc   …turns spaces into non-breaking spaces
text nbsp &#160;   …a single non-breaking space
text character(160) &#xa0;   …output a character given its unicode code point
text character(:right-arrow) &#x2192;   …output a character given its unicode name
instruct <?xml version="1.0" encoding="UTF-8"?>
comment 'foo' <!--foo-->
url '' <a href=""></a>
capture { div } <div></div>   …returns the block as a string, doesn't add it to the current output stream
div :class => ['a', 'b'] <div class="a b"></div>
'if (x < y && x > z)
alert("don't stop");)
<script type="text/javascript">
// <![CDATA[
if (x < y && x > z) alert("don't stop");
// ]]>
jquery '$("p").wrap("<div></div>");' <script type="text/javascript">
// <![CDATA[
// ]]>
join([widget1, widget2],
  …See examples/join.rb for more explanation

Lots more documentation is at the RDoc API pages especially for Erector::Widget so don't go saying we never wrote you nothin'.