Skip to content

Commit

Permalink
Add support for rendering with block instead of partial
Browse files Browse the repository at this point in the history
Add a `render` macro to the ComponentHelper, which takes a block that is
used to render the component instead of using the partial.

This is mainly useful for small components that will be rendered many
times, the overhead of rendering the partial can be quite significant.
  • Loading branch information
jonmast committed Jan 26, 2020
1 parent a02e065 commit 1cc3d49
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./no_template.scss";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.no-template {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module NoTemplateComponent
extend ComponentHelper

render do |block|
content_tag :div, class: "no-template #{@additional_class}" do
block.call
end
end
end
6 changes: 6 additions & 0 deletions lib/komponent/component_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ def property(name, options = {})
def self.extended(component)
component.properties = {}
end

def render(&block)
@render_block = block
end

attr_reader :render_block
end
6 changes: 5 additions & 1 deletion lib/komponent/component_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def _render(component, locals = {}, options = {}, &block)
define_singleton_method(:block_given_to_component) { block }
end

@context.render("components/#{component}/#{parts.join('_')}", &block)
if component_module.respond_to?(:render_block) && component_module.render_block
@context.instance_exec(block, &component_module.render_block)
else
@context.render("components/#{component}/#{parts.join('_')}", &block)
end
end

def resolved_component_path(component)
Expand Down
2 changes: 1 addition & 1 deletion test/komponent/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_all_returns_components
all = Komponent::Component.all

assert all.is_a?(Hash)
assert_equal all.count, 9
assert_equal all.count, 10
assert all["foo"].is_a?(Komponent::Component)
end

Expand Down
7 changes: 7 additions & 0 deletions test/komponent/komponent_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def test_helper_lists_components
'foo',
'foo_bar',
'hello',
'no_template',
'ping',
'pong',
'required',
Expand All @@ -104,4 +105,10 @@ def test_helper_renders_with_doc
}</code></pre>),
component_with_doc('all', world: "🌎", sunglasses: "😎").chomp
end

def test_helper_renders_without_template
assert_equal \
%(<div class="no-template classy">🌎</div>),
component('no_template', additional_class: "classy") { "🌎" }.chomp
end
end

0 comments on commit 1cc3d49

Please sign in to comment.