Skip to content

Instantly share code, notes, and snippets.

@isaaclyman
Last active August 29, 2015 14:27
Show Gist options
  • Save isaaclyman/ac96e24febf0ac19fafc to your computer and use it in GitHub Desktop.
Save isaaclyman/ac96e24febf0ac19fafc to your computer and use it in GitHub Desktop.
Using requireJS, jQuery and a unique template syntax, fill in text in an HTML document
'use strict';
// "source" should be the name of a text file (the '.txt' extension is assumed, so leave it off),
// formatted as follows:
/*
:css selector:
Text content for this element
::
:another css selector:
Text content for this other element
::
*/
define(['jquery'], function ($) {
function getDictionary (source) {
var dict = {};
source = source.split('::');
for (var index = 0; index < source.length; index++) {
var keyVal = source[index].trim();
var keyStart = keyVal.indexOf(':') + 1;
var keyEnd = keyVal.indexOf(':', keyStart);
var key = keyVal.substr(keyStart, keyEnd - keyStart).replace(/[\n\r]+/g, '');
var definition = keyVal.substr(keyEnd + 1).replace(/[\n\r]+/, '').replace(/[\n\r]+$/, '');
dict[key] = definition;
}
return dict;
}
return function (source) {
require(['text!' + source + '.txt'], function (source) {
var dict = getDictionary(source);
for (var selector in dict) {
$(selector).text(dict[selector]);
}
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment