-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_helper.html.erb
282 lines (209 loc) · 9.31 KB
/
template_helper.html.erb
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
<h1>Template Helpers</h1>
<p>Template helpers are methods which can be used to simplify HTML building. Fire.app provides a lot of built-in helpers and you can customize what you want.</p>
<h2>LiveReload</h2>
<p>The following code will generate <a href="https://github.com/livereload/livereload-js">LiveReload-js</a> :</p>
<pre class="prettyprint linenums lang-html">
<%= livereload_js %>
</pre>
<p>Only insert LiveReload-js in the develope files, not in the output static files:</p>
<pre class="prettyprint linenums lang-html">
<%= livereload_js if ENV["RACK_ENV"] != "production" %>
</pre>
<h2>Content Helpers</h2>
<p>These helpers handle duplicate work when building content.</p>
<h3><code>capture(&block)</code></h3>
<p>The capture method allows you to extract part of a template into a variable. You can use this variable anywhere in your templates or layouts.</p>
<p>For example, below we put a section of template into the variable <code>@today</code>.</p>
<pre class="prettyprint linenums lang-html">
<% @today = capture do %>
Today is
<%= Time.now.to_date %>
<% end %>
</pre>
<p>To use the variable, just write this:</p>
<pre class="prettyprint linenums lang-html">
<%= @today %>
</pre>
<h3><code>content_for(symbol, &block)</code></h3>
<p>This helper stores a block of markup in an identifier for later use. You can make subsequent calls to the stored content in other templates, helper modules or layouts by passing the identifier as an argument to content_for.</p>
<p>For example, we can write this inside the sidebar area in our layout template.</p>
<pre class="prettyprint linenums lang-html">
<%= yield :special_block %>
</pre>
<p>And write this in the "about" page:</p>
<pre class="prettyprint linenums lang-html">
<% content_for :special_block do %>
<h2>Our boss says</h2>
<p>Blah blah blah blah blah…</p>
<% end %>
</pre>
<p>This uses the same layout and shows "Our boss says" only in the "about" page.</p>
<h3><code>content_for?(symbol)</code></h3>
<p>This helper checks whether any content has been captured yet using <code>content_for</code>. Continuing with the previous example, if we want every page without a special block to show a default link, we can change <code><%= yield :special_block %></code> to:</p>
<pre class="prettyprint linenums lang-html">
<% if content_for?(:special_block) %>
<%= yield :special_block %>
<% else %>
<a href="#">Special Discount!!!</a>
<% end %>
</pre>
<h2>Tag Helpers</h2>
<p>Tag helpers are used to construct HTML "tags" within a view template.</p>
<h3><code>link_to(name, href, ...)</code></h3>
<p>This helper creates a link tag with attributes.</p>
<pre class="prettyprint linenums lang-ruby">
<%= link_to("twitter", "http://twitter.com", :id => "twitter") %>
# => <a id="twitter" href="http://twitter.com">twitter</a>
</pre>
<h1></h1>
<h3><code>mail_to(email_address, …)</code></h3>
<p>This helper creates a mailto link tag for the specified email_address.</p>
<pre class="prettyprint linenums lang-ruby">
<%= mail_to("[email protected]", "example mail address") %>
# => <a href="mailto:[email protected]">example mail address</a>
</pre>
<h3><code>content_tag(name, content, ...)</code></h3>
<p>This helper returns an HTML block tag of type name surrounding the content, and attributes can be added. For example:</p>
<pre class="prettyprint linenums lang-ruby">
<%= content_tag(:p, "OOPS!", :class => "strong") %>
# => <p class="strong">OOPS!</p>
</pre>
<p>Nesting is also supported as shown here:</p>
<pre class="prettyprint linenums lang-html">
<%= content_tag(:div, content_tag(:p, "OOPS!"), :class => "strong", :id => "oops") %>
</pre>
<p>The code above returns the following:</p>
<pre class="prettyprint linenums lang-html">
<div id="oops" class="strong">
<p>OOPS!</p>
</div>
</pre>
<h3><code>tag(name, ...)</code></h3>
<p>This helper returns an empty HTML tag.</p>
<pre class="prettyprint linenums lang-ruby">
<%= tag("br") %>
# => <br />
</pre>
<h3><code>image_tag(src, ...)</code></h3>
<p>This helper returns an img tag.</p>
<pre class="prettyprint linenums lang-ruby">
<%= image_tag("test.jpg") %>
# => <img src="test.jpg" />
</pre>
<h3><code>javascript_tag(...)</code></h3>
<p>This helper is used to insert javascripts. As shown here:</p>
<pre class="prettyprint linenums lang-ruby">
<%= javascript_tag "alert('OOPS')" %>
</pre>
<p>The code above returns the following:</p>
<pre class="prettyprint linenums lang-html">
<script type="text/javascript">
//<![CDATA[
alert('OOPS')
//]]>
</script>
</pre>
<h3><code>javascript_include_tag(*sources)</code></h3>
<p>This helper returns an HTML script tag for each of the sources provided. For example:</p>
<pre class="prettyprint linenums lang-ruby">
<%= javascript_include_tag "main" %>
# => <script src="/javascripts/main.js" type="text/javascript"></script>
</pre>
<h3><code>stylesheet_link_tag(*sources)</code></h3>
<p>This helper returns a stylesheet link tag for the sources specified as arguments. For example:</p>
<pre class="prettyprint linenums lang-ruby">
<%= stylesheet_link_tag "main" %>
# => <link href="/stylesheets/main.css" media="screen" rel="stylesheet" type="text/css" />
</pre>
<h2>Escape Helpers</h2>
<p>These process specified tags and formats that need to escape.</p>
<h3><code>html_escape(string)</code></h3>
<p>This helper escapes HTML tags. It also has an alias: <code>h</code>.</p>
<h3><code>json_escape(string)</code></h3>
<p>This helper escapes JSON. It also has an alias: <code>j</code>.</p>
<h2>Lorem Ipsum Helpers</h2>
<p>Fire.app also comes with some helpers to make filler texts and images. The idea is borrowed from <a href="https://github.com/blahed/frank">Frank</a> and <a href="http://middlemanapp.com/">Middleman</a>.</p>
<pre class="prettyprint linenums lang-ruby">
lorem_word # returns a single sentence
lorem_words(argument) # returns individual words, length base on its argument
lorem_sentence
lorem_sentences(argument)
lorem_paragraph
lorem_paragraphs(argument)
lorem_date # returns "ddd mmm dd, yyyy"
lorem_date("%Y/%m/%d", 2011..2013) # returns "yyyy/mm/dd". Year must be in between 2011 to 2013.
lorem_date.to_date # returns "yyyy-mm-dd"
lorem_name # returns full name
lorem_first_name
lorem_last_name
lorem_image("WIDTHxHEIGHT") # returns a lorem image url
</pre>
<p>lorem_image("WIDTHxHEIGHT") uses <a href="http://placehold.it/">http://placehold.it/</a> to generate lorem image url. Usually use this with <code>image_tag</code>. For example: </p>
<pre class="prettyprint linenums lang-ruby">
<%= image_tag(lorem_image("300x400")) %>
# => <img src="http://placehold.it/300x400" />
</pre>
<p>Or use it in HTML directly:</p>
<pre class="prettyprint linenums lang-html">
<img src="<%= lorem_image("300x400") %>" />
</pre>
<h3>Traditional Chinese Lorem Ipsum Helpers</h3>
<p>Fire.app also provide helpers for traditional Chinese:</p>
<ul>
<li>zh_lorem_word, zh_lorem_words</li>
<li>zh_lorem_sentence, zh_lorem_sentences</li>
<li>zh_lorem_paragraph, zh_lorem_paragraphs</li>
<li>zh_lorem_name</li>
<li>zh_lorem_name_pinyin</li>
<li>zh_lorem_first_name</li>
<li>zh_lorem_first_name_pinyin</li>
<li>zh_lorem_last_name</li>
<li>zh_lorem_last_name_pinyin</li>
<li>zh_lorem_email</li>
</ul>
<p>The helpers ending in "_pinyin" and the <code>zh_lorem_email</code> helper replace Chinese with Pinyin.</p>
<h3>Japanese Lorem Ipsum Helpers</h3>
<p>We provide helpers for Japanese, too:</p>
<ul>
<li>jp_lorem_word, jp_lorem_words</li>
<li>jp_lorem_sentence, jp_lorem_sentences</li>
<li>jp_lorem_paragraph, jp_lorem_paragraphs</li>
<li>jp_lorem_name</li>
<li>jp_lorem_name_en</li>
<li>jp_lorem_first_name</li>
<li>jp_lorem_first_name_en</li>
<li>jp_lorem_last_name</li>
<li>jp_lorem_last_name_en</li>
<li>jp_lorem_email</li>
</ul>
<p>The helpers ending in "_en" replace Japanese with Roman letters.</p>
<p class="sp-point">Pull requests are welcome if you want to provide these helpers in your own language.</p>
<h2>Custom Helper </h2>
<p>You can customize your own helpers by creating a file named <code>view_helpers.rb</code> in the root folder. The idea is borrowed from <a href="http://get-serve.com/">Serve</a> and <a href="http://middlemanapp.com/">Middleman</a>. The file format looks like this:</p>
<pre class="prettyprint linenums lang-ruby">
module ViewHelpers
def helper_name(arg1, arg2, ....)
return "something"
end
end
</pre>
<p>For example, take <code>view_helpers.rb</code> as shown here:</p>
<pre class="prettyprint linenums lang-ruby">
module ViewHelpers
def lorem_menu(num)
@list = ""
(1..num).map do |list|
@list += content_tag(:li, link_to(lorem_word, '#'));
end
return '<ul class="menu">'+@list+'</ul>'
end
end
</pre>
<p>Now using <code><%= lorem_menu(3) %></code> returns a lorem menu:</p>
<pre class="prettyprint linenums lang-html">
<ul class="menu">
<li><a href="#">adipisci</a></li>
<li><a href="#">dolor</a></li>
<li><a href="#">eum</a></li>
</ul>
</pre>