handlebars-engine 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +26 -0
- data/lib/handlebars/engine/version.rb +1 -1
- data/lib/handlebars/engine.rb +24 -13
- data/lib/handlebars-engine.rb +3 -0
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cad7fc20a466c4635a87db80dcd3996ef8753bfb6fdbea1d9f90e4463403850b
|
4
|
+
data.tar.gz: c3613182647712f6b9dc8d4da55721e546c1cd4786956524aa5596338e656686
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d28c6fe7442a645a4f4c463776bde45cfece64c431538fe799380f769c520f9ff38a7f3b4bbc619dd366e81d21ba46a0b8c8466e15000c6eec852a3fba499b1
|
7
|
+
data.tar.gz: d2cf108b5e1a029de806fba2366e0a5ff6060d1ac1898e51c98b1242ed9f918ef95f2ef92bb90c57703f11ac4d41044833797f30e97ebea6877b0ae7e9050011
|
data/CHANGELOG.md
CHANGED
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
## [0.3.0] - 2022-01-31
|
11
|
+
|
12
|
+
### Added
|
13
|
+
- `initialize`: add path parameter ([#5](https://github.com/gi/handlebars-ruby/pull/5))
|
14
|
+
- `register_helper`: accept multiple helpers as keyword parameters ([#6](https://github.com/gi/handlebars-ruby/pull/6))
|
15
|
+
- `register_helper`: accept javascript function as string ([#7](https://github.com/gi/handlebars-ruby/pull/7))
|
16
|
+
- `ci`: verify gem builds ([#8](https://github.com/gi/handlebars-ruby/pull/8))
|
17
|
+
- `require`: allow loading from `handlebars-engine` and `handlebars/engine` ([#8](https://github.com/gi/handlebars-ruby/pull/8))
|
18
|
+
|
10
19
|
## [0.2.0] - 2022-01-27
|
11
20
|
|
12
21
|
This is the initial implementation, wrapping the JavaScript Handlebars.
|
data/README.md
CHANGED
@@ -82,6 +82,32 @@ See https://handlebarsjs.com/guide/#custom-helpers.
|
|
82
82
|
|
83
83
|
### Block Helpers
|
84
84
|
|
85
|
+
Block helpers make it possible to define custom iterators and other
|
86
|
+
functionality that can invoke the passed block with a new context.
|
87
|
+
|
88
|
+
Currently, there is a limitation with the underlying JavaScript engine: it does
|
89
|
+
not allow for reentrant calls from within attached Ruby functions: see
|
90
|
+
[MiniRacer#225](https://github.com/rubyjs/mini_racer/issues/225). Thus, the
|
91
|
+
block function returned to the helper (in `options.fn`) cannot be invoked.
|
92
|
+
|
93
|
+
Thus, for block helpers, a string of JavaScript must define the helper function:
|
94
|
+
```ruby
|
95
|
+
handlebars = Handlebars::Engine.new
|
96
|
+
handlebars.register_helper(map: <<~JS)
|
97
|
+
function(...args) {
|
98
|
+
const ctx = this;
|
99
|
+
const opts = args.pop();
|
100
|
+
const items = args[0];
|
101
|
+
const separator = args[1];
|
102
|
+
const mapped = items.map((item) => opts.fn(item));
|
103
|
+
return mapped.join(separator);
|
104
|
+
}
|
105
|
+
JS
|
106
|
+
template = handlebars.compile("{{#map items '|'}}'{{this}}'{{/map}}")
|
107
|
+
template.call({ items: [1, 2, 3] })
|
108
|
+
# => "'1'|2'|'3'"
|
109
|
+
```
|
110
|
+
|
85
111
|
See https://handlebarsjs.com/guide/#block-helpers.
|
86
112
|
|
87
113
|
### Partials
|
data/lib/handlebars/engine.rb
CHANGED
@@ -16,7 +16,10 @@ module Handlebars
|
|
16
16
|
#
|
17
17
|
# @param lazy [true, false] immediately loads and initializes the JavaScript
|
18
18
|
# environment.
|
19
|
-
|
19
|
+
# @param path [String, nil] the path to the version of Handlebars to load.
|
20
|
+
# If `nil`, the contents of `Handlebars::Source.bundled_path` is loaded.
|
21
|
+
def initialize(lazy: false, path: nil)
|
22
|
+
@path = path
|
20
23
|
init! unless lazy
|
21
24
|
end
|
22
25
|
|
@@ -60,14 +63,29 @@ module Handlebars
|
|
60
63
|
|
61
64
|
# Registers helpers accessible by any template in the environment.
|
62
65
|
#
|
66
|
+
# The function can be either a proc or a string:
|
67
|
+
# * When the function is a proc, it can be either passed in as a normal
|
68
|
+
# parameter or as a block.
|
69
|
+
# * When the function is a string, it is interpreted as a JavaScript
|
70
|
+
# function.
|
71
|
+
#
|
63
72
|
# @param name [String, Symbol] the name of the helper
|
73
|
+
# @param function [Proc, String] the helper function
|
64
74
|
# @yieldparam context [Hash] the current context
|
65
75
|
# @yieldparam arguments [Object] the arguments (optional)
|
66
76
|
# @yieldparam options [Hash] the options hash (optional)
|
67
77
|
# @see https://handlebarsjs.com/api-reference/runtime.html#handlebars-registerhelper-name-helper
|
68
|
-
def register_helper(name, &block)
|
69
|
-
|
70
|
-
|
78
|
+
def register_helper(name = nil, function = nil, **helpers, &block)
|
79
|
+
helpers[name] = block || function if name
|
80
|
+
helpers.each do |n, f|
|
81
|
+
case f
|
82
|
+
when Proc
|
83
|
+
attach(n, &f)
|
84
|
+
evaluate("registerHelper('#{n}', #{n})")
|
85
|
+
when String, Symbol
|
86
|
+
evaluate("Handlebars.registerHelper('#{n}', #{f})")
|
87
|
+
end
|
88
|
+
end
|
71
89
|
end
|
72
90
|
|
73
91
|
# Unregisters a previously registered helper.
|
@@ -204,21 +222,14 @@ module Handlebars
|
|
204
222
|
return if @init
|
205
223
|
|
206
224
|
@context = MiniRacer::Context.new
|
207
|
-
@context.load(::Handlebars::Source.bundled_path)
|
225
|
+
@context.load(@path || ::Handlebars::Source.bundled_path)
|
208
226
|
@context.load(File.absolute_path("engine/init.js", __dir__))
|
209
227
|
|
210
228
|
@init = true
|
211
229
|
end
|
212
230
|
|
213
231
|
def js_args(args)
|
214
|
-
args.map { |arg|
|
215
|
-
case arg
|
216
|
-
when Symbol
|
217
|
-
arg
|
218
|
-
else
|
219
|
-
JSON.generate(arg)
|
220
|
-
end
|
221
|
-
}
|
232
|
+
args.map { |arg| JSON.generate(arg) }
|
222
233
|
end
|
223
234
|
end
|
224
235
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handlebars-engine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Gianos
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: handlebars-source
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- LICENSE
|
51
51
|
- README.md
|
52
52
|
- exe/handlebars
|
53
|
+
- lib/handlebars-engine.rb
|
53
54
|
- lib/handlebars/engine.rb
|
54
55
|
- lib/handlebars/engine/init.js
|
55
56
|
- lib/handlebars/engine/version.rb
|
@@ -61,7 +62,7 @@ metadata:
|
|
61
62
|
github_repo: https://github.com/gi/handlebars-ruby
|
62
63
|
homepage_uri: https://github.com/gi/handlebars-ruby
|
63
64
|
source_code_uri: https://github.com/gi/handlebars-ruby
|
64
|
-
post_install_message:
|
65
|
+
post_install_message:
|
65
66
|
rdoc_options: []
|
66
67
|
require_paths:
|
67
68
|
- lib
|
@@ -76,8 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
77
|
- !ruby/object:Gem::Version
|
77
78
|
version: '0'
|
78
79
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
80
|
-
signing_key:
|
80
|
+
rubygems_version: 3.3.3
|
81
|
+
signing_key:
|
81
82
|
specification_version: 4
|
82
83
|
summary: A simple interface to Handlebars.js for Ruby.
|
83
84
|
test_files: []
|