Handlebars Templates

Item:OSW6df03625b42e4b44bd9f2dfa77387887
Handlebars Templates [OSW6df03625b42e4b44bd9f2dfa77387887]
ID OSW6df03625b42e4b44bd9f2dfa77387887
UUID 6df03625-b42e-4b44-bd9f-2dfa77387887
Label Handlebars Templates
Machine compatible name HandlebarsTemplates
Ontology equivalents
Statements (outgoing)
Statements (incoming)
Keywords

Description

No description found
Item
Type(s)/Category(s) Tutorial
CreativeWork
Article
Tutorial
Prerequisites (required) JSON Tutorial
Prerequisites (optional)
Follow-up (recommended)

View as slide show

Helpers

Compare operator

// register compare operator 
// e.g. {{#when <operand1> 'eq' <operand2>}} {{/when}}
// {{#when var1 'eq' var2}}equal{{else when var1 'gt' var2}}gt{{else}}lt{{/when}}
Handlebars.registerHelper("when", (operand_1, operator, operand_2, options) => {
	let operators = {
		'eq': (l, r) => l == r,
		'==': (l, r) => l == r,
		'===': (l, r) => l === r,
		'noteq': (l, r) => l != r,
		'!=': (l, r) => l != r,
		'!==': (l, r) => l !== r,
		'gt': (l, r) => (+l) > (+r),
		'>': (l, r) => (+l) > (+r),
		'gteq': (l, r) => ((+l) > (+r)) || (l == r),
		'>=': (l, r) => ((+l) > (+r)) || (l == r),
		'lt': (l, r) => (+l) < (+r),
		'<': (l, r) => (+l) < (+r),
		'lteq': (l, r) => ((+l) < (+r)) || (l == r),
		'<=': (l, r) => ((+l) < (+r)) || (l == r),
		'or': (l, r) => l || r,
		'||': (l, r) => l || r,
		'and': (l, r) => l && r,
		'&&': (l, r) => l && r,
		'mod': (l, r) => (l % r) === 0,
		'%': (l, r) => (l % r) === 0
	};
	let result = operators[operator](operand_1, operand_2);
	if (result) return options.fn(this);
	return options.inverse(this);
});

Replace operator

// register replace operator 
// e. g. {{#replace <find> <replace>}}{{string}}{{/replace}}
Handlebars.registerHelper('replace', function( find, replace, options) {
	let string = options.fn(this);
	return string.replaceAll( find, replace );
});

Split operator    

// register split operator 	
// {{#split <find> <index>}}<string>{{/split}}
// e. g. {{#split "/" -1}}https://test.com/target{{/split}} => target
Handlebars.registerHelper('split', function( find, index, options) {
	let string = options.fn(this);
	let result = string.split( find );
  	if (index < 0) return result[result.length + index];
    else return result[index];
});

Split interator

// register split interator
// {{#each_split <string> <find>}}...{{/each_split}}
// e. g. {{#each_split "https://test.com/target" "/"}}{{.}},{{/each_split}} => https:,,test.com,target, 
Handlebars.registerHelper('each_split', function( string, find, options) {
  	let data = string.split(find);
  	let result = '';
	data.forEach((item) => {
		result += options.fn(item);
	});
	return result;
});

Substring operator

// register substring operator
// {{#substring start end}}<string>{{/substring}}
// e. g. {{#substring 0 4}}My-test-string{{/substring}} => My-t
// e. g. {{#substring -2 ""}}My-test-string{{/substring}} => ng
// e. g. {{#substring 0 -2}}My-test-string{{/substring}} => My-test-stri
Handlebars.registerHelper('substring', function( start, end, options) {
	let string = options.fn(this);
	let result = "";
  	if (end === "") result = string.slice( start);
  	else result = string.slice( start, end );
	return result;
});
jsondata
type
"Category:OSW494f660e6a714a1a9681c517bbb975da"
uuid"6df03625-b42e-4b44-bd9f-2dfa77387887"
name"HandlebarsTemplates"
label
text"Handlebars Templates"
lang"en"
required_predecessor
"Item:OSWf1df064239044b8fa3c968339fb93344"