Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic support for the "with" template helper #28

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ruby-handlebars/helpers/register_default_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_relative 'helper_missing_helper'
require_relative 'if_helper'
require_relative 'unless_helper'
require_relative 'with_helper'

module Handlebars
module Helpers
Expand All @@ -10,6 +11,7 @@ def self.register_default_helpers(hbs)
HelperMissingHelper.register(hbs)
IfHelper.register(hbs)
UnlessHelper.register(hbs)
WithHelper.register(hbs)
end
end
end
25 changes: 25 additions & 0 deletions lib/ruby-handlebars/helpers/with_helper.rb
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
80 changes: 80 additions & 0 deletions spec/ruby-handlebars/helpers/with_helper_spec.rb
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