mustache 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -88,8 +88,7 @@ class Mustache
88
88
  # Given a file name and an optional context, attempts to load and
89
89
  # render the file as a template.
90
90
  def self.render_file(name, context = {})
91
- data = File.read("#{template_path}/#{name}.#{template_extension}")
92
- render(data, context)
91
+ render(partial(name), context)
93
92
  end
94
93
 
95
94
  # Given a file name and an optional context, attempts to load and
@@ -98,6 +97,22 @@ class Mustache
98
97
  self.class.render_file(name, context)
99
98
  end
100
99
 
100
+ # Given a name, attempts to read a file and return the contents as a
101
+ # string. The file is not rendered, so it might contain
102
+ # {{mustaches}}.
103
+ #
104
+ # Call `render` if you need to process it.
105
+ def self.partial(name)
106
+ File.read("#{template_path}/#{name}.#{template_extension}")
107
+ end
108
+
109
+ # Override this in your subclass if you want to do fun things like
110
+ # reading templates from a database. It will be rendered by the
111
+ # context, so all you need to do is return a string.
112
+ def partial(name)
113
+ self.class.partial(name)
114
+ end
115
+
101
116
  # The template path informs your Mustache subclass where to look for its
102
117
  # corresponding template. By default it's the current directory (".")
103
118
  def self.template_path
@@ -22,31 +22,19 @@ class Mustache
22
22
  #
23
23
  # If the Mustache view handling the rendering (e.g. the view
24
24
  # representing your profile page or some other template) responds
25
- # to `partial`, we call it and use the result. Otherwise we render
26
- # and compile the partial as its own view and return the result.
25
+ # to `partial`, we call it and render the result.
27
26
  def partial(name)
28
- # Look for any Mustaches in the stack.
27
+ # Look for the first Mustache in the stack.
29
28
  mustache = mustache_in_stack
30
29
 
31
- if mustache.respond_to? :partial
32
- # We found a mustache and it responds to `partial`, send it.
33
- mustache.render(mustache.partial(name), self)
34
-
35
- elsif mustache
36
- # We found a mustache without `partial`, use it to render.
37
- mustache.render_file(name, self)
38
- else
39
- # Can't find any staches, abort and use whatever we can..
40
- raise "No Mustache views in stack."
41
- end
30
+ # Call its `partial` method and render the result.
31
+ mustache.render(mustache.partial(name), self)
42
32
  end
43
33
 
44
34
  # Find the first Mustache in the stack. If we're being rendered
45
35
  # inside a Mustache object as a context, we'll use that one.
46
36
  def mustache_in_stack
47
- @stack.detect do |frame|
48
- frame.is_a?(Mustache)
49
- end
37
+ @stack.detect { |frame| frame.is_a?(Mustache) }
50
38
  end
51
39
 
52
40
  # Adds a new object to the context's internal stack.
@@ -48,7 +48,7 @@ EOF
48
48
  SKIP_WHITESPACE = [ '#', '/' ]
49
49
 
50
50
  # The content allowed in a tag name.
51
- ALLOWED_CONTENT = /(\w|[?!-])*/
51
+ ALLOWED_CONTENT = /(\w|[?!\/-])*/
52
52
 
53
53
  # These types of tags allow any content,
54
54
  # the rest only allow ALLOWED_CONTENT.
@@ -56,11 +56,17 @@ class Mustache
56
56
  # compiling templates on each page load.
57
57
  klass = mustache_class(template, options)
58
58
 
59
- # If they aren't explicitly diabling layouts, try to find
59
+ # If they aren't explicitly disabling layouts, try to find
60
60
  # one.
61
61
  if options[:layout] != false
62
+ # Let the user pass in a layout name.
63
+ layout_name = options[:layout]
64
+
65
+ # If all they said was `true` (or nothing), default to :layout.
66
+ layout_name = :layout if layout_name == true || !layout_name
67
+
62
68
  # If they passed a layout name use that.
63
- layout = mustache_class(options[:layout] || :layout, options)
69
+ layout = mustache_class(layout_name, options)
64
70
 
65
71
  # If it's just an anonymous subclass then don't bother, otherwise
66
72
  # give us a layout instance.
@@ -1,3 +1,3 @@
1
1
  class Mustache
2
- Version = '0.9.1'
2
+ Version = '0.9.2'
3
3
  end
@@ -0,0 +1,3 @@
1
+ <h1>中文 {{test}}</h1>
2
+
3
+ {{> utf8_partial}}
@@ -0,0 +1 @@
1
+ <h2>中文又来啦</h2>
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  $LOAD_PATH.unshift File.dirname(__FILE__)
2
3
  require 'helper'
3
4
 
@@ -376,6 +377,20 @@ expected
376
377
  <div>3</div>
377
378
  <div>3</div>
378
379
  <div>3</div>
380
+ rendered
381
+ end
382
+
383
+ def test_utf8
384
+ klass = Class.new(Mustache)
385
+ klass.template_name = 'utf8'
386
+ klass.template_path = 'test/fixtures'
387
+ view = klass.new
388
+ view[:test] = "中文"
389
+
390
+ assert_equal <<-rendered, view.render
391
+ <h1>中文 中文</h1>
392
+
393
+ <h2>中文又来啦</h2>
379
394
  rendered
380
395
  end
381
396
  end
@@ -12,6 +12,15 @@ You have just won $100000!
12
12
  end_partial
13
13
  end
14
14
 
15
+ def test_partial_with_slashes
16
+ klass = Class.new(Mustache)
17
+ klass.template = '{{> test/fixtures/inner_partial}}'
18
+ view = klass.new
19
+ view[:title] = 'success'
20
+
21
+ assert_equal "Again, success!", view.render
22
+ end
23
+
15
24
  def test_view_partial_inherits_context
16
25
  klass = Class.new(TemplatePartial)
17
26
  klass.template_path = File.dirname(__FILE__) + '/fixtures'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-27 00:00:00 -07:00
12
+ date: 2010-03-29 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -87,6 +87,8 @@ files:
87
87
  - test/fixtures/template_partial.txt
88
88
  - test/fixtures/unescaped.mustache
89
89
  - test/fixtures/unescaped.rb
90
+ - test/fixtures/utf8.mustache
91
+ - test/fixtures/utf8_partial.mustache
90
92
  - test/helper.rb
91
93
  - test/mustache_test.rb
92
94
  - test/parser_test.rb