
Web Template Engine - Neutral TS
Neutral TS is a safe, modular, language-agnostic template engine built in Rust. It works as a native Rust library or via IPC for other languages like Python and PHP. With Neutral TS you can reuse the same template across multiple languages with consistent results.
For non-Rust requires an IPC server that you can download from the IPC repository - IPC server. Alternatively in Python you can use PYPI Package
The documentation of the web template engine is here: template engine doc and Rust documentation here: rust doc.
Template Engine - Features
It allows you to create templates compatible with any system and any programming language.
- Safe
- Language-agnostic
- Modular
- Parameterizable
- Efficient
- Inheritance
- Cache modular and !cache
- Objects
- JS fetch
- Parse files
- Embed files
- Localization
- Debug
- Loops: for and each
- Snippets
- Nesting, grouping and wrapping
- Redirections: HTTP y JavaScript
- Exit with error: 403, 404, 503, ...
- Comments
How it works
Neutral TS template engine offers two main ways to integrate with other programming languages:
-
In Rust: You can use Neutral TS template engine as a library by downloading the crate.
-
In other programming languages: Inter-Process Communication (IPC) is necessary, similar to how databases like MariaDB work.
Imagine a database. It has a server, and different programming languages access the data through a client. This means that if you run a "SELECT ..." query from any programming language, the result will always be the same.
Similarly, Neutral TS has an IPC server, and each programming language has a client. No matter where you run the template, the result will always be the same.
Thanks to this, and to its modular and parameterizable design, it is possible to create utilities or plugins that will work everywhere. For example, you can develop tools to create forms or form fields and create your own libraries of "snippets" for repetitive tasks.
Localization
Neutral TS template engine provides powerful and easy-to-use translation utilities... define the translation in a JSON:
"locale":
Now you can use:
{:trans; Hello :}
Actually you can always use "trans" because if there is no translation it returns the text. See: locale and trans.
Bif layout (Build-in function)
.-- open bif
| .-- bif name
| | .-- name separator
| | | .-- params
| | | | .-- params/code separator
| | | | | .-- code
| | | | | | .-- close bif
| | | | | | |
v v v v v v v
-- ----- - -------- -- ----- --
{:snippet; snipname >> ... :}
------------------------------
^ -------------------
| ^
| |
| `-- source
`-- Build-in function
Bif example: (See: syntax)
{:filled; varname >>
Hello!
:}
Neutral TS template engine is based on Bifs with block structure, we call the set of nested Bifs of the same level a block:
.-- {:coalesce;
| {:code;
| {:code; ... :}
| {:code; ... :}
Block --> | {:code; ... :}
| :}
| {:code;
| {:code; ... :}
| :}
`-- :}
{:coalesce;
.------ {:code;
| {:code; ... :}
Block --> | {:code; ... :}
| {:code; ... :}
`------ :}
.------ {:code;
Block --> | {:code; ... :}
`------ :}
:}
Short circuit at block level, if varname is not defined, the following ">>" is not evaluated:
{:defined; varname >>
{:code;
{:code;
...
:}
:}
:}
By design all Bifs can be nested and there can be a Bif anywhere in another Bif except in the name.
Data
The data is defined in a JSON:
"data":
And they are displayed with the bif {:; ... :} (var)
Simple variable:
{:;hello:}
Arrays with the "->" operator
{:;array->hello:}
Snippets
Snippet is a tool that can be used in a similar way to a function, it defines a snippet:
{:snippet; name >>
Any content here, including other snippet.
:}
From then on you can invoke it like this:
{:snippet; name :}
See: snippet.
Cache
The cache is modular, allowing only parts of the template to be included in the cache:
Template engine cache
{:cache; /120/ >>
{:code; ... :}
:}
{:date; %H:%M:%S :}
{:cache; /120/ >>
{:code; ... :}
:}
Or exclude parts of the cache, the previous example would be much better like this:
{:cache; /120/ >>
Template engine cache
{:code; ... :}
{:!cache;
{:date; %H:%M:%S :}
:}
{:code; ... :}
:}
Fetch
Neutral TS template engine provides a basic JavaScript to perform simple fetch requests:
Template engine
{:fetch; "/form-login" >>
Loading...
:}
See: fetch.
Object
obj allows you to execute scripts in other languages like Python
{:obj;
{
"engine": "Python",
"file": "script.py",
"template": "template.ntpl"
}
:}
See: obj.
Debug
Display debug information
{:debug; data->varname :}
See: debug.
Web template - example
{:*
comment
*:}
{:locale; locale.json :}
{:include; theme-snippets.ntpl :}
{:trans; Site title :}
{:snippet; current-theme:head :}
{:snippet; current-theme:body_begin :}
{:snippet; current-theme:body-content :}
{:snippet; current-theme:body-footer :}
Usage
You need two things, a template file and a json schema:
{
"config": {
"comments": "remove",
"cache_prefix": "neutral-cache",
"cache_dir": "",
"cache_on_post": false,
"cache_on_get": true,
"cache_on_cookies": true,
"cache_disable": false,
"filter_all": false,
"disable_js": false
},
"inherit": {
"locale": {
"current": "en",
"trans": {
"en": {
"Hello nts": "Hello",
"ref:greeting-nts": "Hello"
},
"es": {
"Hello nts": "Hola",
"ref:greeting-nts": "Hola"
},
"de": {
"Hello nts": "Hallo",
"ref:greeting-nts": "Hallo"
},
"fr": {
"Hello nts": "Bonjour",
"ref:greeting-nts": "Bonjour"
},
"el": {
"Hello nts": "Γεια σας",
"ref:greeting-nts": "Γεια σας"
}
}
}
},
"data": {
"CONTEXT": {
"ROUTE": "",
"HOST": "",
"GET": {},
"POST": {},
"HEADERS": {},
"FILES": {},
"COOKIES": {},
"SESSION": {},
"ENV": {}
},
"site_name": "MySite",
"site": {
"name": "MySite",
}
}
}
Template file.ntpl:
{:;site_name:}
Or for array:
{:;site->name:}
Native use (Rust)
use neutralts::Template;
use serde_json::json;
let template = Template::from_file_value("file.ntpl", schema).unwrap();
let content = template.render();
// e.g.: 200
let status_code = template.get_status_code();
// e.g.: OK
let status_text = template.get_status_text();
// empty if no error
let status_param = template.get_status_param();
// act accordingly at this point according to your framework
Rust examples
Python - Package
pip install neutraltemplate
from neutraltemplate import NeutralTemplate
template = NeutralTemplate("file.ntpl", schema)
contents = template.render()
# e.g.: 200
status_code = template.get_status_code()
# e.g.: OK
status_text = template.get_status_text()
# empty if no error
status_param = template.get_status_param()
# act accordingly at this point according to your framework
Python - IPC
Requires an IPC server that you can download from the repository, and an IPC client that you can download here: IPC client
from NeutralIpcTemplate import NeutralIpcTemplate
template = NeutralIpcTemplate("file.ntpl", schema)
contents = template.render()
# e.g.: 200
status_code = template.get_status_code()
# e.g.: OK
status_text = template.get_status_text()
# empty if no error
status_param = template.get_status_param()
# act accordingly at this point according to your framework
Python examples
PHP
Requires an IPC server that you can download from the repository, and an IPC client that you can download here: IPC client
include 'NeutralIpcTemplate.php';
$template = new NeutralIpcTemplate("file.ntpl", $schema);
$contents = $template->render();
// e.g.: 200
$status_code = $template->get_status_code();
// e.g.: OK
$status_text = $template->get_status_text();
// empty if no error
$status_param = $template->get_status_param();
// act accordingly at this point according to your framework
PHP examples
Links
Neutral TS template engine.