slim 1.3.0 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.travis.yml +9 -5
  2. data/CHANGES +23 -0
  3. data/Gemfile +13 -2
  4. data/README.md +346 -105
  5. data/Rakefile +21 -9
  6. data/lib/slim.rb +3 -3
  7. data/lib/slim/boolean_attributes.rb +67 -0
  8. data/lib/slim/command.rb +23 -23
  9. data/lib/slim/control_structures.rb +57 -0
  10. data/lib/slim/embedded_engine.rb +80 -43
  11. data/lib/slim/end_inserter.rb +1 -1
  12. data/lib/slim/engine.rb +21 -15
  13. data/lib/slim/filter.rb +0 -6
  14. data/lib/slim/grammar.rb +2 -7
  15. data/lib/slim/interpolation.rb +1 -1
  16. data/lib/slim/logic_less/filter.rb +8 -8
  17. data/lib/slim/logic_less/wrapper.rb +1 -1
  18. data/lib/slim/parser.rb +51 -52
  19. data/lib/slim/splat_attributes.rb +112 -0
  20. data/lib/slim/translator.rb +13 -12
  21. data/lib/slim/version.rb +1 -1
  22. data/slim.gemspec +1 -1
  23. data/test/{slim → core}/helper.rb +3 -7
  24. data/test/{slim → core}/test_code_blocks.rb +0 -0
  25. data/test/{slim → core}/test_code_escaping.rb +4 -4
  26. data/test/{slim → core}/test_code_evaluation.rb +3 -112
  27. data/test/{slim → core}/test_code_output.rb +0 -0
  28. data/test/{slim → core}/test_code_structure.rb +0 -0
  29. data/test/{slim → core}/test_embedded_engines.rb +8 -3
  30. data/test/{slim → core}/test_encoding.rb +0 -0
  31. data/test/core/test_html_attributes.rb +218 -0
  32. data/test/{slim → core}/test_html_escaping.rb +17 -0
  33. data/test/{slim → core}/test_html_structure.rb +13 -98
  34. data/test/{slim → core}/test_parser_errors.rb +24 -15
  35. data/test/{slim → core}/test_pretty.rb +0 -0
  36. data/test/{slim → core}/test_ruby_errors.rb +7 -0
  37. data/test/{slim → core}/test_slim_template.rb +0 -0
  38. data/test/{slim → core}/test_text_interpolation.rb +2 -2
  39. data/test/core/test_thread_options.rb +18 -0
  40. data/test/{slim/logic_less → logic_less}/test_logic_less.rb +21 -0
  41. data/test/{slim/logic_less → logic_less}/test_wrapper.rb +3 -3
  42. data/test/rails/app/controllers/slim_controller.rb +4 -2
  43. data/test/rails/app/views/parents/_form.html.slim +1 -0
  44. data/test/rails/app/views/parents/edit.html.slim +2 -1
  45. data/test/rails/app/views/parents/new.html.slim +2 -1
  46. data/test/rails/app/views/slim/thread_options.html.slim +1 -0
  47. data/test/rails/test/test_slim.rb +6 -4
  48. data/test/{slim/translator → translator}/test_translator.rb +0 -0
  49. metadata +44 -82
  50. data/lib/slim/compiler.rb +0 -194
  51. data/test/rails/app/views/slim/nil.html.slim +0 -1
  52. data/test/slim/test_chain_manipulation.rb +0 -42
@@ -6,10 +6,13 @@ rvm:
6
6
  - rbx-18mode
7
7
  - rbx-19mode
8
8
  env:
9
- - "TASK=test"
9
+ - "TASK=test:core_and_plugins"
10
10
  - "TASK=test:rails RAILS=master"
11
- - "TASK=test:rails RAILS=3.0.11"
12
- - "TASK=test:rails RAILS=3.1.3"
11
+ - "TASK=test:rails RAILS=3.0.17"
12
+ - "TASK=test:rails RAILS=3.1.8"
13
+ - "TASK=test:rails RAILS=3.2.8"
14
+ - "TASK=test:sinatra SINATRA=master"
15
+ - "TASK=test:sinatra SINATRA=1.3.3"
13
16
  matrix:
14
17
  exclude:
15
18
  # Test Rails master only on 1.9.3+ Rubies
@@ -20,9 +23,10 @@ matrix:
20
23
  - rvm: rbx-18mode
21
24
  env: "TASK=test:rails RAILS=master"
22
25
  allow_failures:
23
- - rvm: ruby-head
24
26
  - env: "TASK=test:rails RAILS=master"
25
- script: "bundle exec rake test:ci"
27
+ - env: "TASK=test:sinatra SINATRA=master"
28
+ - rvm: ruby-head
29
+ script: "bundle exec rake $TASK"
26
30
  notifications:
27
31
  email: false
28
32
  irc:
data/CHANGES CHANGED
@@ -1,3 +1,26 @@
1
+ 1.3.2
2
+
3
+ * Fix boolean attributes #299
4
+
5
+ 1.3.1
6
+
7
+ * Support inline html at the beginning of a line (New line indicator <). No pipe symbol is | necessary.
8
+ It is even possible to wrap other Slim syntax in such a html block.
9
+ * Code restructured - Handling of boolean and splat attributes improved and PERFORMANCE improved.
10
+ * BACKWARD INCOMPATIBLE CHANGE - Dynamic attributes which return empty value "" are not removed anymore
11
+ (this means "" is now interpreted as true), whereas false and nil are still removed.
12
+ This corresponds to the definition of boolean attributes in XHTML and HTML5.
13
+ * Deprecated option :remove_empty_attrs (TODO: Remove in 1.4.0)
14
+ * Add option :escape_quoted_attrs to escape quoted attributes, use == if you don't want that.
15
+ The default is false to stay backward compatible.
16
+ * Use Temple::FilterError exception
17
+ * Use Temple::Parser
18
+ * / is not escaped anymore to &#47;
19
+ * Parser: check for missing closing quote in quoted attributes
20
+ * Use new temple option validation to make Slim configuration more user friendly.
21
+ * Support thread options Slim::Engine.with_options which especially useful for Rails
22
+ * Add explicit column number to SyntaxError.to_s
23
+
1
24
  1.3.0
2
25
 
3
26
  * Parser wraps text blocks in [:slim, :text, ...] (Used by Translator/I18n plugin)
data/Gemfile CHANGED
@@ -3,12 +3,14 @@ source :rubygems
3
3
  gemspec
4
4
 
5
5
  if ENV['TRAVIS'] || ENV['TEMPLE'] == 'master'
6
- gem 'temple', :git => 'git://github.com/judofyr/temple.git'
6
+ gem 'temple', :github => 'judofyr/temple'
7
+ elsif ENV['TEMPLE_PATH']
8
+ gem 'temple', :path => ENV['TEMPLE_PATH']
7
9
  end
8
10
 
9
11
  if ENV['RAILS']
10
12
  if ENV['RAILS'] == 'master'
11
- gem 'rails', :git => 'git://github.com/rails/rails.git'
13
+ gem 'rails', :github => 'rails/rails'
12
14
  else
13
15
  gem 'rails', "= #{ENV['RAILS']}"
14
16
  end
@@ -21,3 +23,12 @@ if ENV['RAILS']
21
23
  gem 'sqlite3-ruby'
22
24
  end
23
25
  end
26
+
27
+ if ENV['SINATRA']
28
+ gem 'rack-test'
29
+ if ENV['SINATRA'] == 'master'
30
+ gem 'sinatra', :github => 'sinatra/sinatra'
31
+ else
32
+ gem 'sinatra', "= #{ENV['SINATRA']}"
33
+ end
34
+ end
data/README.md CHANGED
@@ -1,9 +1,22 @@
1
1
  # Slim
2
2
 
3
-
4
3
  [![Build Status](https://secure.travis-ci.org/stonean/slim.png?branch=master)](http://travis-ci.org/stonean/slim) [![Dependency Status](https://gemnasium.com/stonean/slim.png?travis)](https://gemnasium.com/stonean/slim) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/stonean/slim)
5
4
 
6
- Slim is a template language whose goal is to reduce the view syntax to the essential parts without becoming cryptic.
5
+ Slim is a template language whose goal is to reduce the view syntax to the essential parts without becoming cryptic. It started as an exercise to see how much could be removed from a standard html template (<, >, closing tags, etc...). As more people took an interest in Slim, the functionality grew and so did the flexibility of the syntax.
6
+
7
+ A short list of the features...
8
+
9
+ * Short syntax without closing tags (Using indentation instead)
10
+ * Embedded engines like Markdown and Textile
11
+ * Configurable shortcut tags (`#` for `div id` and `.` for `div class` in the default configuration)
12
+ * Automatic HTML escaping and support for Rails' `html_safe?`
13
+ * HTML style mode with closing tags
14
+ * Logic less mode similar to Mustache, realized as plugin
15
+ * Translator/I18n, realized as plugin
16
+ * Highly configurable and extendable
17
+ * High performance (Comparable to ERB)
18
+ * Supported by all major frameworks (Rails, Sinatra, ...)
19
+ * Streaming support in Rails
7
20
 
8
21
  ## Introduction
9
22
 
@@ -14,12 +27,12 @@ continous integration (travis-ci).
14
27
 
15
28
  Slim's core syntax is guided by one thought: "What's the minimum required to make this work".
16
29
 
17
- As more people have contributed to Slim, there have been syntax additions influenced from their use of [Haml](https://github.com/nex3/haml) and [Jade](https://github.com/visionmedia/jade). The Slim team is open to these additions because we know beauty is in the eye of the beholder.
30
+ As more people have contributed to Slim, there have been syntax additions influenced from their use of [Haml](https://github.com/haml/haml) and [Jade](https://github.com/visionmedia/jade). The Slim team is open to these additions because we know beauty is in the eye of the beholder.
18
31
 
19
32
  Slim uses [Temple](https://github.com/judofyr/temple) for parsing/compilation and is also integrated into [Tilt](https://github.com/rtomayko/tilt), so it can be used together with [Sinatra](https://github.com/sinatra/sinatra) or plain [Rack](https://github.com/rack/rack).
20
33
 
21
34
  The architecture of Temple is very flexible and allows the extension of the parsing and compilation process without monkey-patching. This is used
22
- by the logic-less plugin and the translator plugin which provides I18n.
35
+ by the logic less plugin and the translator plugin which provides I18n.
23
36
 
24
37
  ### Why use Slim?
25
38
 
@@ -66,9 +79,10 @@ Here's a quick example to demonstrate what a Slim template looks like:
66
79
  td.name = item.name
67
80
  td.price = item.price
68
81
  - else
69
- p No items found
82
+ p No items found Please add some inventory.
83
+ Thank you!
70
84
 
71
- #footer
85
+ div id="footer"
72
86
  = render 'footer'
73
87
  | Copyright &copy; #{year} #{author}
74
88
 
@@ -100,18 +114,43 @@ Each following line that is indented greater than the backtick is copied over.
100
114
  This line will have two spaces in front of it.
101
115
  And so on...
102
116
 
117
+ You can also embed html in the text line
118
+
119
+ - articles.each do |a|
120
+ | <tr><td>#{a.name}</td><td>#{a.description}</td></tr>
121
+
103
122
  ### Text with trailing space `'`
104
123
 
105
- The single quote tells Slim to copy the line (similar to |), but makes sure that a single trailing space is appended.
124
+ The single quote tells Slim to copy the line (similar to `|`), but makes sure that a single trailing space is appended.
125
+
126
+ ### Inline html `<` (HTML style)
127
+
128
+ You can write html tags directly in Slim which allows you to write your templates in a more html like style with closing tags or mix html and Slim style.
129
+
130
+ <html>
131
+ head
132
+ title Example
133
+ <body>
134
+ - if articles.empty?
135
+ - else
136
+ table
137
+ - articles.each do |a|
138
+ <tr><td>#{a.name}</td><td>#{a.description}</td></tr>
139
+ </body>
140
+ </html>
106
141
 
107
142
  ### Control code `-`
108
143
 
109
144
  The dash denotes control code. Examples of control code are loops and conditionals. `end` is forbidden behind `-`. Blocks are defined only by indentation.
110
- If your ruby code needs to use multiple lines, append a `\` at the end of the lines.
145
+ If your ruby code needs to use multiple lines, append a backslash `\` at the end of the lines.
146
+
147
+ body
148
+ - if articles.empty?
149
+ | No inventory
111
150
 
112
151
  ### Dynamic output `=`
113
152
 
114
- The equal sign tells Slim it's a Ruby call that produces output to add to the buffer. If your ruby code needs to use multiple lines, append a `\` at the end of the lines, for example:
153
+ The equal sign tells Slim it's a Ruby call that produces output to add to the buffer. If your ruby code needs to use multiple lines, append a backslash `\` at the end of the lines, for example:
115
154
 
116
155
  = javascript_include_tag \
117
156
  "jquery", \
@@ -145,13 +184,15 @@ Use the forward slash for code comments - anything after it won't get displayed
145
184
 
146
185
  ### HTML comment `/!`
147
186
 
148
- Use the forward slash immediately followed by an exclamation mark for html comments (` <!-- --> `).
187
+ Use the forward slash immediately followed by an exclamation mark for html comments (`<!-- ... -->`).
149
188
 
150
- ### IE conditional comment `/![IE]`
189
+ ### IE conditional comment `/[...]`
151
190
 
152
- /[ if IE ]
191
+ /[if IE]
153
192
  p Get a better browser.
154
193
 
194
+ renders as
195
+
155
196
  <!--[if IE]><p>Get a better browser.</p><![endif]-->
156
197
 
157
198
  ## HTML tags
@@ -296,6 +337,10 @@ You can use text interpolation in the quoted attributes:
296
337
 
297
338
  a href="http://#{url}" Goto the #{url}
298
339
 
340
+ The attribute value will be escaped by default. Use == if you want to disable escaping in the attribute.
341
+
342
+ a href=="&amp;"
343
+
299
344
  #### Ruby attributes
300
345
 
301
346
  Write the ruby code directly after the `=`. If the code contains spaces you have to wrap
@@ -308,7 +353,9 @@ the code into parentheses `(...)`, `{...}` or `[...]`. The code in the parenthes
308
353
  a href=user_action(user, :edit) Edit #{user.name}
309
354
  a href={path_to_user user} = user.name
310
355
 
311
- Use == if you want to disable escaping in the attribute.
356
+ The attribute value will be escaped by default. Use == if you want to disable escaping in the attribute.
357
+
358
+ a href==action_path(:start)
312
359
 
313
360
  #### Boolean attributes
314
361
 
@@ -329,6 +376,9 @@ The splat shortcut allows you turn a hash in to attribute/value pairs
329
376
 
330
377
  .card*{'data-url'=>place_path(place), 'data-id'=>place.id} = place.name
331
378
  .card *method_which_returns_hash = place.name
379
+ .card *@hash_instance_variable = place.name
380
+
381
+ renders as
332
382
 
333
383
  <div class="card" data-id="1234" data-url="/place/1234">Slim's house</div>
334
384
 
@@ -385,7 +435,7 @@ To escape the interpolation (i.e. render as is)
385
435
 
386
436
  ## Embedded engines (Markdown, ...)
387
437
 
388
- Thanks to Tilt, Slim has impressive support for embedding other template engines.
438
+ Thanks to [Tilt](https://github.com/rtomayko/tilt), Slim has impressive support for embedding other template engines.
389
439
 
390
440
  Examples:
391
441
 
@@ -400,27 +450,24 @@ Examples:
400
450
  Supported engines:
401
451
 
402
452
  <table>
403
- <thead style="font-weight:bold"><tr><td>Engine</td><td>Filter</td><td>Required libraries</td><td>Type</td><td>Description</td></tr></thead>
453
+ <thead style="font-weight:bold"><tr><td>Filter</td><td>Required gems</td><td>Type</td><td>Description</td></tr></thead>
404
454
  <tbody>
405
- <tr><td>Ruby</td><td>ruby:</td><td>none</td><td>Shortcut</td><td>Shortcut to embed ruby code</td></tr>
406
- <tr><td>Javascript</td><td>javascript:</td><td>none</td><td>Shortcut</td><td>Shortcut to embed javascript code and wrap in script tag</td></tr>
407
- <tr><td>CSS</td><td>css:</td><td>none</td><td>Shortcut</td><td>Shortcut to embed css code and wrap in style tag</td></tr>
408
- <tr><td>Sass</td><td>sass:</td><td>sass</td><td>Compile time</td><td>Embed sass code and wrap in style tag</td></tr>
409
- <tr><td>Scss</td><td>scss:</td><td>sass</td><td>Compile time</td><td>Embedd scss code and wrap in style tag</td></tr>
410
- <tr><td>LessCSS</td><td>less:</td><td>less</td><td>Compile time</td><td>Embed less css code and wrap in style tag</td></tr>
411
- <tr><td>Stylus</td><td>styl:</td><td>styl</td><td>Compile time</td><td>Embed stylus css code and wrap in style tag</td></tr>
412
- <tr><td>CoffeeScript</td><td>coffee:</td><td>coffee-script (+node coffee)</td><td>Compile time</td><td>Compile coffee script code and wrap in script tag</td></tr>
413
- <tr><td>RDiscount</td><td>markdown:</td><td>rdiscount/kramdown</td><td>Compile time + Interpolation</td><td>Compile markdown code and interpolate #\{variables} in text</td></tr>
414
- <tr><td>RedCloth</td><td>textile:</td><td>redcloth</td><td>Compile time + Interpolation</td><td>Compile textile code and interpolate #\{variables} in text</td></tr>
415
- <tr><td>Creole</td><td>creole:</td><td>creole</td><td>Compile time + Interpolation</td><td>Compile creole code and interpolate #\{variables} in text</td></tr>
416
- <tr><td>Wikicloth</td><td>wiki:, mediawiki:</td><td>wikicloth</td><td>Compile time + Interpolation</td><td>Compile wiki code and interpolate #\{variables} in text</td></tr>
417
- <tr><td>RDoc</td><td>rdoc:</td><td>rdoc</td><td>Compile time + Interpolation</td><td>Compile rdoc code and interpolate #\{variables} in text</td></tr>
418
- <tr><td>Builder</td><td>builder:</td><td>builder</td><td>Precompiled</td><td>Embed builder code</td></tr>
419
- <tr><td>Nokogiri</td><td>nokogiri:</td><td>nokogiri</td><td>Precompiled</td><td>Embed nokogiri builder code</td></tr>
420
- <tr><td>ERB</td><td>erb:</td><td>none</td><td>Precompiled</td><td>Embed erb code</td></tr>
421
- <tr><td>Liquid</td><td>liquid:</td><td>liquid</td><td>Runtime</td><td>Embed liquid code (Not recommended, no caching)</td></tr>
422
- <tr><td>Radius</td><td>radius:</td><td>radius</td><td>Runtime</td><td>Embed radius code (Not recommended, no caching)</td></tr>
423
- <tr><td>Markaby</td><td>markaby:</td><td>markaby</td><td>Runtime</td><td>Embed markaby code (Not recommended, no caching)</td></tr>
455
+ <tr><td>ruby:</td><td>none</td><td>Shortcut</td><td>Shortcut to embed ruby code</td></tr>
456
+ <tr><td>javascript:</td><td>none</td><td>Shortcut</td><td>Shortcut to embed javascript code and wrap in script tag</td></tr>
457
+ <tr><td>css:</td><td>none</td><td>Shortcut</td><td>Shortcut to embed css code and wrap in style tag</td></tr>
458
+ <tr><td>sass:</td><td>sass</td><td>Compile time</td><td>Embed sass code and wrap in style tag</td></tr>
459
+ <tr><td>scss:</td><td>sass</td><td>Compile time</td><td>Embedd scss code and wrap in style tag</td></tr>
460
+ <tr><td>less:</td><td>less</td><td>Compile time</td><td>Embed less css code and wrap in style tag</td></tr>
461
+ <tr><td>styl:</td><td>styl</td><td>Compile time</td><td>Embed stylus css code and wrap in style tag</td></tr>
462
+ <tr><td>coffee:</td><td>coffee-script</td><td>Compile time</td><td>Compile coffee script code and wrap in script tag</td></tr>
463
+ <tr><td>markdown:</td><td>redcarpet/rdiscount/kramdown</td><td>Compile time + Interpolation</td><td>Compile markdown code and interpolate #\{variables} in text</td></tr>
464
+ <tr><td>textile:</td><td>redcloth</td><td>Compile time + Interpolation</td><td>Compile textile code and interpolate #\{variables} in text</td></tr>
465
+ <tr><td>creole:</td><td>creole</td><td>Compile time + Interpolation</td><td>Compile creole code and interpolate #\{variables} in text</td></tr>
466
+ <tr><td>wiki:, mediawiki:</td><td>wikicloth</td><td>Compile time + Interpolation</td><td>Compile wiki code and interpolate #\{variables} in text</td></tr>
467
+ <tr><td>rdoc:</td><td>rdoc</td><td>Compile time + Interpolation</td><td>Compile rdoc code and interpolate #\{variables} in text</td></tr>
468
+ <tr><td>builder:</td><td>builder</td><td>Precompiled</td><td>Embed builder code</td></tr>
469
+ <tr><td>nokogiri:</td><td>nokogiri</td><td>Precompiled</td><td>Embed nokogiri builder code</td></tr>
470
+ <tr><td>erb:</td><td>none</td><td>Precompiled</td><td>Embed erb code</td></tr>
424
471
  </tbody>
425
472
  </table>
426
473
 
@@ -430,8 +477,10 @@ The embedded engines can be configured in Slim by setting the options directly o
430
477
 
431
478
  ## Configuring Slim
432
479
 
433
- Slim and the underlying Temple framework are highly configurable. Unfortunately the way how you configure Slim depends on the compilation mechanism (Rails or Tilt).
434
- It is always possible to set default options. This can be done in Rails' environment files. For instance, in config/environments/development.rb you probably want:
480
+ Slim and the underlying [Temple](https://github.com/judofyr/temple) framework are highly configurable.
481
+ The way how you configure Slim depends a bit on the compilation mechanism (Rails or [Tilt](https://github.com/rtomayko/tilt)). It is always possible to set default options per `Slim::Engine` class. This can be done in Rails' environment files. For instance, in config/environments/development.rb you probably want:
482
+
483
+ ### Default options
435
484
 
436
485
  # Indent html for pretty debugging and do not sort attributes (Ruby 1.8)
437
486
  Slim::Engine.set_default_options :pretty => true, :sort_attrs => false
@@ -443,18 +492,37 @@ You can also access the option hash directly:
443
492
 
444
493
  Slim::Engine.default_options[:pretty] = true
445
494
 
446
- For developers who know more about Slim and Temple architecture it is possible to override default
447
- options at different positions. Temple uses an inheritance mechanism to allow subclasses to override
448
- options of the superclass. The option priorities are as follows:
495
+ ### Setting options at runtime
496
+
497
+ There are two ways to set options at runtime. For Tilt templates (`Slim::Template`) you can set
498
+ the options when you instatiate the template:
499
+
500
+ Slim::Template.new('template.slim', optional_option_hash).render(scope)
501
+
502
+ The other possibility is to set the options per thread which is interesting mostly for Rails:
503
+
504
+ Slim::Engine.with_options(option_hash) do
505
+ # Any Slim engines which are created here use the option_hash
506
+ # For example in Rails:
507
+ render :page, :layout => true
508
+ end
509
+
510
+ You have to be aware that the compiled engine code and the options are cached per template in Rails and you cannot change the option afterwards.
449
511
 
450
- Options passed at engine instantination > Slim::Template > Slim::Engine > Parser/Filter/Generator (e.g Slim::Parser, Slim::Compiler)
512
+ # First render call
513
+ Slim::Engine.with_options(:pretty => true) do
514
+ render :page, :layout => true
515
+ end
451
516
 
452
- It is also possible to set options for superclasses like Temple::Engine. But this will affect all temple template engines then.
517
+ # Second render call
518
+ Slim::Engine.with_options(:pretty => false) do
519
+ render :page, :layout => true # :pretty is still true because it is cached
520
+ end
453
521
 
454
- Slim::Engine > Temple::Engine
455
- Slim::Compiler > Temple::Filter
522
+ ### Available options
456
523
 
457
524
  The following options are exposed by the `Slim::Engine` and can be set with `Slim::Engine.set_default_options`.
525
+ There are a lot of them but the good thing is, that Slim checks the configuration keys and reports an error if you try to use an invalid configuration key.
458
526
 
459
527
  <table>
460
528
  <thead style="font-weight:bold"><tr><td>Type</td><td>Name</td><td>Default</td><td>Purpose</td></tr></thead>
@@ -464,35 +532,132 @@ The following options are exposed by the `Slim::Engine` and can be set with `Sli
464
532
  <tr><td>String</td><td>:encoding</td><td>"utf-8"</td><td>Set encoding of template</td></tr>
465
533
  <tr><td>String</td><td>:default_tag</td><td>"div"</td><td>Default tag to be used if tag name is omitted</td></tr>
466
534
  <tr><td>Hash</td><td>:shortcut</td><td>\{'.' => 'class', '#' => 'id'}</td><td>Attribute shortcuts</td></tr>
467
- <tr><td>String list</td><td>:enable_engines</td><td>nil <i>(All enabled)</i></td><td>List of enabled embedded engines (whitelist)</td></tr>
468
- <tr><td>String list</td><td>:disable_engines</td><td>nil <i>(None disabled)</i></td><td>List of disabled embedded engines (blacklist)</td></tr>
535
+ <tr><td>Symbol/String list</td><td>:enable_engines</td><td>nil <i>(All enabled)</i></td><td>List of enabled embedded engines (whitelist)</td></tr>
536
+ <tr><td>Symbol/String list</td><td>:disable_engines</td><td>nil <i>(None disabled)</i></td><td>List of disabled embedded engines (blacklist)</td></tr>
469
537
  <tr><td>Boolean</td><td>:disable_capture</td><td>false (true in Rails)</td><td>Disable capturing in blocks (blocks write to the default buffer </td></tr>
470
538
  <tr><td>Boolean</td><td>:disable_escape</td><td>false</td><td>Disable automatic escaping of strings</td></tr>
539
+ <tr><td>Boolean</td><td>:escape_quoted_attrs</td><td>false</td><td>Escape quoted attributes</td></tr>
471
540
  <tr><td>Boolean</td><td>:use_html_safe</td><td>false (true in Rails)</td><td>Use String#html_safe? from ActiveSupport (Works together with :disable_escape)</td></tr>
472
541
  <tr><td>Symbol</td><td>:format</td><td>:xhtml</td><td>HTML output format (Possible formats :xhtml, :html4, :html5, :html)</td></tr>
473
542
  <tr><td>String</td><td>:attr_wrapper</td><td>'"'</td><td>Character to wrap attributes in html (can be ' or ")</td></tr>
474
543
  <tr><td>Hash</td><td>:attr_delimiter</td><td>\{'class' => ' '}</td><td>Joining character used if multiple html attributes are supplied (e.g. class="class1 class2")</td></tr>
475
544
  <tr><td>Boolean</td><td>:sort_attrs</td><td>true</td><td>Sort attributes by name</td></tr>
476
- <tr><td>Boolean</td><td>:remove_empty_attrs</td><td>true</td><td>Remove attributes with empty value</td></tr>
477
545
  <tr><td>Boolean</td><td>:pretty</td><td>false</td><td>Pretty html indenting <b>(This is slower!)</b></td></tr>
478
546
  <tr><td>String</td><td>:indent</td><td>' '</td><td>Indentation string</td></tr>
479
547
  <tr><td>Boolean</td><td>:streaming</td><td>false (true in Rails > 3.1)</td><td>Enable output streaming</td></tr>
480
548
  <tr><td>Class</td><td>:generator</td><td>Temple::Generators::ArrayBuffer/RailsOutputBuffer</td><td>Temple code generator (default generator generates array buffer)</td></tr>
549
+ <tr><td>String</td><td>:buffer</td><td>'_buf' ('@output_buffer' in Rails)</td><td>Variable used for buffer</td></tr>
481
550
  </tbody>
482
551
  </table>
483
552
 
484
- Additionally the code generator options can be set (used by the :generator class). The standard generators support the options :buffer and :capture_generator.
485
- There are more options which are supported by the filters which are used by `Slim::Engine` but which are not exposed and are not officially supported. You
553
+ There are more options which are supported by the Temple filters but which are not exposed and are not officially supported. You
486
554
  have to take a look at the Slim and Temple code for that.
487
555
 
556
+ ### Option priority and inheritance
557
+
558
+ For developers who know more about Slim and Temple architecture it is possible to override default
559
+ options at different positions. Temple uses an inheritance mechanism to allow subclasses to override
560
+ options of the superclass. The option priorities are as follows:
561
+
562
+ 1. `Slim::Template` options passed at engine instatination
563
+ 2. `Slim::Template.default_options`
564
+ 3. `Slim::Engine.thread_options`, `Slim::Engine.default_options`
565
+ 5. Parser/Filter/Generator `thread_options`, `default_options` (e.g `Slim::Parser`, `Slim::Compiler`)
566
+
567
+ It is also possible to set options for superclasses like `Temple::Engine`. But this will affect all temple template engines then.
568
+
569
+ Slim::Engine < Temple::Engine
570
+ Slim::Compiler < Temple::Filter
571
+
488
572
  ## Plugins
489
573
 
490
- ### Logic-less mode
574
+ ### Logic less mode
575
+
576
+ <a name="logicless">Logic less mode</a> is inspired by [Mustache](https://github.com/defunkt/mustache). Logic less mode uses a dictionary object
577
+ e.g. a recursive hash tree which contains the dynamic content.
578
+
579
+ #### Conditional
580
+
581
+ If the object is not false or empty?, the content will show
582
+
583
+ - article
584
+ h1 = title
585
+
586
+ #### Inverted conditional
587
+
588
+ If the object is false or empty?, the content will show
589
+
590
+ -! article
591
+ p Sorry, article not found
592
+
593
+ #### Iteration
594
+
595
+ If the object is an array, the section will iterate
596
+
597
+ - articles
598
+ tr: td = title
599
+
600
+ #### Wrapped dictionary - Resolution order
601
+
602
+ Example code:
603
+
604
+ - article
605
+ h1 = title
606
+
607
+ In wrapped dictionary acccess mode (the default, see the options), the dictionary object is accessed in the following order.
608
+
609
+ 1. If `article.respond_to?(:title)`, Slim will execute `article.send(:title)`
610
+ 2. If `article.respond_to?(:has_key?)` and `article.has_key?(:title)`, Slim will execute `article[:title]`
611
+ 3. If `article.instance_variable_defined?(@title)`, Slim will execute `article.instance_variable_get @title`
612
+
613
+ If all the above fails, Slim will try to resolve the title reference in the same order against the parent object. In this example, the parent would be the dictionary object you are rendering the template against.
614
+
615
+ As you might have guessed, the article reference goes through the same steps against the dictionary. Instance variables are not allowed in the view code, but Slim will find and use them. Essentially, you're just using dropping the @ prefix in your template. Parameterized method calls are not allowed.
616
+
617
+ #### Logic less in Rails
491
618
 
492
- Enable the logic-less plugin with
619
+ Install:
620
+
621
+ $ gem install slim
622
+
623
+ Require:
624
+
625
+ gem 'slim', :require => 'slim/logic_less'
626
+
627
+ You might want to activate logic less mode only for a few actions, you should disable logic-less mode globally at first in the configuration
628
+
629
+ Slim::Engine.set_default_options :logic_less => false
630
+
631
+ and activate logic less mode per render call in your action
632
+
633
+ class Controller
634
+ def action
635
+ Slim::Engine.with_options(:logic_less => true) do
636
+ render
637
+ end
638
+ end
639
+ end
640
+
641
+ #### Logic less in Sinatra
642
+
643
+ Sinata has built-in support for Slim. All you have to do is require the logic less Slim plugin. This can be done in your config.ru:
493
644
 
494
645
  require 'slim/logic_less'
495
646
 
647
+ You are then ready to rock!
648
+
649
+ You might want to activate logic less mode only for a few actions, you should disable logic-less mode globally at first in the configuration
650
+
651
+ Slim::Engine.set_default_options :logic_less => false
652
+
653
+ and activate logic less mode per render call in your application
654
+
655
+ get '/page'
656
+ slim :page, :logic_less => true
657
+ end
658
+
659
+ #### Options
660
+
496
661
  <table>
497
662
  <thead style="font-weight:bold"><tr><td>Type</td><td>Name</td><td>Default</td><td>Purpose</td></tr></thead>
498
663
  <tbody>
@@ -502,18 +667,29 @@ Enable the logic-less plugin with
502
667
  </tbody>
503
668
  </table>
504
669
 
505
- #### Variable output
670
+ ### Translator/I18n
671
+
672
+ The translator plugin provides automatic translation of the templates using Gettext, Fast-Gettext or Rails I18n. Static text
673
+ in the template is replaced by the translated version.
674
+
675
+ Example:
506
676
 
507
- #### Section
677
+ h1 Welcome to #{url}!
508
678
 
509
- #### Inverted section
679
+ Gettext translates the string from english to german where interpolations are replaced by %1, %2, ...
510
680
 
511
- ### Translator
681
+ "Welcome to %1!" -> "Willkommen auf %1!"
682
+
683
+ and renders as
684
+
685
+ <h1>Willkommen auf slim-lang.com!</h1>
512
686
 
513
687
  Enable the translator plugin with
514
688
 
515
689
  require 'slim/translator'
516
690
 
691
+ #### Options
692
+
517
693
  <table>
518
694
  <thead style="font-weight:bold"><tr><td>Type</td><td>Name</td><td>Default</td><td>Purpose</td></tr></thead>
519
695
  <tbody>
@@ -527,7 +703,7 @@ Enable the translator plugin with
527
703
 
528
704
  ### Tilt
529
705
 
530
- Slim uses Tilt to compile the generated code. If you want to use the Slim template directly, you can use the Tilt interface.
706
+ Slim uses [Tilt](https://github.com/rtomayko/tilt) to compile the generated code. If you want to use the Slim template directly, you can use the Tilt interface.
531
707
 
532
708
  Tilt.new['template.slim'].render(scope)
533
709
  Slim::Template.new('template.slim', optional_option_hash).render(scope)
@@ -537,20 +713,19 @@ The optional option hash can have to options which were documented in the sectio
537
713
 
538
714
  ### Sinatra
539
715
 
540
- <pre>
541
- require 'sinatra'
542
- require 'slim'
543
-
544
- get('/') { slim :index }
545
-
546
- __END__
547
- @@ index
548
- doctype html
549
- html
550
- head
551
- title Sinatra With Slim
552
- body
553
- h1 Slim Is Fun!
716
+ <pre>require 'sinatra'
717
+ require 'slim'
718
+
719
+ get('/') { slim :index }
720
+
721
+ __END__
722
+ @@ index
723
+ doctype html
724
+ html
725
+ head
726
+ title Sinatra With Slim
727
+ body
728
+ h1 Slim Is Fun!
554
729
  </pre>
555
730
 
556
731
  ### Rails
@@ -559,8 +734,56 @@ Rails generators are provided by [slim-rails](https://github.com/leogalmeida/sli
559
734
  is not necessary to use Slim in Rails though. Just install Slim and add it to your Gemfile with `gem 'slim'`.
560
735
  Then just use the .slim extension and you're good to go.
561
736
 
737
+ #### Streaming
738
+
739
+ HTTP streaming is enabled enabled by default if you use a Rails version which supports it.
740
+
562
741
  ## Tools
563
742
 
743
+ ### Slim Command 'slimrb'
744
+
745
+ The gem 'slim' comes with the small tool 'slimrb' to test Slim from the command line.
746
+
747
+ <pre>
748
+ $ slimrb --help
749
+ Usage: slimrb [options]
750
+ -s, --stdin Read input from standard input instead of an input file
751
+ --trace Show a full traceback on error
752
+ -c, --compile Compile only but do not run
753
+ -r, --rails Generate rails compatible code (Implies --compile)
754
+ -t, --translator Enable translator plugin
755
+ -l, --logic-less Enable logic less plugin
756
+ -p, --pretty Produce pretty html
757
+ -o, --option [NAME=CODE] Set slim option
758
+ -h, --help Show this message
759
+ -v, --version Print version
760
+ </pre>
761
+
762
+ Start 'slimrb', type your code and press Ctrl-d to send EOF. Example usage:
763
+
764
+ <pre>
765
+ $ slimrb
766
+ markdown:
767
+ First paragraph.
768
+
769
+ Second paragraph.
770
+
771
+ * one
772
+ * two
773
+ * three
774
+
775
+ //Enter Ctrl-d
776
+ &lt;p&gt;First paragraph &lt;/p&gt;
777
+
778
+ &lt;p&gt;Second paragraph &lt;/p&gt;
779
+
780
+ &lt;ul&gt;
781
+ &lt;li&gt;one&lt;/li&gt;
782
+ &lt;li&gt;two&lt;/li&gt;
783
+ &lt;li&gt;three&lt;/li&gt;
784
+ &lt;/ul&gt;
785
+ </pre>
786
+
564
787
  ### Syntax Highlighters
565
788
 
566
789
  There are plugins for various text editors (including the most important ones - Vim, Emacs and Textmate):
@@ -581,8 +804,8 @@ There are plugins for various text editors (including the most important ones -
581
804
  ### Benchmarks
582
805
 
583
806
  *The benchmarks demonstrate that Slim in production mode
584
- is nearly as fast as ERB. So if you choose not to use Slim it
585
- is not due to its speed.*
807
+ is nearly as fast as Erubis (which is the fastest template engine).
808
+ So if you choose not to use Slim it is not due to its speed.*
586
809
 
587
810
  Run the benchmarks with `rake bench`. You can add the option `slow` to
588
811
  run the slow parsing benchmark which needs more time. You can also increase the number of iterations.
@@ -591,38 +814,39 @@ run the slow parsing benchmark which needs more time. You can also increase the
591
814
 
592
815
  <pre>
593
816
  Linux + Ruby 1.9.3, 1000 iterations
817
+
594
818
  user system total real
595
- (1) erb 0.020000 0.000000 0.020000 ( 0.016618)
596
- (1) erubis 0.010000 0.000000 0.010000 ( 0.013974)
597
- (1) fast erubis 0.010000 0.000000 0.010000 ( 0.014625)
598
- (1) temple erb 0.030000 0.000000 0.030000 ( 0.024930)
599
- (1) slim pretty 0.030000 0.000000 0.030000 ( 0.030838)
600
- (1) slim ugly 0.020000 0.000000 0.020000 ( 0.021263)
601
- (1) haml pretty 0.120000 0.000000 0.120000 ( 0.121439)
602
- (1) haml ugly 0.110000 0.000000 0.110000 ( 0.105082)
603
- (2) erb 0.030000 0.000000 0.030000 ( 0.034145)
604
- (2) erubis 0.020000 0.000000 0.020000 ( 0.022493)
605
- (2) temple erb 0.040000 0.000000 0.040000 ( 0.034921)
606
- (2) slim pretty 0.040000 0.000000 0.040000 ( 0.041750)
607
- (2) slim ugly 0.030000 0.000000 0.030000 ( 0.030792)
608
- (2) haml pretty 0.140000 0.000000 0.140000 ( 0.144159)
609
- (2) haml ugly 0.130000 0.000000 0.130000 ( 0.129690)
610
- (3) erb 0.140000 0.000000 0.140000 ( 0.140154)
611
- (3) erubis 0.110000 0.000000 0.110000 ( 0.110870)
612
- (3) fast erubis 0.100000 0.000000 0.100000 ( 0.098940)
613
- (3) temple erb 0.040000 0.000000 0.040000 ( 0.036024)
614
- (3) slim pretty 0.040000 0.000000 0.040000 ( 0.043326)
615
- (3) slim ugly 0.040000 0.000000 0.040000 ( 0.031623)
616
- (3) haml pretty 0.310000 0.000000 0.310000 ( 0.317270)
617
- (3) haml ugly 0.250000 0.000000 0.250000 ( 0.256257)
618
- (4) erb 0.350000 0.000000 0.350000 ( 0.352818)
619
- (4) erubis 0.310000 0.000000 0.310000 ( 0.308558)
620
- (4) fast erubis 0.310000 0.000000 0.310000 ( 0.308920)
621
- (4) temple erb 0.920000 0.000000 0.920000 ( 0.920607)
622
- (4) slim pretty 3.510000 0.000000 3.510000 ( 3.513418)
623
- (4) slim ugly 2.940000 0.000000 2.940000 ( 2.944823)
624
- (4) haml pretty 2.320000 0.000000 2.320000 ( 2.321830)
625
- (4) haml ugly 2.180000 0.000000 2.180000 ( 2.179788)
819
+ (1) erb 0.020000 0.000000 0.020000 ( 0.017383)
820
+ (1) erubis 0.020000 0.000000 0.020000 ( 0.015048)
821
+ (1) fast erubis 0.020000 0.000000 0.020000 ( 0.015372) &lt;===
822
+ (1) temple erb 0.030000 0.000000 0.030000 ( 0.026239)
823
+ (1) slim pretty 0.030000 0.000000 0.030000 ( 0.031463)
824
+ (1) slim ugly 0.020000 0.000000 0.020000 ( 0.018868) &lt;===
825
+ (1) haml pretty 0.130000 0.000000 0.130000 ( 0.122521)
826
+ (1) haml ugly 0.110000 0.000000 0.110000 ( 0.106640)
827
+ (2) erb 0.030000 0.000000 0.030000 ( 0.035520)
828
+ (2) erubis 0.020000 0.000000 0.020000 ( 0.023070)
829
+ (2) temple erb 0.040000 0.000000 0.040000 ( 0.036514)
830
+ (2) slim pretty 0.040000 0.000000 0.040000 ( 0.040086)
831
+ (2) slim ugly 0.030000 0.000000 0.030000 ( 0.028461)
832
+ (2) haml pretty 0.150000 0.000000 0.150000 ( 0.145618)
833
+ (2) haml ugly 0.130000 0.000000 0.130000 ( 0.129492)
834
+ (3) erb 0.140000 0.000000 0.140000 ( 0.134953)
835
+ (3) erubis 0.120000 0.000000 0.120000 ( 0.119723)
836
+ (3) fast erubis 0.100000 0.000000 0.100000 ( 0.097456)
837
+ (3) temple erb 0.040000 0.000000 0.040000 ( 0.035916)
838
+ (3) slim pretty 0.040000 0.000000 0.040000 ( 0.039626)
839
+ (3) slim ugly 0.030000 0.000000 0.030000 ( 0.027827)
840
+ (3) haml pretty 0.310000 0.000000 0.310000 ( 0.306664)
841
+ (3) haml ugly 0.250000 0.000000 0.250000 ( 0.248742)
842
+ (4) erb 0.350000 0.000000 0.350000 ( 0.350719)
843
+ (4) erubis 0.310000 0.000000 0.310000 ( 0.304832)
844
+ (4) fast erubis 0.300000 0.000000 0.300000 ( 0.303070)
845
+ (4) temple erb 0.910000 0.000000 0.910000 ( 0.911745)
846
+ (4) slim pretty 3.410000 0.000000 3.410000 ( 3.413267)
847
+ (4) slim ugly 2.880000 0.000000 2.880000 ( 2.885265)
848
+ (4) haml pretty 2.280000 0.000000 2.280000 ( 2.292623)
849
+ (4) haml ugly 2.170000 0.000000 2.170000 ( 2.169292)
626
850
 
627
851
  (1) Compiled benchmark. Template is parsed before the benchmark and
628
852
  generated ruby code is compiled into a method.
@@ -662,9 +886,26 @@ Slim is working well on all major Ruby implementations:
662
886
  * JRuby
663
887
  * Rubinius 2.0
664
888
 
889
+ ## Contributing
890
+
891
+ If you'd like to help improve Slim, clone the project with Git by running:
892
+
893
+ $ git clone git://github.com/stonean/slim
894
+
895
+ Work your magic and then submit a pull request. We love pull requests!
896
+
897
+ Please remember to test against Ruby versions 1.9.2 and 1.8.7.
898
+
899
+ If you find the documentation lacking (and you probably will), help us out
900
+ The docs are located in the gh-pages branch:
901
+
902
+ $ git checkout gh-pages
903
+
904
+ If you don't have the time to work on Slim, but found something we should know about, please submit an issue.
905
+
665
906
  ## License
666
907
 
667
- This project is released under the MIT license.
908
+ Slim is released under the [MIT license](http://www.opensource.org/licenses/MIT).
668
909
 
669
910
  ## Authors
670
911
 
@@ -707,5 +948,5 @@ Language ports/Similar languages:
707
948
  * [Hamlet.rb (Similar template language)](https://github.com/gregwebs/hamlet.rb)
708
949
  * [Plim (Python port of Slim)](https://github.com/2nd/plim)
709
950
  * [Skim (Slim for Javascript)](https://github.com/jfirebaugh/skim)
710
- * [Haml (Older engine which inspired Slim)](https://github.com/nex3/haml)
711
- * [Jade (Similar engine for javascript)](https://github.com/visionmedia/jade)
951
+ * [Haml (Older engine which inspired Slim)](https://github.com/haml/haml)
952
+ * [Jade (Similar engine for javascript)](https://github.com/visionmedia/jade)