Рендирайте шаблони от текст, файлове и URL със TEMPLATE
TEMPLATE(source, model, [$]) рендира Tangular шаблон и връща promise с текста. source може да бъде:
- текст, който съдържа
{{ ... }}: той е самият шаблон - името на файл от папката
templates:TEMPLATE('invoice', model)четеtemplates/invoice.html(~в началото прави името абсолютен път) - адрес
http://илиhttps://, който изходният код наtotal5изтегля (не е проверено на тази страница)
Ta е друго име на Tangular. За разлика от Tangular.render(), моделът е достъпен като value: пишете {{ value.name }}. Файлът на шаблона може да носи и два блока, които TEMPLATE чете и премахва от изхода: <script>, който добавя помощници към Thelpers, и <script type="text/json"> с модела по подразбиране, който се използва, когато не е подаден модел.
<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'));
}