Atpl.js
A complete and fast template engine fully compatible with twig for node.js.
Install / Use
/learn @soywiz-archive/Atpl.jsREADME
Build status on Travis:
Install with NPM:
npm install atpl
Using with express3:
app.engine('html', require('atpl').__express);
app.set('devel', false);
app.set('view engine', 'html');
app.set('view cache', true);
app.set('views', __dirname + '/templates');
app.get('/simple', function(req, res) {
res.render('simple', { name : 'Test' });
});
Using as standalone:
var atpl = require('atpl');
console.log(atpl.renderFileSync(__dirname + '/views', 'simple.html', { name: 'MyName' }, false));
atpl.renderFile(__dirname + '/views', 'simple.html', { name: 'MyName' }, false), function(err, result) {
console.log(result);
});
Typescript typings: https://github.com/soywiz/atpl.js/blob/master/lib/atpl.d.ts
This project is designed to be compatible with twig. So the documentation about tags, filters, functions and tests is on the twig page:
- http://twig.sensiolabs.org/documentation
I have continued working on it because there are several projects implementing twig templates but they are not implemented very well and lacks stuff like multiple inheritance or nesting blocks, or are slow because doesn't perform dynamic recompilation.
This project will implement the full twig set and will be as fast as possible.
Supported syntax:
- Inheritance
- Conditional inheritance
- Include
- if+elseif+else
- for+else+loop scope variable
- Auto-escape
- Escape (|e) (|e('js')) (|e('css'))...
- Skip autoescape (|raw)
- Filters (all twig filters but 'convert_encoding' that is not required because javascript strings are unicode)
- Functions (all twig functions but 'constant')
- Tests (all twig tests but 'constant')
- Tags (all twig tags)
- value in array
- value in string
- set a, b = 'a', 'b'
- macro support (macro+import+from)
- sandbox
- use (horizontal reuse)
{% autoescape %}
{% autoescape 'type' %}
{% extends "file.atpl" %}
{% extends cond ? "base1" : "base2" %}
{% include "template" %}
{% include "template" with { 'foo' : 'bar' } only %}
{% block name %}...{% endblock %}
{% for var in list %}...{% endfor %}
{% for var in list %}...{% else %}...{% endfor %}
{% for key, value in list %}...{% else %}...{% endfor %}
{% for key, value in list if condition %}...{% else %}...{% endfor %}
{% for key in ['a', 'b', 'c'] %}{{ loop.index0 }}{% endfor %}
{% if condition %}...{% else %}...{% endif %}
{% if cond1 %}...{% elseif cond2 %}...{% else %}...{% endif %}
{% set a, b = 'a', 'b' %}
{{ expression }}
{{ expression|filter }}
{{ expression|filter(params) }}
{{ function(params) }}
{{ var is even }}
{{ var is not defined }}
{{ var is sameas(var) }}
{{ var.array['access'] }}
{{ 3 in [1, 2, 3, 4] }}
{{ {'key':'value','key2':'value2'} }}
Related Skills
node-connect
346.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
