ruby-handlebars 0.2.1 → 0.3.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: ab18e04aceeade619546191a66499ee4d7c1bab5
4
- data.tar.gz: 83080df33384b570dc88269960ee29929eb1b47a
3
+ metadata.gz: c95697b3d0cf8b3500b7249cb254cfb97eb38bf2
4
+ data.tar.gz: 9e1549cddad55b384bcda6fda8e9284c158159de
5
5
  SHA512:
6
- metadata.gz: 1f2a8eac8b1c767a7f33f68c375751b27d54676cd65aa9ed641301ebce1e17a0fd55bc40619544eb691e43449f1231d205cbe6f931a22bfb6d2b28ac6ed201cd
7
- data.tar.gz: 5d814478c5fa2112446ab1263c548596e6d27cf9a78325cc9e83e1dfab677ca83cf04d21285ecf8ad712fee483d712d20880f0bf485c763bc468ba2e0898d387
6
+ metadata.gz: 6f602263f74b2a2cbc186643288f6b7c636969bb3ce07480de9a0f3c7ec00064d7df5c86138764c32c640781f07686099b7abadfbeb8f4d1a8da926240ac76c4
7
+ data.tar.gz: bb6ff52d41ae7c6b1f8d9dbb2c8b0a54370c3f483457eef30e86e692166b2151993354c39d6765db854e7ac77711feb2275a7549431e546c53f979e77174f500
data/README.md CHANGED
@@ -82,22 +82,49 @@ hbs.compile(template).call({description: my_description})
82
82
  # Output will depend on the validity of the 'my_description' variable
83
83
  ```
84
84
 
85
- Two default helpers are provided: ``each`` and ``if``. It is not yet possible to name the current item in an each loop and ``this`` must be used to reference it.
85
+ Default helpers:
86
+ ----------------
86
87
 
87
- ```ruby
88
- template = [
89
- "{{#each items}}",
90
- " {{{ this }}}",
91
- "{{else}}",
92
- " No items",
93
- "{{/each}}",
94
- "",
95
- "{{#if my_condition}}",
96
- " It's ok",
97
- "{{else}}",
98
- " or maybe not",
99
- "{{/if}}",
100
- ].join("\n")
88
+ Three default helpers are provided: ``each``, ``if`` and ``unless``.
89
+
90
+ The each helper let you walk through a list. You can either use the basic notation and referencing the current item as ``this``:
91
+
92
+ ```
93
+ {{#each items}}
94
+ {{{ this }}}
95
+ {{else}}
96
+ No items
97
+ {{/each}}
98
+ ```
99
+
100
+ or the "as |name|" notation:
101
+
102
+ ```
103
+ {{#each items as |item| }}
104
+ {{{ item }}}
105
+ {{else}}
106
+ No items
107
+ {{/each}}
108
+ ```
109
+
110
+ The ``if`` helper can be used to write conditionnal templates:
111
+
112
+ ```
113
+ {{#if my_condition}}
114
+ It's ok
115
+ {{else}}
116
+ or maybe not
117
+ {{/if}}
118
+ ```
119
+
120
+ The ``unless`` helper works the opposite way to ``if``:
121
+
122
+ ```
123
+ {{#unless my_condition}}
124
+ It's not ok
125
+ {{else}}
126
+ or maybe it is
127
+ {{/unless}}
101
128
  ```
102
129
 
103
130
  Currently, if you call an unknown helper, it will raise an exception. You can override that by registering your own version of the ``helperMissing`` helper. Note that only the name of the missing helper will be provided.
@@ -124,4 +151,4 @@ Aknowledgements
124
151
  This gem would simply not exist if the handlebars team was not here. Thanks a lot for this awesome templating system.
125
152
  Thanks a lot to @cowboyd for the [handlebars.rb](https://github.com/cowboyd/handlebars.rb) gem. We used it for a while and it's great (and as told at the beginning of the README, if you do not need any Windows support, use handlebars.rb instead ;) )
126
153
 
127
- Thanks a lot to the contributors @mvz and @schuetzm for making it a way better Handlebars renderer :)
154
+ Thanks a lot to the contributors @mvz, @schuetzm and @stewartmckee for making it a way better Handlebars renderer :)
@@ -11,6 +11,7 @@ module Handlebars
11
11
  attr_reader :escaper
12
12
 
13
13
  def initialize()
14
+ @as_helpers = {}
14
15
  @helpers = {}
15
16
  @partials = {}
16
17
  register_default_helpers
@@ -25,10 +26,18 @@ module Handlebars
25
26
  @helpers[name.to_s] = Helper.new(self, fn)
26
27
  end
27
28
 
29
+ def register_as_helper(name, &fn)
30
+ @as_helpers[name.to_s] = Helper.new(self, fn)
31
+ end
32
+
28
33
  def get_helper(name)
29
34
  @helpers[name.to_s]
30
35
  end
31
36
 
37
+ def get_as_helper(name)
38
+ @as_helpers[name.to_s]
39
+ end
40
+
32
41
  def register_partial(name, content)
33
42
  @partials[name.to_s] = Template.new(self, template_to_ast(content))
34
43
  end
@@ -14,6 +14,14 @@ module Handlebars
14
14
  @fn.call(*args)
15
15
  end
16
16
 
17
+ def apply_as(context, arguments = [], as_arguments = [], block = [], else_block = [])
18
+ arguments = [arguments] unless arguments.is_a? Array
19
+ as_arguments = [as_arguments] unless as_arguments.is_a? Array
20
+ args = [context] + arguments.map {|arg| arg.eval(context)} + as_arguments.map(&:name) + split_block(block, else_block)
21
+
22
+ @fn.call(*args)
23
+ end
24
+
17
25
  private
18
26
 
19
27
  def split_block(block, else_block)
@@ -4,7 +4,11 @@ module Handlebars
4
4
  def self.register(hbs)
5
5
  hbs.register_helper(self.registry_name) do |context, parameters, block, else_block|
6
6
  self.apply(context, parameters, block, else_block)
7
- end
7
+ end if self.respond_to?(:apply)
8
+
9
+ hbs.register_as_helper(self.registry_name) do |context, parameters, as_names, block, else_block|
10
+ self.apply_as(context, parameters, as_names, block, else_block)
11
+ end if self.respond_to?(:apply_as)
8
12
  end
9
13
 
10
14
  # Should be implemented by sub-classes
@@ -15,6 +19,10 @@ module Handlebars
15
19
  # def self.apply(context, parameters, block, else_block)
16
20
  # # Do things and stuff
17
21
  # end
22
+
23
+ # def self.apply_as(context, parameters, as_names, block, else_block)
24
+ # # Do things and stuffa, but with 'as |param| notation'
25
+ # end
18
26
  end
19
27
  end
20
28
  end
@@ -8,14 +8,18 @@ module Handlebars
8
8
  end
9
9
 
10
10
  def self.apply(context, items, block, else_block)
11
+ self.apply_as(context, items, :this, block, else_block)
12
+ end
13
+
14
+ def self.apply_as(context, items, name, block, else_block)
11
15
  if (items.nil? || items.empty?)
12
16
  if else_block
13
17
  result = else_block.fn(context)
14
18
  end
15
19
  else
16
- context.with_temporary_context(:this => nil, :@index => 0, :@first => false, :@last => false) do
20
+ context.with_temporary_context(name => nil, :@index => 0, :@first => false, :@last => false) do
17
21
  result = items.each_with_index.map do |item, index|
18
- context.add_items(:this => item, :@index => index, :@first => (index == 0), :@last => (index == items.length - 1))
22
+ context.add_items(name => item, :@index => index, :@first => (index == 0), :@last => (index == items.length - 1))
19
23
  block.fn(context)
20
24
  end.join('')
21
25
  end
@@ -10,6 +10,8 @@ module Handlebars
10
10
  rule(:slash) { str('/')}
11
11
  rule(:ocurly) { str('{')}
12
12
  rule(:ccurly) { str('}')}
13
+ rule(:pipe) { str('|')}
14
+
13
15
 
14
16
  rule(:docurly) { ocurly >> ocurly }
15
17
  rule(:dccurly) { ccurly >> ccurly }
@@ -17,6 +19,8 @@ module Handlebars
17
19
  rule(:tccurly) { ccurly >> ccurly >> ccurly }
18
20
 
19
21
  rule(:else_kw) { str('else') }
22
+ rule(:as_kw) { str('as') }
23
+
20
24
  rule(:identifier) { (else_kw >> space? >> dccurly).absent? >> match['@\-a-zA-Z0-9_\?'].repeat(1) }
21
25
  rule(:path) { identifier >> (dot >> (identifier | else_kw)).repeat }
22
26
 
@@ -38,8 +42,11 @@ module Handlebars
38
42
  rule(:string) { sq_string | dq_string }
39
43
 
40
44
  rule(:parameter) {
41
- (path | string).as(:parameter_name) |
42
- (str('(') >> space? >> identifier.as(:safe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> str(')'))
45
+ (as_kw >> space? >> pipe).absent? >>
46
+ (
47
+ (path | string).as(:parameter_name) |
48
+ (str('(') >> space? >> identifier.as(:safe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> str(')'))
49
+ )
43
50
  }
44
51
  rule(:parameters) { parameter >> (space >> parameter).repeat }
45
52
 
@@ -48,6 +55,25 @@ module Handlebars
48
55
 
49
56
  rule(:helper) { unsafe_helper | safe_helper }
50
57
 
58
+ rule(:as_block_helper) {
59
+ docurly >>
60
+ hash >>
61
+ identifier.capture(:helper_name).as(:helper_name) >>
62
+ space >> parameters.as(:parameters) >>
63
+ space >> as_kw >> space >> pipe >> space? >> parameters.as(:as_parameters) >> space? >> pipe >>
64
+ space? >>
65
+ dccurly >>
66
+ scope {
67
+ block
68
+ } >>
69
+ scope {
70
+ docurly >> space? >> else_kw >> space? >> dccurly >> scope { block_item.repeat.as(:else_block_items) }
71
+ }.maybe >>
72
+ dynamic { |src, scope|
73
+ docurly >> slash >> str(scope.captures[:helper_name]) >> dccurly
74
+ }
75
+ }
76
+
51
77
  rule(:block_helper) {
52
78
  docurly >>
53
79
  hash >>
@@ -75,7 +101,7 @@ module Handlebars
75
101
  dccurly
76
102
  }
77
103
 
78
- rule(:block_item) { (template_content | unsafe_replacement | safe_replacement | helper | partial | block_helper ) }
104
+ rule(:block_item) { (template_content | unsafe_replacement | safe_replacement | helper | partial | block_helper | as_block_helper) }
79
105
  rule(:block) { block_item.repeat.as(:block_items) }
80
106
 
81
107
  root :block
@@ -55,6 +55,17 @@ module Handlebars
55
55
  end
56
56
  end
57
57
 
58
+ class AsHelper < TreeItem.new(:name, :parameters, :as_parameters, :block, :else_block)
59
+ def _eval(context)
60
+ helper = context.get_as_helper(name.to_s)
61
+ if helper.nil?
62
+ context.get_helper('helperMissing').apply(context, String.new(name.to_s))
63
+ else
64
+ helper.apply_as(context, parameters, as_parameters, block, else_block)
65
+ end
66
+ end
67
+ end
68
+
58
69
  class EscapedHelper < Helper
59
70
  def _eval(context)
60
71
  context.escaper.escape(super(context).to_s)
@@ -133,6 +144,25 @@ module Handlebars
133
144
  Tree::Helper.new(name, parameters, block_items, else_block_items)
134
145
  }
135
146
 
147
+ rule(
148
+ helper_name: simple(:name),
149
+ parameters: subtree(:parameters),
150
+ as_parameters: subtree(:as_parameters),
151
+ block_items: subtree(:block_items),
152
+ ) {
153
+ Tree::AsHelper.new(name, parameters, as_parameters, block_items)
154
+ }
155
+
156
+ rule(
157
+ helper_name: simple(:name),
158
+ parameters: subtree(:parameters),
159
+ as_parameters: subtree(:as_parameters),
160
+ block_items: subtree(:block_items),
161
+ else_block_items: subtree(:else_block_items)
162
+ ) {
163
+ Tree::AsHelper.new(name, parameters, as_parameters, block_items, else_block_items)
164
+ }
165
+
136
166
  rule(partial_name: simple(:partial_name)) {Tree::Partial.new(partial_name)}
137
167
  rule(block_items: subtree(:block_items)) {Tree::Block.new(block_items)}
138
168
  rule(else_block_items: subtree(:else_block_items)) {Tree::Block.new(block_items)}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-handlebars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Pretre
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-08-30 00:00:00.000000000 Z
12
+ date: 2019-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parslet