Skip to content

Commit 1cc3d49

Browse files
committed
Add support for rendering with block instead of partial
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.
1 parent a02e065 commit 1cc3d49

File tree

7 files changed

+31
-2
lines changed

7 files changed

+31
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./no_template.scss";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.no-template {
2+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module NoTemplateComponent
2+
extend ComponentHelper
3+
4+
render do |block|
5+
content_tag :div, class: "no-template #{@additional_class}" do
6+
block.call
7+
end
8+
end
9+
end

lib/komponent/component_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ def property(name, options = {})
1010
def self.extended(component)
1111
component.properties = {}
1212
end
13+
14+
def render(&block)
15+
@render_block = block
16+
end
17+
18+
attr_reader :render_block
1319
end

lib/komponent/component_renderer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ def _render(component, locals = {}, options = {}, &block)
7676
define_singleton_method(:block_given_to_component) { block }
7777
end
7878

79-
@context.render("components/#{component}/#{parts.join('_')}", &block)
79+
if component_module.respond_to?(:render_block) && component_module.render_block
80+
@context.instance_exec(block, &component_module.render_block)
81+
else
82+
@context.render("components/#{component}/#{parts.join('_')}", &block)
83+
end
8084
end
8185

8286
def resolved_component_path(component)

test/komponent/component_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def test_all_returns_components
77
all = Komponent::Component.all
88

99
assert all.is_a?(Hash)
10-
assert_equal all.count, 9
10+
assert_equal all.count, 10
1111
assert all["foo"].is_a?(Komponent::Component)
1212
end
1313

test/komponent/komponent_helper_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def test_helper_lists_components
8686
'foo',
8787
'foo_bar',
8888
'hello',
89+
'no_template',
8990
'ping',
9091
'pong',
9192
'required',
@@ -104,4 +105,10 @@ def test_helper_renders_with_doc
104105
}</code></pre>),
105106
component_with_doc('all', world: "🌎", sunglasses: "😎").chomp
106107
end
108+
109+
def test_helper_renders_without_template
110+
assert_equal \
111+
%(<div class="no-template classy">🌎</div>),
112+
component('no_template', additional_class: "classy") { "🌎" }.chomp
113+
end
107114
end

0 commit comments

Comments
 (0)