Skip to content

Instantly share code, notes, and snippets.

@tharzeez
Created September 23, 2020 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tharzeez/c4da0b24d31bbf8eda6dba309ce3fd18 to your computer and use it in GitHub Desktop.
Save tharzeez/c4da0b24d31bbf8eda6dba309ce3fd18 to your computer and use it in GitHub Desktop.
Simple html interpolation for react native
/**
* Html interpolation for react native.
* Since lodash template wasnt supported in some devices.
* /{{.*?}}/ regex for Lazy quantifiers (non greedy stop at first occurence)
*/
let interpolationValues = {name: 'Tharzeez', workspace: 'my workspace'}
let interpolatedString = "Hello {{ name }}, welcome back to your {{ workspace }}".replace(/{{.*?}}/g, (char) => {
/**
* char contains the matched occurence i.e {{ name }} for the first time and {{ workspace }} for the second time.
* splits returns array and join with empty space, removing the braces.
*/
let path = char.split('{').join('').split('}').join('');
let value = _.get(interpolationValues, _.trim(path));
return value;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment