Debian Codegraph @

README.md

raw symbols SCIP
1 # Mustache
2
3 [![Gem Version](https://badge.fury.io/rb/mustache.svg)](http://badge.fury.io/rb/mustache)
4 [![Build Status](https://travis-ci.org/mustache/mustache.svg?branch=master)](https://travis-ci.org/mustache/mustache)
5
6 Inspired by [ctemplate][1] and [et][2], Mustache is a
7 framework-agnostic way to render logic-free views.
8
9 As ctemplates says, "It emphasizes separating logic from presentation:
10 it is impossible to embed application logic in this template language."
11
12 For a list of implementations (other than Ruby) and tips, see
13 <http://mustache.github.io/>.
14
15
16 ## Overview
17
18 Think of Mustache as a replacement for your views. Instead of views
19 consisting of ERB or HAML with random helpers and arbitrary logic,
20 your views are broken into two parts: a Ruby class and an HTML
21 template.
22
23 We call the Ruby class the "view" and the HTML template the
24 "template."
25
26 All your logic, decisions, and code is contained in your view. All
27 your markup is contained in your template. The template does nothing
28 but reference methods in your view.
29
30 This strict separation makes it easier to write clean templates,
31 easier to test your views, and more fun to work on your app's front end.
32
33
34 ## Why?
35
36 I like writing Ruby. I like writing HTML. I like writing JavaScript.
37
38 I don't like writing ERB, Haml, Liquid, Django Templates, putting Ruby
39 in my HTML, or putting JavaScript in my HTML.
40
41
42 ## Installation
43
44 Install the gem locally with:
45
46 $ gem install mustache
47
48 Or add it to your `Gemfile`:
49
50 ```ruby
51 gem "mustache", "~> 1.0"
52 ```
53
54
55 ## Usage
56
57 Quick example:
58
59 >> require 'mustache'
60 => true
61 >> Mustache.render("Hello {{planet}}", planet: "World!")
62 => "Hello World!"
63
64 We've got an `examples` folder but here's the canonical one:
65
66 ```ruby
67 class Simple < Mustache
68 def name
69 "Chris"
70 end
71
72 def value
73 10_000
74 end
75
76 def taxed_value
77 value * 0.6
78 end
79
80 def in_ca
81 true
82 end
83 end
84 ```
85
86 We simply create a normal Ruby class and define methods. Some methods
87 reference others, some return values, some return only booleans.
88
89 Now let's write the template:
90
91 Hello {{name}}
92 You have just won {{value}} dollars!
93 {{#in_ca}}
94 Well, {{taxed_value}} dollars, after taxes.
95 {{/in_ca}}
96
97 This template references our view methods. To bring it all together,
98 here's the code to render actual HTML;
99 ```ruby
100 Simple.render
101 ```
102 Which returns the following:
103
104 Hello Chris
105 You have just won 10000 dollars!
106 Well, 6000.0 dollars, after taxes.
107
108 Simple.
109
110
111 ## Tag Types
112
113 For a language-agnostic overview of Mustache's template syntax, see
114 the `mustache(5)` manpage or
115 <http://mustache.github.io/mustache.5.html>.
116
117
118 ## Escaping
119
120 Mustache does escape all values when using the standard double
121 Mustache syntax. Characters which will be escaped: `& \ " < >` (as
122 well as `'` in Ruby `>= 2.0`). To disable escaping, simply use triple
123 mustaches like `{{{unescaped_variable}}}`.
124
125 Example: Using `{{variable}}` inside a template for `5 > 2` will
126 result in `5 &gt; 2`, where as the usage of `{{{variable}}}` will
127 result in `5 > 2`.
128
129
130 ## Dict-Style Views
131
132 ctemplate and friends want you to hand a dictionary to the template
133 processor. Mustache supports a similar concept. Feel free to mix the
134 class-based and this more procedural style at your leisure.
135
136 Given this template (winner.mustache):
137
138 Hello {{name}}
139 You have just won {{value}} bucks!
140
141 We can fill in the values at will:
142
143 ```ruby
144 view = Winner.new
145 view[:name] = 'George'
146 view[:value] = 100
147 view.render
148 ```
149 Which returns:
150
151 Hello George
152 You have just won 100 bucks!
153
154 We can re-use the same object, too:
155
156 ```ruby
157 view[:name] = 'Tony'
158 view.render # => Hello Tony\nYou have just won 100 bucks!
159 ```
160
161 ## Templates
162
163 A word on templates. By default, a view will try to find its template
164 on disk by searching for an HTML file in the current directory that
165 follows the classic Ruby naming convention.
166
167 TemplatePartial => ./template_partial.mustache
168
169 You can set the search path using `Mustache.template_path`. It can be set on a
170 class by class basis:
171
172 ```ruby
173 class Simple < Mustache
174 self.template_path = __dir__
175 end
176 ```
177 Now `Simple` will look for `simple.mustache` in the directory it resides
178 in, no matter the cwd.
179
180 If you want to just change what template is used you can set
181 `Mustache.template_file` directly:
182
183 ```ruby
184 Simple.template_file = './blah.mustache'
185 ```
186 Mustache also allows you to define the extension it'll use.
187
188 ```ruby
189 Simple.template_extension = 'xml'
190 ```
191 Given all other defaults, the above line will cause Mustache to look
192 for './blah.xml'
193
194 Feel free to set the template directly:
195
196 ```ruby
197 Simple.template = 'Hi {{person}}!'
198 ```
199
200 Or set a different template for a single instance:
201 ```ruby
202 Simple.new.template = 'Hi {{person}}!'
203 ```
204
205 Whatever works.
206
207
208 ## Views
209
210 Mustache supports a bit of magic when it comes to views. If you're
211 authoring a plugin or extension for a web framework (Sinatra, Rails,
212 etc), check out the `view_namespace` and `view_path` settings on the
213 `Mustache` class. They will surely provide needed assistance.
214
215
216 ## Helpers
217
218 What about global helpers? Maybe you have a nifty `gravatar` function
219 you want to use in all your views? No problem.
220
221 This is just Ruby, after all.
222
223 ```ruby
224 module ViewHelpers
225 def gravatar
226 gravatar_id = Digest::MD5.hexdigest(self[:email].to_s.strip.downcase)
227 gravatar_for_id(gravatar_id)
228 end
229
230 def gravatar_for_id(gid, size = 30)
231 "#{gravatar_host}/avatar/#{gid}?s=#{size}"
232 end
233
234 def gravatar_host
235 @ssl ? 'https://secure.gravatar.com' : 'http://www.gravatar.com'
236 end
237 end
238 ```
239
240 Then just include it:
241
242 ```ruby
243 class Simple < Mustache
244 include ViewHelpers
245
246 def name
247 "Chris"
248 end
249
250 def value
251 10_000
252 end
253
254 def taxed_value
255 value * 0.6
256 end
257
258 def in_ca
259 true
260 end
261
262 def users
263 User.all
264 end
265 end
266 ```
267
268 Great, but what about that `@ssl` ivar in `gravatar_host`? There are
269 many ways we can go about setting it.
270
271 Here's an example which illustrates a key feature of Mustache: you
272 are free to use the `initialize` method just as you would in any
273 normal class.
274
275 ```ruby
276 class Simple < Mustache
277 include ViewHelpers
278
279 def initialize(ssl = false)
280 @ssl = ssl
281 end
282 end
283 ```
284
285 Now:
286
287 ```ruby
288 Simple.new(request.ssl?).render
289 ```
290
291 Finally, our template might look like this:
292
293 <ul>
294 {{# users}}
295 <li><img src="{{ gravatar }}"> {{ login }}</li>
296 {{/ users}}
297 </ul>
298
299
300 ## Integrations
301
302 ### Sinatra
303
304 Sinatra integration is available with the
305 [mustache-sinatra gem](https://github.com/mustache/mustache-sinatra).
306
307 An example Sinatra application is also provided:
308 <https://github.com/defunkt/mustache-sinatra-example>
309
310 If you are upgrading to Sinatra 1.0 and Mustache 0.9.0+ from Mustache
311 0.7.0 or lower, the settings have changed. But not that much.
312
313 See [this diff][diff] for what you need to
314 do. Basically, things are named properly now and all should be
315 contained in a hash set using `set :mustache, hash`.
316
317
318 ### [Rack::Bug][4]
319
320 Mustache also provides a `Rack::Bug` panel.
321 First you have to install the `rack-bug-mustache_panel` gem, then in your `config.ru` add the following code:
322
323 ```ruby
324 require 'rack/bug/panels/mustache_panel'
325 use Rack::Bug::MustachePanel
326 ```
327
328 Using Rails? Add this to your initializer or environment file:
329
330 ```ruby
331 require 'rack/bug/panels/mustache_panel'
332 config.middleware.use "Rack::Bug::MustachePanel"
333 ```
334
335 ![Rack::Bug][5]
336
337
338 ### Vim
339
340 vim-mustache-handlebars is available at [mustache/vim-mustache-handlebars][vim]
341
342 ### Emacs
343
344 mustache-mode.el is available at [mustache/emacs][emacs]
345
346
347 ### TextMate
348
349 [Mustache.tmbundle][tmbundle]
350
351 See <https://gist.github.com/defunkt/323624> for installation instructions.
352
353
354 ### Command Line
355
356 See `mustache(1)` man page or <http://mustache.github.io/mustache.1.html>
357 for command line docs.
358
359
360 ## Acknowledgements
361
362 Thanks to [Tom Preston-Werner](https://github.com/mojombo) for showing
363 me ctemplate and [Leah Culver](https://github.com/leah) for the name "Mustache."
364
365 Special thanks to [Magnus Holm](http://judofyr.net/) for all his
366 awesome work on Mustache's parser.
367
368
369 ## Contributing
370
371 Once you've made your great commits:
372
373 1. [Fork][fk] Mustache
374 2. Create a topic branch - `git checkout -b my_branch`
375 3. Push to your branch - `git push origin my_branch`
376 4. Create an [Issue][is] with a link to your branch
377 5. That's it!
378
379
380 ## Mailing List
381
382 ~~To join the list simply send an email to <[email protected]>. This
383 will subscribe you and send you information about your subscription,
384 including unsubscribe information.
385
386 The archive can be found at <http://librelist.com/browser/mustache/>.~~
387
388 The mailing list hasn't been updated in quite a while, please join us on Gitter
389 or IRC:
390
391 [![Join the chat at https://gitter.im/mustache/mustache](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mustache/mustache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
392
393 [#{ on Freenode][irc]
394
395 ## Meta
396
397 * Code: `git clone https://github.com/mustache/mustache.git`
398 * Home: <http://mustache.github.io>
399 * Bugs: <https://github.com/mustache/mustache/issues>
400 * List: <[email protected]>
401 * Gems: <https://rubygems.org/gems/mustache>
402
403 [1]: https://github.com/olafvdspek/ctemplate
404 [2]: http://www.ivan.fomichev.name/2008/05/erlang-template-engine-prototype.html
405 [3]: http://google-ctemplate.googlecode.com/svn/trunk/doc/howto.html
406 [4]: https://github.com/brynary/rack-bug/
407 [5]: http://img.skitch.com/20091027-n8pxwwx8r61tc318a15q1n6m14.png
408 [fk]: https://help.github.com/forking/
409 [is]: https://github.com/mustache/mustache/issues
410 [irc]: irc://irc.freenode.net/#{
411 [vim]: https://github.com/mustache/vim-mustache-handlebars
412 [emacs]: https://github.com/mustache/emacs
413 [tmbundle]: https://github.com/defunkt/Mustache.tmbundle
414 [diff]: https://gist.github.com/defunkt/345490