handlebars 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e02cf1f5fb51ccaa464dc52fa3430bbcf0eebff0
4
- data.tar.gz: ff84310db2c0686cf8005eb1bf2ae14ab8d7cc7b
3
+ metadata.gz: 9d2bdab1c580180f109b07e8741f5d3b0633f2d7
4
+ data.tar.gz: 3a7ff190e74b96058cf1ef5652e9ca5faa3b89f1
5
5
  SHA512:
6
- metadata.gz: 8eaa9163c0700702d1ef3cd14600b45301ea198671d85d4f84dde3f71d4ac0b74c33cfd370d975b55e890580215dccb3dbdc2ec6011366e7915351d8457b138f
7
- data.tar.gz: 66295c89522a5d2047a1317901b7cb72a2bd2ea6db72238530e5d10234f9b0dcae51bbf6d07bce3af91e7ae8c42e7944803f54a34e1f9e3641e080f1a777b516
6
+ metadata.gz: 4a7d614c49902c8a7e8bed1387da9befc7a66fcf977e22c9f67265ea8919dd457af5d513325b47ffdb574b52f98fa0122233f1362ced53a510d47c3f0d3267b8
7
+ data.tar.gz: 3deb5c4809f45b071538130252714bd72f37bcff93b9708066df89596e9ec8f5db2daba4156ead66039966cf8d06e8346c294c1598d4a86d6b03e6a588cc5413
@@ -26,6 +26,8 @@ This uses [therubyracer][1] to bind to the _actual_ JavaScript implementation of
26
26
  Just like JavaScript, you can write block helpers with an `{{else}}` section. To print
27
27
  out a section twice if a condition is met:
28
28
 
29
+ # V8 maps the first argument sent to a block to "this". All subsequent arguments are as
30
+ # described in the Handlebars documentation.
29
31
  handlebars.register_helper(:twice) do |context, condition, block|
30
32
  if condition
31
33
  "#{block.fn(context)}#{block.fn(context)}"
@@ -37,6 +39,38 @@ out a section twice if a condition is met:
37
39
  template.call(foo: true) #=> Hurray!Hurray!
38
40
  template.call(foo: false) #=> Boo!
39
41
 
42
+ ### Private variables:
43
+
44
+ Just like JavaScript, block helpers can inject private variables into their child templates.
45
+ These can be accessed in a template using the `@` prefix:
46
+
47
+ handlebars.register_helper(:list) do |this, context, block|
48
+ "<ul>" + context.each_with_index.map do |x, i|
49
+ if block.keys.include? "data"
50
+ data = handlebars.create_frame(block.data)
51
+ data.index = i
52
+ end
53
+ "<li>" + block.fn(x, data: data) + "</li>"
54
+ end.join + "</ul>"
55
+ end
56
+ template = handlebars.compile("{{#list array}}{{@index}}. {{title}}{{/list}}")
57
+ template.call(array: [{title: "Memento"}, {title: "Inception"}])
58
+ #=> "<ul><li>0. Memento</li><li>1. Inception</li></ul>"
59
+
60
+ ### Hash arguments:
61
+
62
+ When using hash arguments, beware of one gotcha - V8 defines the #hash method for every
63
+ object. Therefore, to access the hash object of the options argument Handlebars sends to your
64
+ block, you must use the `[]` method:
65
+
66
+ handlebars.register_helper :list do |this, context, options|
67
+ attrs = options[:hash].map{|k,v| "#{k}=\"#{v}\""}.join(' ')
68
+ "<ul #{attrs}>" + context.map{|item| "<li>" + options.fn(item) + "</li>"}.join + "</ul>"
69
+ end
70
+ template = handlebars.compile(%({{#list nav id="nav-bar" class="top"}}<a href="{{url}}">{{title}}</a>{{/list}}))
71
+ template.call({nav: [{url: 'www.google.com', title: 'Google'}]})
72
+ #=> <ul class="top" id="nav-bar"><li><a href="www.google.com">Google</a></li></ul>
73
+
40
74
  ### Safe Strings
41
75
 
42
76
  By default, handlebars will escape strings that are returned by your block helpers. To
@@ -72,6 +106,14 @@ Missing partials can also be returned as a function:
72
106
  t.call #=> 1 miss(es) when trying to look up a partial
73
107
  t.call #=> 2 miss(es) when tyring to look up a partial
74
108
 
109
+ ### Security
110
+
111
+ In general, you should not trust user-provided templates: a template can call any method
112
+ (with no arguments) or access any property on any object in the `Handlebars::Context`.
113
+
114
+ If you'd like to render user-provided templates, you'd want to make sure you do so in a
115
+ sanitized Context, e.g. no filesystem access, read-only or no database access, etc.
116
+
75
117
  ## Test
76
118
 
77
119
  rspec spec/
@@ -5,6 +5,7 @@ module Handlebars
5
5
  class Context
6
6
  def initialize
7
7
  @js = V8::Context.new
8
+ @js['global'] = {} # there may be a more appropriate object to be used here @MHW
8
9
  @js.load(Handlebars::Source.bundled_path)
9
10
 
10
11
  @partials = handlebars.partials = Handlebars::Partials.new
@@ -14,6 +15,14 @@ module Handlebars
14
15
  ::Handlebars::Template.new(self, handlebars.compile(*args))
15
16
  end
16
17
 
18
+ def load_helpers(helpers_pattern)
19
+ Dir[helpers_pattern].each{ |path| load_helper(path) }
20
+ end
21
+
22
+ def load_helper(path)
23
+ @js.load(path)
24
+ end
25
+
17
26
  def precompile(*args)
18
27
  handlebars.precompile(*args)
19
28
  end
@@ -26,6 +35,10 @@ module Handlebars
26
35
  handlebars.registerPartial(name, content)
27
36
  end
28
37
 
38
+ def create_frame(data)
39
+ handlebars.createFrame(data)
40
+ end
41
+
29
42
  def partial_missing(&fn)
30
43
  @partials.partial_missing = fn
31
44
  end
@@ -1,3 +1,3 @@
1
1
  module Handlebars
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Lowell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-24 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: therubyracer
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.12.0
19
+ version: 0.12.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.12.0
26
+ version: 0.12.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: handlebars-source
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.3.0
33
+ version: 3.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.3.0
40
+ version: 3.0.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '2.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.0'
69
69
  description: Uses the actual JavaScript implementation of Handlebars, but supports
@@ -75,24 +75,16 @@ executables: []
75
75
  extensions: []
76
76
  extra_rdoc_files: []
77
77
  files:
78
- - .gitignore
79
- - .rspec
80
- - .travis.yml
81
- - Changelog.md
82
- - Gemfile
83
78
  - README.mdown
84
- - Rakefile
85
- - handlebars.gemspec
86
79
  - lib/handlebars.rb
87
80
  - lib/handlebars/context.rb
88
81
  - lib/handlebars/partials.rb
89
82
  - lib/handlebars/safe_string.rb
90
83
  - lib/handlebars/template.rb
91
84
  - lib/handlebars/version.rb
92
- - spec/handlebars_spec.rb
93
- - spike.rb
94
- homepage: http://github.com/cowboyd/handlebars.rb
95
- licenses: []
85
+ homepage: https://github.com/cowboyd/handlebars.rb
86
+ licenses:
87
+ - MIT
96
88
  metadata: {}
97
89
  post_install_message:
98
90
  rdoc_options: []
@@ -100,19 +92,18 @@ require_paths:
100
92
  - lib
101
93
  required_ruby_version: !ruby/object:Gem::Requirement
102
94
  requirements:
103
- - - '>='
95
+ - - ">="
104
96
  - !ruby/object:Gem::Version
105
97
  version: '0'
106
98
  required_rubygems_version: !ruby/object:Gem::Requirement
107
99
  requirements:
108
- - - '>='
100
+ - - ">="
109
101
  - !ruby/object:Gem::Version
110
102
  version: '0'
111
103
  requirements: []
112
- rubyforge_project: handlebars
113
- rubygems_version: 2.0.3
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
114
106
  signing_key:
115
107
  specification_version: 4
116
108
  summary: Ruby bindings for the handlebars.js templating library
117
- test_files:
118
- - spec/handlebars_spec.rb
109
+ test_files: []
data/.gitignore DELETED
@@ -1,4 +0,0 @@
1
- pkg/*
2
- *.gem
3
- .bundle
4
- Gemfile.lock
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --colour
@@ -1,6 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.1.0
4
- - 2.0.0
5
- - 1.9.3
6
-
@@ -1,3 +0,0 @@
1
- # 0.2.3
2
-
3
- * expose precompilation method
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "https://rubygems.org"
2
- # Specify your gem's dependencies in handlebars.gemspec
3
- gemspec
data/Rakefile DELETED
@@ -1,7 +0,0 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
3
-
4
- require 'rspec/core/rake_task'
5
- RSpec::Core::RakeTask.new(:spec)
6
-
7
- task :default => :spec
@@ -1,26 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "handlebars/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "handlebars"
7
- s.version = Handlebars::VERSION
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = ["Charles Lowell"]
10
- s.email = ["cowboyd@thefrontside.net"]
11
- s.homepage = "http://github.com/cowboyd/handlebars.rb"
12
- s.summary = %q{Ruby bindings for the handlebars.js templating library}
13
- s.description = %q{Uses the actual JavaScript implementation of Handlebars, but supports using Ruby objects as template contexts and Ruby procs as view functions and named helpers}
14
-
15
- s.rubyforge_project = "handlebars"
16
-
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
- s.require_paths = ["lib"]
21
-
22
- s.add_dependency "therubyracer", "~> 0.12.0"
23
- s.add_dependency "handlebars-source", "~> 1.3.0"
24
- s.add_development_dependency "rake"
25
- s.add_development_dependency "rspec", "~> 2.0"
26
- end
@@ -1,103 +0,0 @@
1
-
2
- require 'handlebars'
3
- describe(Handlebars::Context) do
4
-
5
- describe "a simple template" do
6
- let(:t) {compile("Hello {{name}}")}
7
- it "allows simple subsitution" do
8
- t.call(:name => 'World').should eql "Hello World"
9
- end
10
-
11
- it "allows Ruby blocks as a property" do
12
- t.call(:name => lambda {|context| ;"Mate"}).should eql "Hello Mate"
13
- end
14
-
15
- it "can use any Ruby object as a context" do
16
- t.call(double(:Object, :name => "Flipper")).should eql "Hello Flipper"
17
- end
18
- end
19
-
20
- describe "allows Handlebars whitespace operator" do
21
- let(:t) {compile("whitespace {{~word~}} be replaced.")}
22
- it "consumes all whitespace characters before/after the tag with the whitespace operator" do
23
- t.call(:word => "should").should eql "whitespaceshouldbe replaced."
24
- end
25
- end
26
-
27
- describe "registering Helpers" do
28
- before do
29
- subject.register_helper('alsowith') do |this, context, block|
30
- block.fn(context)
31
- end
32
- subject.register_helper(:twice) do |this, block|
33
- "#{block.fn}#{block.fn}"
34
- end
35
- end
36
-
37
- it "correctly passes context and implementation" do
38
- t = compile("it's so {{#alsowith weather}}*{{summary}}*{{/alsowith}}!")
39
- t.call(:weather => {:summary => "sunny"}).should eql "it's so *sunny*!"
40
- end
41
-
42
- it "doesn't nee a context or arguments to the call" do
43
- t = compile("{{#twice}}Hurray!{{/twice}}")
44
- t.call.should eql "Hurray!Hurray!"
45
- end
46
- end
47
-
48
- describe "registering Partials" do
49
- before do
50
- subject.register_partial('legend', 'I am {{who}}')
51
- end
52
- it "renders partials" do
53
- compile("{{> legend}}").call(:who => 'Legend!').should eql "I am Legend!"
54
- end
55
- end
56
-
57
- describe "dynamically loading partial" do
58
- it "can be done with a string" do
59
- subject.partial_missing do |name|
60
- "unable to find >#{name}"
61
- end
62
- compile("I am {{>missing}}").call().should eql "I am unable to find >missing"
63
- end
64
-
65
- it "can be done with a function" do
66
- subject.partial_missing do |name|
67
- lambda do |this, context, options|
68
- "unable to find my #{name} #{context.what}"
69
- end
70
- end
71
- compile("I am {{>missing}}").call(:what => 'shoes').should eql "I am unable to find my missing shoes"
72
- end
73
- end
74
-
75
- describe "creating safe strings from ruby" do
76
- let(:t) {subject.compile("{{safe}}")}
77
- it "respects safe strings returned from ruby blocks" do
78
- t.call(:safe => lambda {|this, *args| Handlebars::SafeString.new("<pre>totally safe</pre>")}).should eql "<pre>totally safe</pre>"
79
- end
80
- end
81
-
82
- describe "context specific data" do
83
- before {subject['foo'] = 'bar'}
84
- it 'can be get and set' do
85
- subject['foo'].should eql 'bar'
86
- end
87
- end
88
-
89
- describe "precompiling templates" do
90
- let(:t) {precompile("foo {{bar}}")}
91
- it "should compile down to javascript" do
92
- t.should be_start_with 'function'
93
- end
94
- end
95
-
96
- def compile(*args)
97
- subject.compile(*args)
98
- end
99
-
100
- def precompile(*args)
101
- subject.precompile(*args)
102
- end
103
- end
data/spike.rb DELETED
@@ -1,17 +0,0 @@
1
- require 'handlebars'
2
-
3
- Handlebars.register_helper "table" do |block|
4
- "<table>#{block.call}</table>"
5
- end
6
-
7
- Handlebars.register_helper "row" do |block|
8
- "<tr class='awesome-row'>#{block.call}</tr>"
9
- end
10
-
11
- t = Handlebars.compile <<-HBS
12
- {{#table width}}
13
- {{#row}}<td>Hi</td>{{/row}}
14
- {{/table}}
15
- HBS
16
-
17
- puts t.call