basic if true/false template

main
Matthew Huntington 11 months ago
parent 9dc097d52a
commit 65d392d5da

@ -1,9 +1,14 @@
import watch from './watch.js' import watch from './watch.js'
import { renderVariables } from './template.js' import { renderVariables, renderConditionals } from './template.js'
const template = `hi {{name}}` const template = `
hi
{{#if false}}
matt
{{#endif}}
`
const result = renderVariables(template, { name:'matt'}) const result = renderConditionals(template, { name:'matt'})
console.log(result); console.log(result);

@ -4,4 +4,13 @@ const renderVariables = (template, data) => {
}); });
} }
export { renderVariables } const renderConditionals = (template, data) => {
return template.replace(/\{\{#if (\w+)\}\}([\s\S]*?)\{\{#endif\}\}/g, (match, condition, innerTemplate) => {
if(eval(condition)){
return innerTemplate
} else {
return ''
}
});
}
export { renderVariables, renderConditionals }

Loading…
Cancel
Save