Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 206
- Log:
Initial import of Gullery, an open source photo gallery:
http://nubyonrails.com/pages/gullery
- Author:
- rool
- Date:
- Sun May 20 19:05:59 +0100 2007
- Size:
- 5079 Bytes
1 | = Markaby (Markup as Ruby) |
2 | |
3 | Markaby is a very short bit of code for writing HTML pages in pure Ruby. |
4 | It is an alternative to ERb which weaves the two languages together. |
5 | Also a replacement for templating languages which use primitive languages |
6 | that blend with HTML. |
7 | |
8 | == Using Markaby as a Rails plugin |
9 | |
10 | Write Rails templates in pure Ruby. Example layout: |
11 | |
12 | html do |
13 | head do |
14 | title 'Products: ' + action_name |
15 | stylesheet_link_tag 'scaffold' |
16 | end |
17 | |
18 | body do |
19 | p flash[:notice], :style => "color: green" |
20 | |
21 | self << content_for_layout |
22 | end |
23 | end |
24 | |
25 | == Using Markaby as a Ruby class |
26 | |
27 | Markaby is flaming easy to call from your Ruby classes. |
28 | |
29 | require 'markaby' |
30 | |
31 | mab = Markaby::Builder.new |
32 | mab.html do |
33 | head { title "Boats.com" } |
34 | body do |
35 | h1 "Boats.com has great deals" |
36 | ul do |
37 | li "$49 for a canoe" |
38 | li "$39 for a raft" |
39 | li "$29 for a huge boot that floats and can fit 5 people" |
40 | end |
41 | end |
42 | end |
43 | puts mab.to_s |
44 | |
45 | Markaby::Builder.new does take two arguments for passing in variables and |
46 | a helper object. You can also affix the block right on to the class. |
47 | |
48 | See Markaby::Builder for all of that. |
49 | |
50 | = A Note About <tt>instance_eval</tt> |
51 | |
52 | The Markaby::Builder class is different from the normal Builder class, |
53 | since it uses <tt>instance_eval</tt> when running blocks. This cleans |
54 | up the appearance of the Markaby code you write. If <tt>instance_eval</tt> |
55 | was not used, the code would look like this: |
56 | |
57 | mab = Markaby::Builder.new |
58 | mab.html do |
59 | mab.head { mab.title "Boats.com" } |
60 | mab.body do |
61 | mab.h1 "Boats.com has great deals" |
62 | end |
63 | end |
64 | puts mab.to_s |
65 | |
66 | So, the advantage is the cleanliness of your code. The disadvantage is that |
67 | the block will run inside the Markaby::Builder object's scope. This means |
68 | that inside these blocks, <tt>self</tt> will be your Markaby::Builder object. |
69 | When you use instance variables in these blocks, they will be instance variables |
70 | of the Markaby::Builder object. |
71 | |
72 | This doesn't effect Rails users, but when used in regular Ruby code, it can |
73 | be a bit disorienting. You are recommended to put your Markaby code in a |
74 | module where it won't mix with anything. |
75 | |
76 | = A Note About Rails Helpers |
77 | |
78 | When used in Rails templates, the Rails helper object is passed into |
79 | Markaby::Builder. When you call helper methods inside Markaby, the output |
80 | from those methods will be output to the stream. This is incredibly |
81 | handy, since most Rails helpers output HTML tags. |
82 | |
83 | head do |
84 | javascript_include_tag 'prototype' |
85 | autodiscovery_link_tag |
86 | end |
87 | |
88 | However, some methods are designed to give back a String which you can use |
89 | elsewhere. Call the <tt>@helpers</tt> object with the method and you'll get |
90 | the String back and nothing will be output. |
91 | |
92 | p "Total is: #{@helper.number_to_human_size @file_bytes}" |
93 | |
94 | Conversely, you may call instance variables from your controller by using |
95 | a method and its value will be returned, nothing will be output. |
96 | |
97 | # Inside imaginary ProductController |
98 | def list |
99 | @products = Product.find :all |
100 | end |
101 | |
102 | # Inside app/views/product/list.mab |
103 | products.each do |product| |
104 | p product.title |
105 | end |
106 | |
107 | = A Quick Tour |
108 | |
109 | If you dive right into Markaby, it'll probably make good sense, but you're |
110 | likely to run into a few kinks. Keep these pointers in mind and everything |
111 | will be fine. |
112 | |
113 | == Element Classes |
114 | |
115 | Element classes may be added by hooking methods onto container elements: |
116 | |
117 | div.entry do |
118 | h2.entryTitle 'Son of WebPage' |
119 | div.entrySection %{by Anthony} |
120 | div.entryContent 'Okay, once again, the idea here is ...' |
121 | end |
122 | |
123 | Which results in: |
124 | |
125 | <div class="entry"> |
126 | <h2 class="entryTitle">Son of WebPage</h2> |
127 | <div class="entrySection">by Anthony</div> |
128 | <div class="entryContent">Okay, once again, the idea here is ...</div> |
129 | </div> |
130 | |
131 | == Element IDs |
132 | |
133 | IDs may be added by the use of bang methods: |
134 | |
135 | div.page! |
136 | div.content! |
137 | h1 "A Short Short Saintly Dog" |
138 | end |
139 | end |
140 | |
141 | Which results in: |
142 | |
143 | <div id="page"> |
144 | <div id="content"> |
145 | <h1>A Short Short Saintly Dog</h1> |
146 | </div> |
147 | </div> |
148 | |
149 | == Markaby assumes XHTML 1.0 Transitional |
150 | |
151 | Output defaults to XHTML 1.0 Transitional. To do XHTML 1.0 Strict, |
152 | try this: |
153 | |
154 | xhtml_strict do |
155 | # innerds |
156 | end |
157 | |
158 | == The <tt>capture</tt> Method |
159 | |
160 | Want to catch a block of HTML as a string and play with it a bit? |
161 | Use the <tt>capture</tt> method. |
162 | |
163 | Commonly used to join HTML blocks together: |
164 | |
165 | div.menu! \ |
166 | ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category| |
167 | capture { link_to category } |
168 | end. |
169 | join( " | " ) |
170 | |
171 | == The <tt>tag!</tt> Method |
172 | |
173 | If you need to force a tag at any time, call <tt>tag!</tt> with the |
174 | tag name followed by the possible arguments and block. The CssProxy |
175 | won't work with this technique. |
176 | |
177 | tag! :select, :id => "country_list" do |
178 | countries.each do |country| |
179 | tag! :option, country |
180 | end |
181 | end |
182 | |
183 | = Credits |
184 | |
185 | Markaby is a work of immense hope by Tim Fletcher and why the lucky stiff. |
186 | Thankyou for giving it a whirl. |
187 | |
188 | Markaby is inspired by the HTML library within cgi.rb. Hopefully it will |
189 | turn around and take some cues. |