Skip to content
6 changes: 6 additions & 0 deletions lib/ruby-handlebars/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Parser < Parslet::Parser
rule(:ocurly) { str('{')}
rule(:ccurly) { str('}')}
rule(:pipe) { str('|')}
rule(:eq) { str('=')}


rule(:docurly) { ocurly >> ocurly }
Expand Down Expand Up @@ -50,6 +51,9 @@ class Parser < Parslet::Parser
}
rule(:parameters) { parameter >> (space >> parameter).repeat }

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 }
rule(:safe_helper) { tocurly >> space? >> identifier.as(:safe_helper_name) >> (space? >> parameters.as(:parameters)).maybe >> space? >> tccurly }

Expand Down Expand Up @@ -98,6 +102,8 @@ class Parser < Parslet::Parser
space? >>
identifier.as(:partial_name) >>
space? >>
arguments.as(:arguments).maybe >>
space? >>
dccurly
}

Expand Down
16 changes: 16 additions & 0 deletions lib/ruby-handlebars/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ def _eval(context)
end
end

class PartialWithArgs < TreeItem.new(:partial_name, :arguments)
def _eval(context)
[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

class Block < TreeItem.new(:items)
def _eval(context)
items.map {|item| item._eval(context)}.join()
Expand Down Expand Up @@ -162,6 +171,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)}
Expand Down
21 changes: 21 additions & 0 deletions spec/handlebars_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ def evaluate(template, args = {})
hbs.register_partial('brackets', "[{{name}}]")
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]")
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
Expand Down