code-docs/utils/nunjucks.md
Nunjucks template engine wrapper with Lowdefy-specific filters.
Provides template rendering with:
import { nunjucksString, nunjucksFunction } from '@lowdefy/nunjucks';
Render template from string (one-off, slower):
const result = nunjucksString('Hello {{ name }}!', { name: 'World' });
// 'Hello World!'
Compile and memoize template function (faster for repeated use):
const template = nunjucksFunction('Hello {{ name }}!');
template({ name: 'Alice' }); // 'Hello Alice!'
template({ name: 'Bob' }); // 'Hello Bob!'
Performance Note: Templates are cached by string content, so the same template string will reuse the compiled function.
Validate template syntax:
// Returns boolean
validNunjucksString('{{ name }}'); // true
validNunjucksString('{{ name'); // false
// Return error object
const result = validNunjucksString('{{ name', true);
// { valid: false, error: SyntaxError }
Access the configured Nunjucks environment:
import { nunjucksEnv } from '@lowdefy/nunjucks';
// Add custom filter
nunjucksEnv.addFilter('uppercase', (str) => str.toUpperCase());
Format dates using Day.js:
// Default ISO format
{
{
date | date;
}
}
// '2024-01-15T10:30:00.000Z'
// Custom format
{
{
date | date('YYYY-MM-DD');
}
}
// '2024-01-15'
// Relative time
{
{
date | date('fromNow');
}
}
// '2 hours ago'
// With method chaining
{
{
date | date('add', 1, 'days') | date('YYYY-MM-DD');
}
}
// '2024-01-16'
Supported Day.js Methods:
format(string) - Format datefromNow() - Relative timeadd(n, unit) - Add timesubtract(n, unit) - Subtract timestartOf(unit) - Start of periodendOf(unit) - End of periodGet unique values from array:
{
{
[1, 2, 2, 3, 3, 3] | unique;
}
}
// [1, 2, 3]
{
{
items | unique;
}
}
Parse or format URL query strings:
// Parse string to object
{{ 'page=1&filter=active' | urlQuery }}
// { page: '1', filter: 'active' }
// Format object to string
{{ { page: 1, filter: 'active' } | urlQuery }}
// 'page=1&filter=active'
Standard Nunjucks syntax:
{# Comments #}
{{ variable }}
{{ object.property }}
{{ array[0] }}
{% if condition %}
content
{% elif otherCondition %}
other content
{% else %}
fallback
{% endif %}
{% for item in items %}
{{ item.name }}
{% endfor %}
{{ value | filter }}
{{ value | filter(arg1, arg2) }}
nunjucks (3.2.4)dayjs (1.11.x)@lowdefy/helpers (4.4.0)import { nunjucksString } from '@lowdefy/nunjucks';
const label = nunjucksString('Showing {{ count }} of {{ total }} items', { count: 10, total: 100 });
// 'Showing 10 of 100 items'
import { nunjucksString } from '@lowdefy/nunjucks';
const formatted = nunjucksString('Created: {{ date | date("MMMM D, YYYY") }}', {
date: new Date('2024-01-15'),
});
// 'Created: January 15, 2024'
import { nunjucksString } from '@lowdefy/nunjucks';
const message = nunjucksString(
`
{% if items.length > 0 %}
{{ items.length }} items found
{% else %}
No items found
{% endif %}
`,
{ items: ['a', 'b', 'c'] }
);
// '3 items found'
import { nunjucksFunction } from '@lowdefy/nunjucks';
const emailTemplate = nunjucksFunction(`
Dear {{ name }},
Thank you for your order #{{ orderId }}.
Total: ${{ total | round(2) }}
{% for item in items %}
- {{ item.name }}: ${{ item.price }}
{% endfor %}
`);
const email1 = emailTemplate({
name: 'Alice',
orderId: '12345',
total: 99.99,
items: [{ name: 'Widget', price: 49.99 }]
});
import { validNunjucksString } from '@lowdefy/nunjucks';
function validateTemplate(template) {
const result = validNunjucksString(template, true);
if (!result.valid) {
throw new Error(`Invalid template: ${result.error.message}`);
}
return true;
}
Uses webpack with SWC loader for bundling. Output is CommonJS module.
| File | Purpose |
|---|---|
src/nunjucksString.js | One-off rendering |
src/nunjucksFunction.js | Compiled template function |
src/validNunjucksString.js | Syntax validation |
src/nunjucksEnv.js | Environment configuration |
src/filters/dateFilter.js | Date formatting filter |
src/filters/uniqueFilter.js | Array unique filter |
src/filters/urlQueryFilter.js | URL query filter |