Fixing $ in replacement string issue

Missed a second place that replace was called.
Adding a function to take in a layout and a body to be used in the
replacement.
Had to add the function so jshint wouldn't yell about creating a
function inside a loop.

Fixes #286