Skip to content

Commit

Permalink
Merge pull request #2 from barelyhuman/issue-1
Browse files Browse the repository at this point in the history
fix: implement nested templates
  • Loading branch information
barelyhuman committed Oct 24, 2023
2 parents a427489 + cd9ee24 commit d2fca0b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,22 @@ function interpolateExpressions(htmlString, expressionInstance) {

if (
typeof watcherReturn !== 'object' &&
typeof watcherReturn !== 'function'
typeof watcherReturn !== 'function' &&
!Array.isArray(watcherReturn)
) {
return htmlString.replace(delimiterComment, watcherReturn)
}

if (Array.isArray(watcherReturn)) {
const result = watcherReturn.map(x => {
if ('isT' in x) {
return renderToString(x)
}
return x
})
return result.join('')
}

if (watcherReturn && watcherReturn.isT) {
const _nestedHtmlString = renderToString(watcherReturn)
return htmlString.replace(delimiterComment, _nestedHtmlString)
Expand Down
25 changes: 25 additions & 0 deletions tests/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,29 @@ test('function composition basic', async () => {
await inlineSnapshot(out, `<p>hello world</p>`)
})

test('stringify nested array of templates', async () => {
const tables = [1, 2, 3]

const templ = html`
<div class="container">
<div class="row">
<div class="col">${() => tables.map(x => html`<p>${x}</p>`)}</div>
</div>
</div>
`

const out = renderToString(templ)

await inlineSnapshot(
out,
`
<div class="container">
<div class="row">
<div class="col"><p>1</p><p>2</p><p>3</p></div>
</div>
</div>
`
)
})

test.run()

0 comments on commit d2fca0b

Please sign in to comment.