Answer the question
In order to leave comments, you need to log in
How to insert a variable here?
request = new Request("SELECT * FROM [driving license] WHERE name_driver='Олег'", function (err, rowCount, rows) {
Answer the question
In order to leave comments, you need to log in
var text = "SELECT * FROM [driving license] WHERE name_driver={{user.name}}";
templater(text, {
user: {
name: 'Олег'
}
});
//> "SELECT * FROM [driving license] WHERE name_driver=Олег"
var text = "SELECT * FROM [driving license] WHERE name_driver={{name}}";
templater(text, {
name: 'Олег'
});
//> "SELECT * FROM [driving license] WHERE name_driver=Олег"
/**
* Обработка тегов, шаблонизатор
* @param {String} Входные данные
* @param {Object} Объект {тег - значение}
* @returns {String} Выходные, обработаные данные
**/
function templater(input, data) {
if (!['string', 'object'].includes(typeof input)) {
return input;
}
for (var key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
if (typeof data[key] === 'object' && data[key] !== null && !Array.isArray(data[key])) {
var nested = {};
for (var name in data[key]) {
nested[key + '.' + name] = data[key][name];
}
input = templater(input, nested);
} else {
input = input.replace(new RegExp('\{\{' + key + '\}\}', 'g'), data[key]);
}
}
}
return input;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question