https://github.com/dmytro-krekota/jlto
Nodejs-based tool for optimizing Jinja like templates.
https://github.com/dmytro-krekota/jlto
jinja jinja2 jinja2-templates jinjava liquid nodejs nunjucks nunjucks-templates optimization twig twig-templates
Last synced: 3 months ago
JSON representation
Nodejs-based tool for optimizing Jinja like templates.
- Host: GitHub
- URL: https://github.com/dmytro-krekota/jlto
- Owner: dmytro-krekota
- License: mit
- Created: 2017-04-16T22:39:34.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-09T02:48:11.000Z (over 2 years ago)
- Last Synced: 2025-01-30T19:11:45.281Z (3 months ago)
- Topics: jinja, jinja2, jinja2-templates, jinjava, liquid, nodejs, nunjucks, nunjucks-templates, optimization, twig, twig-templates
- Language: JavaScript
- Homepage:
- Size: 850 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Support Ukraine πΊπ¦
- Via United24 platform (the initiative of the President of Ukraine):
- [One click donation (credit card, bank transfer or crypto)](https://u24.gov.ua/)
- Via National Bank of Ukraine:
- [Ukrainian army](https://bank.gov.ua/en/about/support-the-armed-forces)
- [Humanitarian aid to Ukraine](https://bank.gov.ua/en/about/humanitarian-aid-to-ukraine)[#StandWithUkraine](https://twitter.com/hashtag/StandWithUkraine)
# JLTO
[](https://app.circleci.com/pipelines/github/dmytro-krekota/jlto)
[](https://coveralls.io/github/dmytro-krekota/jlto?branch=master) [](https://gitter.im/dmytro-krekota_jlto/community?source=orgpage)[](https://nodei.co/npm/jlto/)
> Jinja Like Templates Optimizer (JLTO) is a Nodejs-based tool for optimizing Jinja like templates.
**Gulp tool for JLTO:**
[gulp-jlto](https://www.npmjs.com/package/gulp-jlto)
**Supported template engines:**
- [Nunjucks](https://mozilla.github.io/nunjucks/) (Tested with unit tests)
- [Twig.js](https://github.com/twigjs/twig.js) (Tested with unit tests)
- [LiquidNode](https://github.com/sirlantis/liquid-node) (Tested with unit tests)
- [Twig](https://twig.sensiolabs.org/)
- [Jinja](http://jinja.pocoo.org/)
- [Django](https://docs.djangoproject.com/en/1.11/ref/templates/language/)
- [Liquid](https://shopify.github.io/liquid/)
- [Jinjava](https://github.com/HubSpot/jinjava)**Available options:**
- expressionStart - symbols at the beginning of expressions
- expressionEnd - symbols at the end of expressions
- blockStart - symbols at the beginning of blocks
- blockEnd - symbols at the end of blocks
- commentStart - symbols at the beginning of comments
- commentEnd - symbols at the beginning of comments
- specialChars - special chars in blocks and expressions
- cleanupBlocks - flag for optimize blocks
- cleanupExpressions - flag for optimize expressions
- removeComments - flag for removing comments
- minifyHtml - flag for minifying html code with [html-minifier](https://www.npmjs.com/package/html-minifier)
- minifyHtmlOptions - options for html-minifierSee default values for above options [here](https://github.com/dmytro-krekota/jlto/blob/master/lib/core/default.js).
## Usage
**Simple example:**
```js
let jlto = require('jlto');
let template = `
{{ hello }}
{{ " ?" | escape }}
{{ '2.7' | round }}{% if product %}Product exists.{% endif %}
`;
let optimizedTemplate = jlto.optimizeString(template);
// optimizedTemplate:
// `
//{{hello}}
//{{" ?"|escape}}
//{{'2.7'|round}}{%if product%}Product exists.{%endif%}
// `
```**Example of using minifyHtml option:**
```js
let jlto = require('jlto');
let template = ``;
{% for writable in writables %}
{{ writable | write | raw }}
{% endfor %}
let optimizedTemplate = jlto.optimizeString(template, {minifyHtml: true});
// optimizedTemplate:
// ``{%for writable in writables%} {{writable|write|raw}} {%endfor%}
```**Example of "nunjucks" templates minification with the custom GruntJS task:**
```js
module.exports = (grunt) => {
grunt.registerTask('min-nunjucks', 'Min nunjucks templates', () => {
let jlto = require('jlto');
let fs = require('fs');
let glob = require('glob');
let done = this.async();
glob('./**/*.nunjucks.html', (error, files) => {
files.forEach((filePath) => {
let fileContent;
fileContent = fs.readFileSync(filePath).toString();
try {
fileContent = jlto.optimizeString(fileContent, {minifyHtml: true});
fs.writeFileSync(filePath, fileContent);
} catch (ignored) {}
});
return done();
});
});
};
```## Tests
Unit tests are written using Mocha and Chai. To run, invoke `npm test`.
## License
JLTO is available under the [MIT license](https://opensource.org/licenses/MIT), see the LICENSE file for more information.