Skip to content

Instantly share code, notes, and snippets.

@dadhi
Last active May 14, 2021 13:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadhi/109b84ecb7dffe29f9bc to your computer and use it in GitHub Desktop.
Save dadhi/109b84ecb7dffe29f9bc to your computer and use it in GitHub Desktop.
Token-replacement template engine for PowerShell
<#
Token-replacement template engine for PowerShell courtesy to http://www.bricelam.net/2012/09/simple-template-engine-for-powershell.html
Usage:
Merge-Tokens 'Hello, $target$! My name is $self$.' @{ target = 'World'; self = 'Brice' }
#>
function Merge-Tokens($template, $tokens)
{
return [regex]::Replace($template, '\$(?<token>\w+)\$',
{ param($match) $tokens[$match.Groups['token'].Value] })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment