Created
December 15, 2010 15:52
-
-
Save JamieMason/742126 to your computer and use it in GitHub Desktop.
Performs a find/replace on the contents of a generic view template. eg: A generic template should have quite reusable names which describe the content in respect to the view it's marking up, rather than the data that's being merged into it. Like ${heading
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
/** | |
* Performs a find/replace on the contents of a generic view template. eg: A generic template should have quite reusable names which describe the content in respect to the view it's marking up, rather than the data that's being merged into it. Like ${heading} rather than ${brandName} for example. The same view template might be used in different places with different data. | |
* | |
* @author https://github.com/JamieMason | |
* @param {String} templateContents The template to find/replace on, eg: '<div><strong>${boldText}</strong> ${regularText} (${sideNote})</div>' | |
* @return A new template with renamed tokens which match the properties of the model you plan to use it against | |
* @type String | |
*/ | |
function alignTemplate(templateContents, bindings) | |
{ | |
var matchingString = '', | |
openTokenMarkup = '${', | |
closeTokenMarkup = '}'; | |
// If bindings were passed, we'll align the generic names in the template to match the object's property names we'll be filling them with. | |
// eg: A generic template should have names like ${heading} rather than ${bingoGameName} | |
// This binding tells this instance what data goes where in the template | |
if (typeof bindings === 'object') | |
{ | |
// For every binding provided | |
for (var key in bindings) | |
{ | |
// Find the generic name in the template, eg: ${heading} | |
matchingString = [openTokenMarkup, key, closeTokenMarkup].join(''); | |
// And replace it with the data we'll fill it with in this instance, eg: ${bingoGameName} | |
templateContents = templateContents.split(matchingString).join([openTokenMarkup, bindings[key], closeTokenMarkup].join('')); | |
} | |
} | |
return templateContents; | |
} |
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
var gamesTickerTemplate = alignTemplate('<strong>${boldText}</strong> ${regularText} (${sideNote})', { boldText: 'gameName', regularText: 'gameSummary', sideNote: 'price' }); | |
// >>> '<strong>${gameName}</strong> ${gameSummary} (${gamePayout})' | |
// would create a template you could make subsequent calls with to https://gist.github.com/742024 | |
// Merge 1 object with a template | |
mergeTemplateWithData(gamesTickerTemplate, { | |
gameName: 'Chuck Norris Pinball', | |
gameSummary: 'The only Pinball game officially extreme enough for Chuck himself, includes roundhouse bonus round', | |
price: '£666' | |
}); | |
// >>> '<strong>Chuck Norris Pinball</strong> The only Pinball game officially extreme enough for Chuck himself, includes roundhouse bonus round (£666)' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment