diff --git a/index.js b/index.js index cbca76c..50d0eac 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,14 @@ 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); diff --git a/template.js b/template.js index fc5d35f..7ab99e2 100644 --- a/template.js +++ b/template.js @@ -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 }