template testing

by John Allan

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>

JavaScript

var t, template, regex, matches, test, pass, jsMatch, fieldMatch;

test = ["{{#if baz}}", "{{/if}}", "{{bar}}", "{{foo}}"];

template = '{{#if baz}}<div class="js-test">{{{foo}}} {{LocalizeResource \'PAGE_TITLE\'}}</div><div>{{bar}}</div><input name="lastName"><input name=\'firstName\'><button class="js-testMore other">test</button>';

//token
tokens = template.match(/{{2,3}.+?}{2,3}/g).sort();

//fieldNames
fieldRegex = /(?:name=["']{1})(.+?)(?:["']{1})/g;
fieldNames = [];
while(fieldMatch = fieldRegex.exec(template)) {
	fieldNames.push(fieldMatch[1]);
}
fieldNames.sort();

//js- classes
jsRegex = /(?:class=["']{1}.*?)(js-[^\s"']+)(?:.*?["']{1})/g;
jsClasses = [];
while (jsMatch = jsRegex.exec(template)) {
	jsClasses.push(jsMatch[1]);
}
jsClasses.sort();

console.log(tokens);
console.log(fieldNames);
console.log(jsClasses);

t = Handlebars.compile(template);
html = t();