Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created January 15, 2013 21:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisdickinson/208737f8609bb121a0b1 to your computer and use it in GitHub Desktop.
Save chrisdickinson/208737f8609bb121a0b1 to your computer and use it in GitHub Desktop.

Project 1

A Tiny Templating Language

Write a templating function that transforms strings like:

hello {{ world }} how is your {{ weekday }} going?

into a function that takes an object and returns a rendered string like so:

var tpl = your_function("hello {{ world }} how is your {{ weekday }} going?")
tpl({
  world: 'dude'
, weekday: 'tuesday'
})  // == "hello dude how is your tuesday going?"

You'll need:

  • RegExp literals /\{\{\s*(\w+)\s*\}\}/
  • String slice, replace
  • To understand how to create lists of functions.

Put your code into a file called index.js and export the template function.

You'll need to write tests: use the assert module to make sure your code works. Put this code in a file called test.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment