Last active
August 19, 2017 03:51
-
-
Save jongacnik/84e8d19894f2f737d243c0308133056b to your computer and use it in GitHub Desktop.
Tiny templating for the v basic needs! #template @classnic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] || '') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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