Skip to content

Commit cc4500a

Browse files
authored
Merge pull request #319 from matestack/add_template_component
add template tag to core components
2 parents 736de7a + c1eed0d commit cc4500a

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%template{@tag_attributes}
2+
- if block_given?
3+
= yield
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Matestack::Ui::Core::Template
2+
class Template < Matestack::Ui::Core::Component::Static
3+
end
4+
end

docs/components/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ You can build your [own components](/docs/extend/README.md) as well, both static
7878
- th
7979
- tr
8080
- td
81+
- [template](/docs/components/template.md)
8182
- [u](/docs/components/u.md)
8283
- [var](/docs/components/var.md)
8384
- [video](/docs/components/video.md)

docs/components/template.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# matestack core component: Template
2+
3+
Show [specs](/spec/usage/components/template_spec.rb)
4+
5+
The HTML `<template>` tag implemented in ruby.
6+
7+
## Parameters
8+
9+
This component can take 2 optional configuration params and yield content, either directly or through partials/custom components.
10+
11+
#### # id (optional)
12+
Expects a string with all ids the `<template>` should have.
13+
14+
#### # class (optional)
15+
Expects a string with all classes the `<template>` should have.
16+
17+
## Example 1: Yield other components inside a template
18+
19+
```ruby
20+
template id: 'foo', class: 'bar' do
21+
paragraph text: 'Template example 1' # optional content
22+
end
23+
```
24+
25+
returns
26+
27+
```html
28+
<template id="foo" class="bar">
29+
<p>Template example 1</p>
30+
</template>
31+
```
32+
33+
## Example 2: Render a given block, e.g. a partial
34+
35+
```ruby
36+
template id: 'foo', class: 'bar' do
37+
partial :example_content
38+
end
39+
40+
def example_content
41+
partial {
42+
paragraph text: 'I am part of a partial'
43+
}
44+
end
45+
```
46+
47+
returns
48+
49+
```html
50+
<template id="foo" class="bar">
51+
<p>I am part of a partial</p>
52+
</template>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require_relative '../../support/utils'
2+
include Utils
3+
4+
describe 'Template component', type: :feature, js: true do
5+
it 'Renders a simple and enhanced template tag on a page' do
6+
class ExamplePage < Matestack::Ui::Page
7+
def response
8+
components {
9+
# Simple template
10+
template id: 'foo', class: 'bar' do
11+
paragraph text: 'Template example 1'
12+
end
13+
14+
# Enhanced template
15+
template id: 'foobar', class: 'bar' do
16+
partial :example_content
17+
end
18+
}
19+
end
20+
21+
def example_content
22+
partial {
23+
paragraph text: 'I am part of a partial'
24+
}
25+
end
26+
end
27+
28+
visit '/example'
29+
30+
static_output = page.html
31+
32+
expected_static_output = <<~HTML
33+
<p>Template example 1</p>
34+
<p>I am part of a partial</p>
35+
HTML
36+
# Below is the original output, but capybara/rspec does not recognize <template>-tags
37+
# <template id="foo" class="bar">
38+
# <p>Template example 1</p>
39+
# </template>
40+
# <template id="foobar" class="bar">
41+
# <p>I am part of a partial</p>
42+
# </template>
43+
44+
expect(stripped(static_output)).to include(stripped(expected_static_output))
45+
end
46+
end

0 commit comments

Comments
 (0)