-
Notifications
You must be signed in to change notification settings - Fork 43
/
userguide.html
158 lines (147 loc) · 12.1 KB
/
userguide.html
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
<html><head><title>Erector - User Guide</title><link href="erector.css" rel="stylesheet" type="text/css" /></head><body><table><tr><td valign="top"><div class="sidebar"><a href="index.html"><img align="left" src="erector.jpg" /></a><br clear="all" /><h3>On This Site:</h3><ul><li class="clickable" onclick="document.location='index.html'"><a href="index.html">Readme</a></li><li class="clickable" onclick="document.location='userguide.html'"><a href="userguide.html">User Guide</a></li><li class="clickable" onclick="document.location='faq.html'"><a href="faq.html">FAQ</a></li><li class="clickable" onclick="document.location='developers.html'"><a href="developers.html">For Developers</a></li></ul><hr /><h3>External Links:</h3><ul><li class="clickable" onclick="document.location='http://rubyforge.org/frs/?group_id=4797'"><a href="http://rubyforge.org/frs/?group_id=4797">Download</a><br /><span> (current version: 0.4.200)</span></li><li class="clickable" onclick="document.location='http://erector.rubyforge.org/svn/trunk/History.txt'"><a href="http://erector.rubyforge.org/svn/trunk/History.txt">Version History</a></li><li class="clickable" onclick="document.location='rdoc'"><a href="rdoc">RDoc Documentation</a></li><li class="clickable" onclick="document.location='http://rubyforge.org/projects/erector/'"><a href="http://rubyforge.org/projects/erector/">RubyForge Project</a></li><li class="clickable" onclick="document.location='http://erector.lighthouseapp.com'"><a href="http://erector.lighthouseapp.com">Lighthouse Project</a></li><li class="clickable" onclick="document.location='http://rubyforge.org/scm/?group_id=4797'"><a href="http://rubyforge.org/scm/?group_id=4797">Subversion Repository</a></li><li class="clickable" onclick="document.location='http://rubyforge.org/mailman/listinfo/erector-devel'"><a href="http://rubyforge.org/mailman/listinfo/erector-devel">erector-devel mailing list</a></li></ul><hr /><h3>Supported by:</h3><center><a href="http://www.pivotallabs.com"><img alt="Pivotal Labs" height="57" src="pivotal.gif" width="158" /></a></center></div></td><td valign="top"><h1 class="title">Erector - User Guide</h1><hr /><div class="body"><p>Make sure to check out the <a href="rdoc">RDoc Documentation</a> for more details on the API.</p><ul><li><a href="#thebasics">The Basics</a></li><li><a href="#apicheatsheet">API Cheatsheet</a></li><li><a href="#prettyprinting">Pretty-printing</a></li><li><a href="#usingerectorfromrubyonrails">Using Erector from Ruby on Rails</a></li><li><a href="#erect">Erect: Command-line conversion to and from HTML</a></li><li><a href="#layoutinheritance">Layout Inheritance</a></li><li><a href="#inlinewidgets">Inline Widgets</a></li></ul><a name="thebasics"></a><h2>The Basics</h2><p>The basic way to construct some HTML/XML with erector is to subclass Erector::Widget and implement a render method:</p><table><tr><td><pre>class Hello < Erector::Widget
def render
html do
head do
title "Hello"
end
body do
text "Hello, "
b "world!"
end
end
end
end
</pre></td><td><span class="separator">=></span></td><td><pre><html>
<head>
<title>Hello</title>
</head>
<body>
Hello, <b>world!</b>
</body>
</html>
</pre></td></tr></table><a name="apicheatsheet"></a><h2>API Cheatsheet</h2><pre>element('foo') # <foo></foo>
empty_element('foo') # <foo />
html # <html></html> (likewise for other common html tags)
b 'foo' # <b>foo</b>
text 'foo' # foo
text '&<>' # &amp;&lt;&gt; (what you generally want, especially
# if the text came from the user or a database)
text raw('&<>') # &<> (back door for raw html)
rawtext('&<>') # &<> (alias for text(raw()))
html { text 'foo' } # <html>foo</html>
html 'foo' # <html>foo</html>
html foo # <html>bar</html> (if the method foo returns the string "bar")
a(:href => 'foo.html') # <a href="foo.html"></a>
a(:href => 'q?a&b') # <a href="q?a&amp;b"></a> (quotes as for text)
a(:href => raw('&amp;')) # <a href="&amp;"></a>
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"?>
javascript('if (x < y && x > z) alert("don\'t stop");') #=>
<script type="text/javascript">
// <![CDATA[
if (x < y && x > z) alert("don't stop");
// ]]>
</script>
</pre><table><tr><td><code>join [</code><i>widgets</i><code>], </code><i>separator</i></td><td><i>See examples/join.rb for more explanation</i></td></tr></table><i>TODO: document more obscure features like capture, Table, :class => ['one', 'two']</i><a name="prettyprinting"></a><h2>Pretty-printing</h2><p>Erector has the ability to insert newlines and indentation to make the generated HTML more readable. Newlines are inserted before and after certain tags.</p><p>To enable pretty-printing (insertion of newlines and indentation) of Erector's output, do one of the following:</p><ul><li>call to_pretty instead of to_s on your Erector::Widget</li><li>call enable_prettyprint(true) on your Erector::Widget. Then subsequent calls to to_s will prettyprint</li><li>call Erector::Doc.prettyprint_default = true (for example, in environments/development.rb in a rails application, or anywhere which is convenient)</li></ul><a name="usingerectorfromrubyonrails"></a><h2>Using Erector from Ruby on Rails</h2><p>Your views are just ruby classes. Your controller can either call Rails' <code>render :template</code> method as usual, or directly instantiate the view class and call its render method.</p><p>For example:</p><code>app/controllers/welcome_controller.rb:</code><pre>class WelcomeController < ApplicationController
def index
render :template => 'welcome/show'
end
end
</pre><code>app/views/welcome/show.rb:</code><pre>class Views::Welcome::Show < Erector::Widget
def render
html do
head do
title "Welcome page"
end
body do
p "Hello, world"
end
end
end
end
</pre><p>For Rails to find these .rb files during <code>render :template</code>, you must first either copy the erector source to <code>vendor/plugins/erector</code>, or add <code>require 'erector'</code> to <code>config/environment.rb</code>. You also should delete (or rename) any other view files with the same base name that might be getting in the way.</p><p>Currently there is only partial support for some standard Rails features like partials, layouts, assigns, and helpers. Check the <a href="http://rubyforge.org/mailman/listinfo/erector-devel">erector-devel mailing list</a> for status updates on these features.</p><a name="erect"></a><h2>Erect: Command-line conversion to and from HTML</h2><p>To make Rails integration as smooth as possible, we've written a little tool that will help you
erect your existing Rails app. The 'erect' tool will convert HTML or HTML/ERB into an Erector class.
It ships as part of the Erector gem, so to try it out, install the gem, then run</p><pre>erect app/views/foos/*.html.erb</pre><p>or just</p><pre>erect app/views</pre><p>and then delete the original files when you're satisfied.</p><p>Here's a little command-line howto for erecting a scaffold Rails app:</p><pre>rails foo
cd foo
script/generate scaffold post title:string body:text published:boolean
erect app/views/posts
mate app/views/posts
sleep 30 # this should be enough time for you to stop drooling
rm app/views/posts/*.erb
(echo ""; echo "require 'erector'") >> config/environment.rb
rake db:migrate
script/server
open http://localhost:3000/posts
</pre><a name="layoutinheritance"></a><h2>Layout Inheritance</h2><p>Erector replaces the typical Rails layout mechanism with a more natural construct, the use of inheritance. Want a common
layout? Just implement a layout superclass and inherit from it. Implement render in the superclass and implement template
methods in its subclasses.</p><p>For example:<pre>class Page < Erector::Widget
def initialize(title = self.class.name)
super
@title = title
end
def render
html do
head do
title "MyApp - #{@title}"
css "myapp.css"
end
body do
div :class => 'sidebar' do
render_sidebar
end
div :class => 'body' do
render_body
end
div :class => 'footer' do
render_footer
end
end
end
end
def render_sidebar
a "MyApp Home", :href => "/"
end
def render_body
p "This page intentionally left blank."
end
def render_footer
p "Copyright (c) 2112, Rush Enterprises Inc."
end
end
</pre><pre>class Faq < Page
def initialize
super("FAQ")
end
def render_body
p "Q: Why is the sky blue?"
p "A: To get to the other side"
end
def render_sidebar
super
a "More FAQs", :href => "http://faqs.org"
end
end
</pre></p><p>Notice how this mechanism allows you to...</p><ul><li>Set instance variables (e.g. title)</li><li>Override sections completely (e.g. render_body)</li><li>Append to standard content (e.g. render_sidebar)</li><li>Use standard content unchanged (e.g. render_footer)</li></ul><p>all in a straightforward, easily understood paradigm (OO inheritance). (No more weird yielding to invisible, undocumented closures!)</p><p>To use this in Rails, declare <code>layout nil</code> in <code>app/controllers/application.rb</code> and then define your Page parent class as <code>class Views::Layouts::Page</code> in <code>app/views/layouts</code> as usual.</p><p>There's one trick you'll need to use this layout for non-erector view templates. Here's an example.</p><p><code>application.rb</code> - The Erector layout superclass<pre>class Views::Layouts::Application < Erector::Widget
attr_accessor :content
def render
html do
head { } # head content here
# body content here
body do
text content
end
end
end
end
</pre></p><p><code>application.mab</code> - The markaby template (adjust for other appropriately templating technologies)<pre>widget = Views::Layouts::Application.new(self)
widget.content = content_for_layout
self << widget.to_s
</pre></p><p>Here the abstract layout widget is used in a concrete fashion by the template-based layout. Normally, the <code>content</code> method would be implemented by subclassing widgets, but the layout template sets it directly and then calls <code>to_s</code> on the layout widget. This allows the same layout to be shared in a backward compatible way.</p><a name="inlinewidgets"></a><h2>Inline Widgets</h2><p>Instead of subclassing <code>Erector::Widget</code> and implementing a render method, you can pass a block to <code>Erector::Widget.new</code>. For example:<pre>html = Erector::Widget.new do
p "Hello, world!"
end
html.to_s #=> <p>Hello, world!</p>
</pre>This lets you define mini-widgets on the fly.</p><p>One extra bonus feature of inline widgets is that they can call methods defined on the parent class, even though they're out of scope. How do they do this? Through method_missing magic. (But isn't method_missing magic against the design goals of Erector? Yes, some would say so, and we're probably going to discuss this feature on the mailing list before long.)</p></div></td></tr></table></body></html>