Renderize templates a partir de texto, ficheiros e URLs com TEMPLATE
TEMPLATE(source, model, [$]) renderiza um template Tangular e devolve uma promessa com o texto. O source pode ser:
- um texto que contém
{{ ... }}: é o próprio template - o nome de um ficheiro da pasta
templates:TEMPLATE('invoice', model)lêtemplates/invoice.html(um~no início faz do nome um caminho absoluto) - um endereço
http://ouhttps://, que o código detotal5descarrega (não verificado nesta página)
Ta é outro nome de Tangular. Ao contrário de Tangular.render(), o modelo está disponível como value: escreva {{ value.name }}. Um ficheiro de template também pode levar dois blocos que TEMPLATE lê e remove da saída: um <script> que acrescenta helpers a Thelpers e um <script type="text/json"> com o modelo por omissão, usado quando não é passado nenhum modelo.
<script>
Thelpers.money = function(value) {
return (value || 0).toFixed(2) + ' EUR';
};
</script>
<script type="text/json">{ "customer": "Anonymous", "total": 0 }</script>
<h1>Invoice for {{ value.customer }}</h1>
<p>Total: {{ value.total | money() }}</p>exports.install = function() {
ROUTE('GET /text/', text);
ROUTE('GET /file/', file);
ROUTE('GET /default/', withdefault);
ROUTE('GET /absolute/', absolute);
ROUTE('GET /alias/', alias);
};
async function text($) {
$.plain(await TEMPLATE('Hello {{ value.name }}!', { name: $.query.name || 'world' }));
}
async function file($) {
$.plain(await TEMPLATE('invoice', { customer: 'Ana', total: 12.5 }));
}
function alias($) {
$.json({ same: Ta === Tangular, rendered: Ta.render('{{ name }}', { name: 'x' }) });
}
async function absolute($) {
const file = require('path').join(__dirname, '..', 'templates', 'invoice.html');
$.plain(await TEMPLATE('~' + file, { customer: 'Eva', total: 1 }));
}
async function withdefault($) {
$.plain(await TEMPLATE('invoice'));
}