Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Last active August 19, 2017 03:51
Show Gist options
  • Select an option

  • Save jongacnik/84e8d19894f2f737d243c0308133056b to your computer and use it in GitHub Desktop.

Select an option

Save jongacnik/84e8d19894f2f737d243c0308133056b to your computer and use it in GitHub Desktop.
Tiny templating for the v basic needs! #template @classnic
function tinyBars (str, data) {
var regex = /{{\s*([\w\.]+)\s*}}/gi
return str.replace(regex, function (match, val) {
return data[val.trim()] || ''
})
}
// or
var tinyBars = (s, d) => s.replace(/{{\s*([\w]+)\s*}}/gi, (m, v) => d[v] || '')
var myString = `
# Tiny Title
## Table of Contents
{{ contents }}
`
var contents = ['One', 'Two', 'Three'].map(section => `- ${section}`).join('\n')
var doc = tinyBars(myString, { contents: contents })
// # Tiny Title
//
// ## Table of Contents
//
// - One
// - Two
// - Three
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment