-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Replace token defined in args in HTML template | ||
* The tokens must be provided in form { token1name: 'token1Value', token2Name: 'token2Value' ...} | ||
* | ||
* @param {*} filename | ||
* @param {...{}} args | ||
* @returns {*} | ||
*/ | ||
function tokensInTemplate(filename, ...args) { | ||
// Ensure the first argument is a string | ||
if (typeof filename !== 'string') { | ||
throw new Error('First argument must be a string; be sure to write the template name correctly, without ending .html'); | ||
} | ||
|
||
// Merge all objects into one | ||
let replacements = Object.assign({}, ...args); | ||
|
||
// Create a template from a file | ||
let template = HtmlService.createTemplateFromFile(filename); | ||
|
||
// Replace tokens in the template with replacements | ||
for (let key in replacements) { | ||
template[key] = replacements[key]; | ||
} | ||
|
||
// Evaluate the template and return the result | ||
return template.evaluate().getContent(); | ||
} |