Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. // My very own template engine :DD
  2.  
  3. function precompile (template) {
  4. return function (locals) {
  5. return template.replace(/\#\{([a-z0-9_]+)\}/g, function (_, key) {
  6. return locals[key];
  7. });
  8. }
  9. };
  10.  
  11. var template = [
  12. '<div class="head">',
  13. '<h1>#{title}</h1>',
  14. '<h2>#{subtitle}</h2>',
  15. '</div>'
  16. ].join('\n')
  17.  
  18. var tpl = precompile(template);
  19.  
  20. tpl({ title: 'hello', subtitle: 'world' });
  21.  
  22. /*
  23. "<div class="head">
  24. <h1>hello</h1>
  25. <h2>world</h2>
  26. </div>"
  27. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement