-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial_3.html.erb
76 lines (55 loc) · 3.43 KB
/
tutorial_3.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
<h1>Fire.app Feature Tutorial</h1>
<h2>Using Template Helpers</h2>
<p>Template helpers are methods which can be used to simplify building HTML. Fire.app provides a lot of built-in helpers and you can customize what you want.</p>
<p>Open <code>http://127.0.0.1:24681/screenshots.html</code> in your browser and you'll see the "Screenshots" page. Then open <code>screenshots.html</code> in your IDE and read the source code. You'll find that it has a lot of duplicated HTML code.</p>
<p>First, modify the file extension of <code>screenshots.html</code> from <code>.html</code> to <code>.html.erb</code>, so we can use ERB syntax in this file.</p>
<p>After modifying the file name, add a helper to generate LiveReload-js before <code><%= h '</body>' %></code></p>
<pre class="prettyprint linenums lang-ruby">
<%= livereload_js %>
</pre>
<p>Refresh the browser to active the added JavaScripts. Now, the browser will automatically refresh after modifying files.</p>
<p>Next, we will use some helpers to simplify the placeholder texts. Please find the HTML content that is duplicated five times in <code><%= h '<div class="main">' %></code>.</p>
<pre class="prettyprint linenums lang-html">
<%= h '<h2>Lorem ipsum title</h2>
<p><img src="http://placehold.it/640x360" /></p>
<p>Lorem ipsum … </p>' %>
</pre>
<p>Delete four sections but leave one, then use helpers to replace the original content.</p>
<pre class="prettyprint linenums lang-html">
<h2><%= lorem_words(4+rand(4)) %></h2>
<p><img src="<%= lorem_image("640x360") %>" /></p>
<p><%= lorem_paragraph %></p>
</pre>
<p>In the code above, we see the usage of <code>rand()</code>. We used <code>lorem_words(4+rand(4))</code> to limit the length of <code>lorem_words()</code>'s result between 4 (4 + 0) to 7 (4 + 3). Using lorem ipsum helpers to generate placeholder texts, we can make the page look more realistic. Furthermore, because the resulting content will automatically change each time after refreshing the browser, it's easy to find problems caused by different content length.
</p>
<p>Then, use ERB syntax to duplicate the content.</p>
<pre class="prettyprint linenums lang-html">
<% 5.times do %>
<h2><%= lorem_words(4+rand(4)) %></h2>
<p><img src="<%= lorem_image("640x360") %>" /></p>
<p><%= lorem_paragraph %></p>
<% end %>
</pre>
<p>Switch to your browser and you will see the content changed to five different placeholder sections.</p>
<p>Use this same idea to change the content of <code><%= h '<div class="links">' %></code> and <code><%= h '<div class="footer">' %></code>.</p>
<p><code><%= h '<div class="links">' %></code></p>
<pre class="prettyprint linenums lang-html">
<div class="links">
<h2>Information Links</h2>
<ul>
<% 5.times do %>
<li><a href="#"><%= lorem_words(4+rand(4)) %></a></li>
<% end %>
</ul>
</div>
</pre>
<p><code><%= h '<div class="footer">' %></code></p>
<pre class="prettyprint linenums lang-html">
<div class="footer">
<%= lorem_sentence %>
</div>
</pre>
<p>Switch to your browser again to verify that the page has already changed. Now we can continue to the next step.</p>
<div class="page-footer">
Next: <a href="tutorial_4.html">Basic Usage of Layout <i class="lsf-icon arrow"></i></a>
</div>