From 0c4b6f5003fbd4650b4fc63090b0e470ca76e8d4 Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Thu, 17 Oct 2019 12:52:20 -0700 Subject: [PATCH 01/11] Add test spec to support passing args to partials --- spec/handlebars_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/handlebars_spec.rb b/spec/handlebars_spec.rb index f47dc3b..7b0c7a2 100644 --- a/spec/handlebars_spec.rb +++ b/spec/handlebars_spec.rb @@ -61,6 +61,11 @@ def evaluate(template, args = {}) hbs.register_partial('brackets', "[{{name}}]") expect(evaluate("Hello {{> brackets}}", {name: 'world'})).to eq("Hello [world]") end + + it 'with arguments' do + hbs.register_partial('with_args', "[{{fname}} {{lname}}]") + expect(evaluate("Hello {{> with_args fname='jon' lname='doe'}}")).to eq("Hello [jon doe]") + end end context 'helpers' do From b5c12754dab5fc8f6877209ec323902f1a045286 Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Thu, 17 Oct 2019 12:59:45 -0700 Subject: [PATCH 02/11] Add 3 rules and optional arguments for partials --- lib/ruby-handlebars/parser.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/ruby-handlebars/parser.rb b/lib/ruby-handlebars/parser.rb index 997534a..0f671d6 100644 --- a/lib/ruby-handlebars/parser.rb +++ b/lib/ruby-handlebars/parser.rb @@ -11,6 +11,7 @@ class Parser < Parslet::Parser rule(:ocurly) { str('{')} rule(:ccurly) { str('}')} rule(:pipe) { str('|')} + rule(:eq) { str('=')} rule(:docurly) { ocurly >> ocurly } @@ -50,6 +51,9 @@ class Parser < Parslet::Parser } rule(:parameters) { parameter >> (space >> parameter).repeat } + rule(:argument) { identifier.as(:key) >> space? >> eq >> space? >> string } + rule(:arguments) { argument >> (space >> argument).repeat } + rule(:unsafe_helper) { docurly >> space? >> identifier.as(:unsafe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> dccurly } rule(:safe_helper) { tocurly >> space? >> identifier.as(:safe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> tccurly } @@ -98,6 +102,8 @@ class Parser < Parslet::Parser space? >> identifier.as(:partial_name) >> space? >> + arguments.as(:arguments).maybe >> + space? >> dccurly } From faa3ccfb867ca49f8df4ae934e36ff0aa08c7de3 Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Thu, 17 Oct 2019 13:06:46 -0700 Subject: [PATCH 03/11] Add a rule and a TreeItem subclass for partials w/ args --- lib/ruby-handlebars/tree.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/ruby-handlebars/tree.rb b/lib/ruby-handlebars/tree.rb index 4a2c64c..dbdeb7c 100644 --- a/lib/ruby-handlebars/tree.rb +++ b/lib/ruby-handlebars/tree.rb @@ -78,6 +78,13 @@ def _eval(context) end end + class PartialWithArgs < TreeItem.new(:partial_name, :arguments) + def _eval(context) + hash = Hash[arguments.map { |arg| arg.values.map(&:to_s) }] + context.get_partial(partial_name.to_s).call hash + end + end + class Block < TreeItem.new(:items) def _eval(context) items.map {|item| item._eval(context)}.join() @@ -162,6 +169,13 @@ class Transform < Parslet::Transform ) { Tree::AsHelper.new(name, parameters, as_parameters, block_items, else_block_items) } + + rule( + partial_name: simple(:partial_name), + arguments: subtree(:arguments) + ) { + Tree::PartialWithArgs.new(partial_name, arguments) + } rule(partial_name: simple(:partial_name)) {Tree::Partial.new(partial_name)} rule(block_items: subtree(:block_items)) {Tree::Block.new(block_items)} From e77695dd07ce0adbd5eda372b8da12915cbef3f7 Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Sat, 19 Oct 2019 09:50:40 -0700 Subject: [PATCH 04/11] Add specs for a wider scope of partial arguments --- spec/handlebars_spec.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/spec/handlebars_spec.rb b/spec/handlebars_spec.rb index 7b0c7a2..27cacac 100644 --- a/spec/handlebars_spec.rb +++ b/spec/handlebars_spec.rb @@ -62,10 +62,21 @@ def evaluate(template, args = {}) expect(evaluate("Hello {{> brackets}}", {name: 'world'})).to eq("Hello [world]") end - it 'with arguments' do + it 'with string arguments' do hbs.register_partial('with_args', "[{{fname}} {{lname}}]") expect(evaluate("Hello {{> with_args fname='jon' lname='doe'}}")).to eq("Hello [jon doe]") end + + it 'with variables in arguments' do + hbs.register_partial('with_args', "[{{fname}} {{lname}}]") + expect(evaluate("Hello {{> with_args fname='jon' lname=last_name}}", {last_name: 'doe'})).to eq("Hello [jon doe]") + end + + it 'with a helper as an argument' do + hbs.register_helper('wrap_parens') {|context, value| "(#{value})"} + hbs.register_partial('with_args', "[{{fname}} {{lname}}]") + expect(evaluate("Hello {{> with_args fname='jon' lname=(wrap_parens 'doe')}}")).to eq("Hello [jon (doe)]") + end end context 'helpers' do From bce9447b8dc774ccffeb04c158610536fa06c767 Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Sat, 19 Oct 2019 09:52:29 -0700 Subject: [PATCH 05/11] Allow non-string values for partial args --- lib/ruby-handlebars/tree.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ruby-handlebars/tree.rb b/lib/ruby-handlebars/tree.rb index dbdeb7c..259819e 100644 --- a/lib/ruby-handlebars/tree.rb +++ b/lib/ruby-handlebars/tree.rb @@ -80,7 +80,9 @@ def _eval(context) class PartialWithArgs < TreeItem.new(:partial_name, :arguments) def _eval(context) - hash = Hash[arguments.map { |arg| arg.values.map(&:to_s) }] + hash = Hash[arguments.map(&:values).map do |vals| + [vals.first.to_s, vals.last._eval(context)] + end] context.get_partial(partial_name.to_s).call hash end end From bfe9e8d0c80760cab8e3a556be4835f075ff92b6 Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Sat, 19 Oct 2019 09:54:23 -0700 Subject: [PATCH 06/11] Replace string with parameter in partial args --- lib/ruby-handlebars/parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby-handlebars/parser.rb b/lib/ruby-handlebars/parser.rb index 0f671d6..de94c6c 100644 --- a/lib/ruby-handlebars/parser.rb +++ b/lib/ruby-handlebars/parser.rb @@ -51,7 +51,7 @@ class Parser < Parslet::Parser } rule(:parameters) { parameter >> (space >> parameter).repeat } - rule(:argument) { identifier.as(:key) >> space? >> eq >> space? >> string } + rule(:argument) { identifier.as(:key) >> space? >> eq >> space? >> parameter.as(:value) } rule(:arguments) { argument >> (space >> argument).repeat } rule(:unsafe_helper) { docurly >> space? >> identifier.as(:unsafe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> dccurly } From 4868f5b2834d0b38ea90bcc8b3c754250068200a Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Mon, 21 Oct 2019 01:04:11 -0700 Subject: [PATCH 07/11] Add spec to ensure arguments is an array --- spec/handlebars_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/handlebars_spec.rb b/spec/handlebars_spec.rb index 27cacac..fd55744 100644 --- a/spec/handlebars_spec.rb +++ b/spec/handlebars_spec.rb @@ -62,6 +62,11 @@ def evaluate(template, args = {}) expect(evaluate("Hello {{> brackets}}", {name: 'world'})).to eq("Hello [world]") end + it 'with a string argument' do + hbs.register_partial('with_args', "[{{name}}]") + expect(evaluate("Hello {{> with_args fname='jon'}}")).to eq("Hello [jon]") + end + it 'with string arguments' do hbs.register_partial('with_args', "[{{fname}} {{lname}}]") expect(evaluate("Hello {{> with_args fname='jon' lname='doe'}}")).to eq("Hello [jon doe]") From 552defd5e3dfc617fda3c2a06526f45515c522aa Mon Sep 17 00:00:00 2001 From: d316 Date: Mon, 21 Oct 2019 01:22:07 -0700 Subject: [PATCH 08/11] Revert "Add spec to ensure arguments is an array" This reverts commit 4868f5b2834d0b38ea90bcc8b3c754250068200a. --- spec/handlebars_spec.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/spec/handlebars_spec.rb b/spec/handlebars_spec.rb index fd55744..27cacac 100644 --- a/spec/handlebars_spec.rb +++ b/spec/handlebars_spec.rb @@ -62,11 +62,6 @@ def evaluate(template, args = {}) expect(evaluate("Hello {{> brackets}}", {name: 'world'})).to eq("Hello [world]") end - it 'with a string argument' do - hbs.register_partial('with_args', "[{{name}}]") - expect(evaluate("Hello {{> with_args fname='jon'}}")).to eq("Hello [jon]") - end - it 'with string arguments' do hbs.register_partial('with_args', "[{{fname}} {{lname}}]") expect(evaluate("Hello {{> with_args fname='jon' lname='doe'}}")).to eq("Hello [jon doe]") From 30d2d41a2eda10c09e15079e9cc7a283efa462a7 Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Mon, 21 Oct 2019 01:26:01 -0700 Subject: [PATCH 09/11] Ensure partial arguments are an array --- spec/handlebars_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/handlebars_spec.rb b/spec/handlebars_spec.rb index 27cacac..7044f53 100644 --- a/spec/handlebars_spec.rb +++ b/spec/handlebars_spec.rb @@ -62,6 +62,11 @@ def evaluate(template, args = {}) expect(evaluate("Hello {{> brackets}}", {name: 'world'})).to eq("Hello [world]") end + it 'with a string argument' do + hbs.register_partial('with_args', "[{{name}}]") + expect(evaluate("Hello {{> with_args name='jon'}}")).to eq("Hello [jon]") + end + it 'with string arguments' do hbs.register_partial('with_args', "[{{fname}} {{lname}}]") expect(evaluate("Hello {{> with_args fname='jon' lname='doe'}}")).to eq("Hello [jon doe]") From 36f087366a9b611072944baaeb63e183494fa73f Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Mon, 21 Oct 2019 01:29:57 -0700 Subject: [PATCH 10/11] Ensure partial arguments are an array --- lib/ruby-handlebars/tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby-handlebars/tree.rb b/lib/ruby-handlebars/tree.rb index 259819e..e57fa64 100644 --- a/lib/ruby-handlebars/tree.rb +++ b/lib/ruby-handlebars/tree.rb @@ -80,7 +80,7 @@ def _eval(context) class PartialWithArgs < TreeItem.new(:partial_name, :arguments) def _eval(context) - hash = Hash[arguments.map(&:values).map do |vals| + hash = Hash[[arguments].flatten.map(&:values).map do |vals| [vals.first.to_s, vals.last._eval(context)] end] context.get_partial(partial_name.to_s).call hash From 9ad312015aa76f959bd21ff87d5e82d90122299d Mon Sep 17 00:00:00 2001 From: d316 <56690148+d316@users.noreply.github.com> Date: Mon, 21 Oct 2019 01:50:01 -0700 Subject: [PATCH 11/11] Add partial args properly to context --- lib/ruby-handlebars/tree.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ruby-handlebars/tree.rb b/lib/ruby-handlebars/tree.rb index e57fa64..76628ad 100644 --- a/lib/ruby-handlebars/tree.rb +++ b/lib/ruby-handlebars/tree.rb @@ -80,10 +80,10 @@ def _eval(context) class PartialWithArgs < TreeItem.new(:partial_name, :arguments) def _eval(context) - hash = Hash[[arguments].flatten.map(&:values).map do |vals| - [vals.first.to_s, vals.last._eval(context)] - end] - context.get_partial(partial_name.to_s).call hash + [arguments].flatten.map(&:values).map do |vals| + context.add_item vals.first.to_s, vals.last._eval(context) + end + context.get_partial(partial_name.to_s).call end end