@@ -6,11 +6,8 @@ Minimum Operational Template.
6
6
Description
7
7
-----------
8
8
9
- Mote is the little brother of ERB. It only provides a subset of ERB's
10
- features, but praises itself of being simple--both internally and externally--
11
- and super fast. It was born out of experimentations while discussing
12
- [ NOLATE] ( https://github.com/antirez/nolate ) , another small library with the
13
- same goals and more features.
9
+ Mote is very simple and fast template engine that praises itself of being
10
+ simple--both internally and externally-- and super fast.
14
11
15
12
Usage
16
13
-----
@@ -23,37 +20,43 @@ Usage is very similar to that of ERB:
23
20
Silly example, you may say, and I would agree. What follows is a short list of
24
21
the different use cases you may face:
25
22
23
+
24
+ % if user == "Bruno"
25
+ ${user} rhymes with Piano
26
+ % elsif user == "Brutus"
27
+ ${user} rhymes with Opus
28
+ % end
29
+
30
+ ## Control flow
31
+
32
+ Lines that start with ` % ` are evaluated as Ruby code.
33
+
26
34
## Assignment
27
35
28
- example = Mote.parse("<%= \"***\" %>")
29
- assert_equal "***", example.call
36
+ Whatever it is between ` ${ ` and ` } ` gets printed in the template.
30
37
31
- ## Comment
38
+ ## Comments
32
39
33
- example = Mote.parse("*<%# \"*\" %>*")
34
- assert_equal "**", example.call
40
+ There's nothing special about comments, it's just a ` # ` inside your Ruby code:
35
41
36
- ## Control flow
42
+ % # This is a comment.
37
43
38
- example = Mote.parse("<% if false %>*<% else %>***<% end %>")
39
- assert_equal "***", example.call
40
44
41
45
## Block evaluation
42
46
43
- example = Mote.parse("<% 3.times { %>*<% } %>")
44
- assert_equal "***", example.call
47
+ As with control instructions, it happens naturally:
48
+
49
+ % 3.times do |i|
50
+ ${i}
51
+ % end
45
52
46
53
## Parameters
47
54
48
- example = Mote.parse("<% params[:n].times { %>*<% } %>")
49
- assert_equal "***", example[n: 3]
50
- assert_equal "****", example[n: 4]
55
+ The values passed to the template are available as local variables:
51
56
52
- Two things are worth noting in the last example: the first is that as the
53
- returned value is a ` Proc ` , you can call it with square brackets and anything
54
- you put inside becomes a parameter. Second, that within the template you have
55
- access to those parameters through the ` params ` hash, instead of the usual
56
- approach of converting each key to a local variable.
57
+ example = Mote.parse("Hello ${name}")
58
+ assert_equal "Hello world", example.call(name: "world")
59
+ assert_equal "Hello Bruno", example.call(name: "Bruno")
57
60
58
61
# Helpers
59
62
@@ -69,46 +72,20 @@ Here are the available helpers:
69
72
The ` mote ` helper receives a template string and returns the rendered version
70
73
of it:
71
74
72
- assert_equal "1 2 3", mote("1 <%= 2 %> 3")
75
+ assert_equal "1 2 3", mote("1 ${2} 3")
73
76
74
77
It works with parameters too:
75
78
76
- assert_equal "1 2 3", mote("1 <%= params[:n] %> 3", :n => 2)
79
+ assert_equal "1 2 3", mote("1 ${n} 3", n: 2)
77
80
78
81
## mote_file
79
82
80
83
The ` mote_file ` helper receives a file name and returns the rendered version of
81
84
its content. The compiled template is cached for subsequent calls.
82
85
83
- assert_equal "***\n", mote_file("test/basic.erb", :n => 3)
86
+ assert_equal "***\n", mote_file("test/basic.erb", n: 3)
84
87
85
88
Installation
86
89
------------
87
90
88
91
$ gem install mote
89
-
90
- License
91
- -------
92
-
93
- Copyright (c) 2011 Michel Martens
94
-
95
- Permission is hereby granted, free of charge, to any person
96
- obtaining a copy of this software and associated documentation
97
- files (the "Software"), to deal in the Software without
98
- restriction, including without limitation the rights to use,
99
- copy, modify, merge, publish, distribute, sublicense, and/or sell
100
- copies of the Software, and to permit persons to whom the
101
- Software is furnished to do so, subject to the following
102
- conditions:
103
-
104
- The above copyright notice and this permission notice shall be
105
- included in all copies or substantial portions of the Software.
106
-
107
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
108
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
109
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
110
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
111
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
112
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
113
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
114
- OTHER DEALINGS IN THE SOFTWARE.
0 commit comments