-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial_2.html.erb
69 lines (61 loc) · 2.46 KB
/
tutorial_2.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
<h1>Fire.app Feature Tutorial</h1>
<h2>Introducing Template Language: ERB</h2>
<p>Fire.app provides many template languages to simplify HTML development.<br />
We choose to introduce ERB here because ERB is actually "HTML with embedded Ruby." Those good with HTML will find ERB is easy to get familiar with.</p>
<p>With Fire.app we can easily use the template language by adding a corresponding file extension. For example, ERB uses this file extension <code>.html.erb</code>.</p>
<p>The three common ways to insert RUBY into ERB are listed below:</p>
<p>Only executes Ruby code and does not output results into HTML: </p>
<pre class="prettyprint linenums lang-ruby">
<% Ruby code... %>
</pre>
<p>Use an equal sign <code>=</code> to execute Ruby code and output results into HTML.</p>
<pre class="prettyprint linenums lang-ruby">
<%= Ruby code... %>
</pre>
<p>Use a sharp sign <code>#</code> to write a comment.</p>
<pre class="prettyprint linenums lang-ruby">
<%# Comment... %>
</pre>
<h3>In addition, there are three commonly used functions:</h3>
<h4>If...else conditional</h4>
<pre class="prettyprint linenums lang-ruby">
<% if condition %>
This part will be executed if condition is true
<% else %>
This part will be executed if condition is false
<% end %>
</pre>
<p>For example:</p>
<pre class="prettyprint linenums lang-ruby">
<% if (1+1==2) %>
This line will be displayed.
<% else %>
This line will never be displayed.
<% end %>
</pre>
<p>Because the condition <code>1+1==2</code> is always true, the result of the code above will be <code>This line will be displayed.</code> no matter what time it runs.</p>
<h4>Do something for n times</h4>
<pre class="prettyprint linenums lang-ruby">
<% n.times do %>
do something
<% end %>
</pre>
<p>For example:</p>
<pre class="prettyprint linenums lang-ruby">
<% 3.times do %>
Good morning!
<% end %>
</pre>
<p>The result of the code above will be <code>Good morning! Good morning! Good morning! </code>.</p>
<h4>Generating a number from 0 to n-1 randomly</h4>
<pre class="prettyprint linenums lang-ruby">
<%= rand(n) %>
</pre>
<p>For example:</p>
<pre class="prettyprint linenums lang-ruby">
<%= rand(3) %>
</pre>
<p>The result of the code above could be <code>0</code>, <code>1</code> or <code>2</code>.</p>
<div class="page-footer">
Next: <a href="tutorial_3.html">Using Template Helpers <i class="lsf-icon arrow"></i></a>
</div>