Skip to content

Commit b1d5548

Browse files
authored
lib: initial implementation (#1)
1 parent 789bb14 commit b1d5548

9 files changed

Lines changed: 916 additions & 8 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ jobs:
5353
- ruby-2.6
5454
- ruby-2.7
5555
- ruby-3.0
56+
- ruby-3.1
5657
# - jruby
5758
# - truffleruby
5859
runs-on: ${{ matrix.os }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/[._]*/
2+
!/.github/
23
/doc/
34
/pkg/
45
/spec/reports/

.rubocop.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ Metrics/ClassLength:
119119
- heredoc
120120
Max: 120
121121

122-
Metrics/CyclomaticComplexity:
123-
Max: 10
124-
125122
Metrics/MethodLength:
126123
CountAsOne:
127124
- array
@@ -138,8 +135,14 @@ Metrics/ModuleLength:
138135
Metrics/ParameterLists:
139136
Max: 10
140137

138+
RSpec/ExampleLength:
139+
CountAsOne:
140+
- array
141+
- hash
142+
- heredoc
143+
141144
RSpec/MultipleMemoizedHelpers:
142-
Max: 10
145+
Max: 20
143146

144147
RSpec/NestedGroups:
145148
Max: 8

README.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,144 @@ Or install it yourself as:
2626

2727
## Usage
2828

29-
TODO: Write usage instructions here
29+
### Quick Start
30+
31+
```ruby
32+
handlebars = Handlebars::Engine.new
33+
template = handlebars.compile("{{firstname}} {{lastname}}")
34+
template.call({ firstname: "Yehuda", lastname: "Katz" })
35+
# => "Yehuda Katz"
36+
```
37+
38+
### Custom Helpers
39+
40+
Handlebars helpers can be accessed from any context in a template. You can
41+
register a helper with the `register_helper` method:
42+
43+
```ruby
44+
handlebars = Handlebars::Engine.new
45+
handlebars.register_helper(:loud) do |ctx, arg, opts|
46+
arg.upcase
47+
end
48+
template = handlebars.compile("{{firstname}} {{loud lastname}}")
49+
template.call({ firstname: "Yehuda", lastname: "Katz" })
50+
# => "Yehuda KATZ"
51+
```
52+
53+
#### Helper Arguments
54+
55+
Helpers receive the current context as the first argument of the block.
56+
57+
```ruby
58+
handlebars = Handlebars::Engine.new
59+
handlebars.register_helper(:full_name) do |ctx, opts|
60+
"#{ctx["firstname"]} #{ctx["lastname"]}"
61+
end
62+
template = handlebars.compile("{{full_name}}")
63+
template.call({ firstname: "Yehuda", lastname: "Katz" })
64+
# => "Yehuda Katz"
65+
```
66+
67+
Any arguments to the helper are included as individual positional arguments.
68+
69+
```ruby
70+
handlebars = Handlebars::Engine.new
71+
handlebars.register_helper(:join) do |ctx, *args, opts|
72+
args.join(" ")
73+
end
74+
template = handlebars.compile("{{join firstname lastname}}")
75+
template.call({ firstname: "Yehuda", lastname: "Katz" })
76+
# => "Yehuda Katz"
77+
```
78+
79+
The last argument is a hash of options.
80+
81+
See https://handlebarsjs.com/guide/#custom-helpers.
82+
83+
### Block Helpers
84+
85+
See https://handlebarsjs.com/guide/#block-helpers.
86+
87+
### Partials
88+
89+
Handlebars partials allow for code reuse by creating shared templates.
90+
91+
You can register a partial using the `register_partial` method:
92+
93+
```ruby
94+
handlebars = Handlebars::Engine.new
95+
handlebars.register_partial(:person, "{{person.name}} is {{person.age}}.")
96+
template = handlebars.compile("{{> person person=.}}")
97+
template.call({ name: "Yehuda Katz", age: 20 })
98+
# => "Yehuda Katz is 20."
99+
```
100+
101+
See https://handlebarsjs.com/guide/#partials.
102+
See https://handlebarsjs.com/guide/partials.html.
103+
104+
### Hooks
105+
106+
#### Helper Missing
107+
108+
This hook is called for a mustache or a block-statement when
109+
* a simple mustache-expression is not a registered helper, *and*
110+
* it is not a property of the current evaluation context.
111+
112+
You can add custom handling for those situations by registering a helper with
113+
the `register_helper_missing` method:
114+
115+
```ruby
116+
handlebars = Handlebars::Engine.new
117+
handlebars.register_helper_missing do |ctx, *args, opts|
118+
"Missing: #{opts["name"]}(#{args.join(", ")})"
119+
end
120+
121+
template = handlebars.compile("{{foo 2 true}}")
122+
template.call
123+
# => "Missing: foo(2, true)"
124+
125+
template = handlebars.compile("{{#foo true}}{{/foo}}")
126+
template.call
127+
# => "Missing: foo(true)"
128+
```
129+
130+
See https://handlebarsjs.com/guide/hooks.html#helpermissing.
131+
132+
##### Blocks
133+
134+
This hook is called for a block-statement when
135+
* a block-expression calls a helper that is not registered, *and*
136+
* the name is a property of the current evaluation context.
137+
138+
You can add custom handling for those situations by registering a helper with
139+
the `register_helper_missing` method (with a `:block` argument):
140+
141+
```ruby
142+
handlebars = Handlebars::Engine.new
143+
handlebars.register_helper_missing(:block) do |ctx, *args, opts|
144+
"Missing: #{opts["name"]}(#{args.join(", ")})"
145+
end
146+
147+
template = handlebars.compile("{{#person}}{{name}}{{/person}}")
148+
template.call({ person: { name: "Yehuda Katz" } })
149+
# => "Missing: person"
150+
```
151+
152+
See https://handlebarsjs.com/guide/hooks.html#blockhelpermissing.
153+
154+
#### Partial Missing
155+
156+
This hook is called for a partial that is not registered.
157+
158+
```ruby
159+
handlebars = Handlebars::Engine.new
160+
handlebars.register_partial_missing do |name|
161+
"partial: #{name}"
162+
end
163+
```
164+
165+
Note: This is not a part of the offical Handlebars API. It is provided for
166+
convenience.
30167

31168
## Changelog
32169

handlebars-engine.gemspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ Gem::Specification.new do |spec|
3535
spec.bindir = "exe"
3636
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
3737
spec.require_paths = ["lib"]
38+
39+
spec.add_dependency "handlebars-source"
40+
spec.add_dependency "mini_racer"
3841
end

0 commit comments

Comments
 (0)