|
| 1 | +Mote |
| 2 | +==== |
| 3 | + |
| 4 | +Minimum Operational Template. |
| 5 | + |
| 6 | +Description |
| 7 | +----------- |
| 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. |
| 14 | + |
| 15 | +Usage |
| 16 | +----- |
| 17 | + |
| 18 | +Usage is very similar to that of ERB: |
| 19 | + |
| 20 | + template = Mote.parse("This is a template") |
| 21 | + template.call #=> "This is a template" |
| 22 | + |
| 23 | +Silly example, you may say, and I would agree. What follows is a short list of |
| 24 | +the different use cases you may face: |
| 25 | + |
| 26 | +## Assignment |
| 27 | + |
| 28 | + example = Mote.parse("<%= \"***\" %>") |
| 29 | + assert_equal "***", example.call |
| 30 | + |
| 31 | +## Comment |
| 32 | + |
| 33 | + example = Mote.parse("*<%# \"*\" %>*") |
| 34 | + assert_equal "**", example.call |
| 35 | + |
| 36 | +## Control flow |
| 37 | + |
| 38 | + example = Mote.parse("<% if false %>*<% else %>***<% end %>") |
| 39 | + assert_equal "***", example.call |
| 40 | + |
| 41 | +## Block evaluation |
| 42 | + |
| 43 | + example = Mote.parse("<% 3.times { %>*<% } %>") |
| 44 | + assert_equal "***", example.call |
| 45 | + |
| 46 | +## Parameters |
| 47 | + |
| 48 | + example = Mote.parse("<% params[:n].times { %>*<% } %>") |
| 49 | + assert_equal "***", example[n: 3] |
| 50 | + assert_equal "****", example[n: 4] |
| 51 | + |
| 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 | + |
| 58 | +# Helpers |
| 59 | + |
| 60 | +There are a couple helpers available in the `Mote::Helpers` module, and you are |
| 61 | +free to include it in your code. To do it, just type: |
| 62 | + |
| 63 | + include Mote::Helpers |
| 64 | + |
| 65 | +Here are the available helpers: |
| 66 | + |
| 67 | +## `mote` |
| 68 | + |
| 69 | +The `mote` helper receives a template string and returns the rendered version |
| 70 | +of it: |
| 71 | + |
| 72 | + assert_equal "1 2 3", mote("1 <%= 2 %> 3") |
| 73 | + |
| 74 | +It works with parameters too: |
| 75 | + |
| 76 | + assert_equal "1 2 3", mote("1 <%= params[:n] %> 3", :n => 2) |
| 77 | + |
| 78 | +## `mote_file` |
| 79 | + |
| 80 | +The `mote_file` helper receives a file name without the `.erb` extension and |
| 81 | +returns the rendered version of its content. The compiled template is cached |
| 82 | +for subsequent calls. |
| 83 | + |
| 84 | + assert_equal "***\n", mote_file("test/basic", :n => 3) |
| 85 | + |
| 86 | +Installation |
| 87 | +------------ |
| 88 | + |
| 89 | + $ gem install mote |
| 90 | + |
| 91 | +License |
| 92 | +------- |
| 93 | + |
| 94 | +Copyright (c) 2011 Michel Martens |
| 95 | + |
| 96 | +Permission is hereby granted, free of charge, to any person |
| 97 | +obtaining a copy of this software and associated documentation |
| 98 | +files (the "Software"), to deal in the Software without |
| 99 | +restriction, including without limitation the rights to use, |
| 100 | +copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 101 | +copies of the Software, and to permit persons to whom the |
| 102 | +Software is furnished to do so, subject to the following |
| 103 | +conditions: |
| 104 | + |
| 105 | +The above copyright notice and this permission notice shall be |
| 106 | +included in all copies or substantial portions of the Software. |
| 107 | + |
| 108 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 109 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 110 | +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 111 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 112 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 113 | +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 114 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 115 | +OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments