-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from creature/add-with-support
Basic support for the "with" template helper
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require_relative 'default_helper' | ||
|
||
module Handlebars | ||
module Helpers | ||
class WithHelper < DefaultHelper | ||
def self.registry_name | ||
'with' | ||
end | ||
|
||
def self.apply(context, data, block, else_block) | ||
if data | ||
context.with_temporary_context(data) do | ||
block.fn(context) | ||
end | ||
else | ||
else_block.fn(context) | ||
end | ||
end | ||
|
||
def self.apply_as(context, data, name, block, else_block) | ||
self.apply(context, { name.to_sym => data }, block, else_block) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require_relative '../../spec_helper' | ||
require_relative './shared' | ||
|
||
require_relative '../../../lib/ruby-handlebars' | ||
require_relative '../../../lib/ruby-handlebars/tree' | ||
require_relative '../../../lib/ruby-handlebars/helpers/with_helper' | ||
|
||
describe Handlebars::Helpers::WithHelper do | ||
let(:subject) { Handlebars::Helpers::WithHelper } | ||
let(:hbs) { Handlebars::Handlebars.new } | ||
|
||
it_behaves_like "a registerable helper", "with" | ||
|
||
context '.apply' do | ||
include_context "shared apply helper" | ||
end | ||
|
||
context "integration" do | ||
include_context "shared helpers integration tests" | ||
|
||
let(:person_data) { { person: { firstname: "Yehuda", lastname: "Katz" }} } | ||
let(:city_data) { { | ||
city: { | ||
name: "San Francisco", | ||
summary: "San Francisco is the <b>cultural center</b> of <b>Northern California</b>", | ||
location: { | ||
north: "37.73,", | ||
east: -122.44, | ||
}, | ||
population: 883305, | ||
}, | ||
} } | ||
|
||
it "changes the evaluation context" do | ||
template = <<~HANDLEBARS | ||
{{#with person}} | ||
{{firstname}} {{lastname}} | ||
{{/with}} | ||
HANDLEBARS | ||
|
||
expect(evaluate(template, person_data).strip).to eq("Yehuda Katz") | ||
end | ||
|
||
it "supports block parameters" do | ||
template = <<~HANDLEBARS | ||
{{#with city as | city |}} | ||
{{#with city.location as | loc |}} | ||
{{city.name}}: {{loc.north}} {{loc.east}} | ||
{{/with}} | ||
{{/with}} | ||
HANDLEBARS | ||
|
||
expect(evaluate(template, city_data).strip).to eq("San Francisco: 37.73, -122.44") | ||
end | ||
|
||
it "supports else blocks" do | ||
template = <<~HANDLEBARS | ||
{{#with city}} | ||
{{city.name}} (not shown because there is no city) | ||
{{else}} | ||
No city found | ||
{{/with}} | ||
HANDLEBARS | ||
|
||
expect(evaluate(template, person_data).strip).to eq("No city found") | ||
end | ||
|
||
it "supports relative paths", skip: "Relative paths are not yet supported" do | ||
template = <<~HANDLEBARS | ||
{{#with city as | city |}} | ||
{{#with city.location as | loc |}} | ||
{{city.name}}: {{../population}} | ||
{{/with}} | ||
{{/with}} | ||
HANDLEBARS | ||
|
||
expect(evaluate(template, city_data).strip).to eq("San Francisco: 883305") | ||
end | ||
end | ||
end |