function render (template, options) {
return template.replace(/\{\{\s?(\w+)\s?\}\}/g, (match, variable) => {
return options[variable] || ''
})
}
describe('#render', () => {
const template = '<h1>{{ the_title }}</h1><h2>{{ the_subtitle }}</h2>'
const data = {
the_title: 'hello, world!',
the_subtitle: 'an experiment in javascript'
}
const expected = '<h1>hello, world!</h1><h2>an experiment in javascript</h2>'
const empty_expected = '<h1></h1><h2></h2>'
it('interpolates data into the template', () => {
expect(render(template, data).indexOf(data.the_title)).toBe(4)
})
it('interpolates multiple strings into the template', () => {
expect(render(template, data)).toBe(expected)
})
it('interpolates nothing if the property is not in the options hash', () => {
expect(render(template, {})).toBe(empty_expected)
})
})
View Compiled