shared_mustache 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ require 'mustache'
2
+
3
+ class MustacheView < Mustache
4
+ def partial(name)
5
+ File.read("#{template_path}/#{name}.#{template_extension}")
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module SharedMustache
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'mustache'
1
+ require 'shared_mustache/mustache_view'
2
2
 
3
3
  module SharedMustache
4
4
  module ViewHelpers
@@ -13,8 +13,9 @@ module SharedMustache
13
13
  end
14
14
 
15
15
  def render_mustache(template, options={})
16
- view = Mustache.new
17
- view.template = File.read(SharedMustache.find_template_path(template, params[:controller]))
16
+ view = MustacheView.new
17
+ view.template_name = SharedMustache.find_template_path(template, params[:controller])
18
+ view.template_path = SharedMustache.view_dir
18
19
  view.render(options).html_safe
19
20
  end
20
21
  end
@@ -13,7 +13,7 @@ module SharedMustache
13
13
  end
14
14
 
15
15
  def self.file_name_to_id(filename)
16
- filename.gsub('.mustache', '').gsub("#{view_dir}/", '').gsub('/', '-')
16
+ filename.gsub('.mustache', '').gsub("#{view_dir}/", '')
17
17
  end
18
18
 
19
19
  def self.find_template_path(filename, controller)
@@ -21,7 +21,6 @@ module SharedMustache
21
21
  if directory == '.'
22
22
  directory = controller
23
23
  end
24
-
25
- File.join(view_dir, directory, "_#{File.basename(filename)}.mustache")
24
+ File.join(directory, "_#{File.basename(filename)}")
26
25
  end
27
26
  end
@@ -29,15 +29,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
29
  /*
30
30
  * mustache-loader.js - Mustache template loader to go with flask-mustache
31
31
  *
32
- * This depends on jQuery, and either:
33
- * - Twitter's Hogan.js: https://github.com/twitter/hogan.js or
34
- * - Mustache.js: https://github.com/janl/mustache.js
32
+ * This depends on jQuery, and Twitter's Hogan.js
33
+ * https://github.com/twitter/hogan.js or
35
34
  *
36
35
  * Usage:
37
36
  *
38
37
  * $('#target').mustache('includes/_user.mustache', {user_name:'Jan'});
39
38
  * var html = $.mustache('includes/_user.mustache', {user_name:'Jan'});
40
- * $.mustacheAsFunction('includes/_user.mustache')({user_name:'Jan'});
41
39
  */
42
40
 
43
41
  /*jslint
@@ -55,14 +53,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55
53
  // this is a cached lookup table of templates
56
54
  var cache = {};
57
55
 
56
+ // only load partials once
57
+ var partialsLoaded = false;
58
+
58
59
  var load = function(templateName) {
59
60
  // this function takes names like: "includes/_user.mustache"
60
61
  // and loads them from somewhere else.
61
62
 
62
- // first we need to convert slashes to hyphens, since
63
- // they're DOM valid
64
- templateName = templateName.replace('/', '-');
65
-
66
63
  // they can be cached as functions, or as strings.
67
64
  // Strings are template content.
68
65
  if (typeof cache[templateName] === 'undefined') {
@@ -72,7 +69,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72
69
  cache[templateName] = $('<div />').html(
73
70
  $(document.getElementById(templateName)).html().trim()).text();
74
71
  }
75
- else if (templates[templateName]){
72
+ else if (window.templates && templates[templateName]){
76
73
  cache[templateName] = templates[templateName];
77
74
  }
78
75
  }
@@ -80,34 +77,30 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
80
77
  return cache[templateName];
81
78
  };
82
79
 
83
- var compile = function(templateName) {
84
- // returns a compiled template.
85
- // only works with Hogan.js or if templates pre-compiled.
86
- var templateContent = load(templateName),
87
- template = null;
88
-
89
- if (typeof templateContent === 'string' && window.Hogan) {
90
- template = cache[templateName] = window.Hogan.compile(templateContent);
80
+ var loadPartials = function() {
81
+ var templateName;
82
+
83
+ if (partialsLoaded === false){
84
+ if(typeof templates === "undefined"){
85
+ $('script[type="text/mustache"]').each(function(i, el){
86
+ templateName = $(el).attr('id');
87
+ // stupid hack to turn HTML-encoded templates into strings, see:
88
+ // http://stackoverflow.com/a/2419664/61435
89
+ cache[templateName] = $('<div />').html($(el).html().trim()).text();
90
+ });
91
+ }
91
92
  }
92
- if (template === null) {
93
- $.error("Couldn't compile template " + templateName);
93
+ else if (window.templates) {
94
+ for(template in templates){
95
+ cache[templateName] = templates[templateName];
96
+ }
94
97
  }
95
- return template;
96
98
  };
97
99
 
98
- var renderFunction = function(templateName) {
99
- // returns a wrapped `render` function
100
- // only works with Hogan.js or if templates pre-compiled.
101
- var template = compile(templateName);
102
-
103
- return function(context) {
104
- return template.render(context);
105
- };
106
- };
107
100
 
108
101
  var render = function(templateName, context) {
109
-
110
- // first we need to try and load the template
102
+ // first we need to try and load the template and partials
103
+ loadPartials();
111
104
  var template = load(templateName);
112
105
 
113
106
  if (typeof template === 'undefined') {
@@ -116,21 +109,17 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
116
109
  // pre-compiled hogan templates are objects
117
110
  else if (typeof template === 'object') {
118
111
  // template has been pre-compiled, just render and return it
119
- return template.render(context);
112
+ return template.render(context, cache);
120
113
  }
121
114
 
122
115
  // template hasn't been pre-compiled yet
123
116
  // so we need to do other things
124
117
  if (window.Hogan) {
125
- return window.Hogan.compile(template).render(context);
126
- }
127
-
128
- if (window.Mustache) {
129
- return window.Mustache.render(template, context);
118
+ return window.Hogan.compile(template).render(context, cache);
130
119
  }
131
120
 
132
- // we don't have Hogan or Mustache, so we need to bail
133
- $.error('Must have either Hogan.js or Mustache.js to load string templates');
121
+ // we don't have Hogan so we need to bail
122
+ $.error('Must have Hogan.js to load string templates');
134
123
  };
135
124
 
136
125
  $.fn.mustache = function(templateName, context) {
@@ -145,12 +134,4 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145
134
 
146
135
  return render(templateName, context);
147
136
  };
148
-
149
- $.mustacheAsFunction = function(templateName) {
150
- // returns a function that can be used to render the
151
- // mustache template
152
-
153
- return renderFunction(templateName);
154
- };
155
-
156
137
  }(jQuery));
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shared_mustache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-02 00:00:00.000000000 Z
12
+ date: 2014-05-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mustache
@@ -77,6 +77,7 @@ files:
77
77
  - lib/shared_mustache/config.rb
78
78
  - lib/shared_mustache/engine.rb
79
79
  - lib/shared_mustache/hogan.rb
80
+ - lib/shared_mustache/mustache_view.rb
80
81
  - lib/shared_mustache/railtie.rb
81
82
  - lib/shared_mustache/version.rb
82
83
  - lib/shared_mustache/view_helpers.rb
@@ -101,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
102
  version: '0'
102
103
  segments:
103
104
  - 0
104
- hash: -2914413933080530514
105
+ hash: -2201938304553543237
105
106
  required_rubygems_version: !ruby/object:Gem::Requirement
106
107
  none: false
107
108
  requirements:
@@ -110,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
111
  version: '0'
111
112
  segments:
112
113
  - 0
113
- hash: -2914413933080530514
114
+ hash: -2201938304553543237
114
115
  requirements: []
115
116
  rubyforge_project:
116
117
  rubygems_version: 1.8.23