{{= variavel }}
{{= variavel.atributo }}
{{= variavel["meu-atributo"] }}
{{= variavel.toLowerCase() }}
{{= escape(variavel) }}
{{= variavel.meu-atributo }}
usar no lugar {{= variavel["meu-atributo"] }}
/** | |
* Simples Lib de template para substituir apenas variáveis. Útil para montar URL | |
* de Webservices com parâmetros variáveis. | |
* | |
* @param {String} template | |
* @param {Json} obj Objetos que serão adicionados no template | |
* @returns {String} | |
*/ | |
function maker(template, obj) { | |
var attr; | |
var to_match = new RegExp('\{\{=\\W*([^}]+)\}\}', 'g'); | |
template = template.replace(to_match, function(match, code) { | |
code = code.replace(/(\w+\-\w+)/, function(x, attr) { | |
if( obj.hasOwnProperty(attr) === true ) { | |
return "data['"+attr+"']"; | |
} | |
return attr; | |
}); | |
return new Function("data", "code", "with(data){return eval(code);}")(obj, code); | |
}); | |
return template; | |
} |