Skip to content

Commit

Permalink
Merge pull request #28 from creature/add-with-support
Browse files Browse the repository at this point in the history
Basic support for the "with" template helper
  • Loading branch information
vincent-psarga authored Jan 11, 2021
2 parents 1e38db5 + a05c02b commit e730124
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
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

0 comments on commit e730124

Please sign in to comment.