Skip to content

Instantly share code, notes, and snippets.

@niccai
Last active April 16, 2021 20:14
Show Gist options
  • Save niccai/8859372 to your computer and use it in GitHub Desktop.
Save niccai/8859372 to your computer and use it in GitHub Desktop.
Changing lodash/underscore template settings in Node.js to use Mustache style braces
// Client side, you typically see the template style changed to {{ }} like so...
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/gim,
evaluate: /\{\#(.+?)\#\}/gim
};
/*
However, in Node.js, this causes issues when trying to render a template.
Likely, you haven't paid too much attention to the fact that you are setting
_.templateSettings outright above. Thus, losing other settings like escape and variable.
AND FOR NODE.JS, IMPORTS IS ALSO LOST.
*/
// Therefore, you should set only what you need to explicitly.
_.templateSettings.interpolate = /\{\{(.+?)\}\}/gim;
_.templateSettings.evaluate = /\{\#(.+?)\#\}/gim;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment