Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including jlto with all npm packages installed. Try it out:

var jlto = require("jlto")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

jlto v1.2.3

Nodejs-based tool for optimizing Jinja like templates

JLTO

Codeship Coverage Dependencies DevDependencies Join the chat at https://gitter.im/dmytro-krekota_jlto/Lobby

NPM

Jinja Like Templates Optimizer (JLTO) is a Nodejs-based tool for optimizing Jinja like templates.

Gulp tool for JLTO:

gulp-jlto

Supported template engines:

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
  • minifyHtmlOptions - options for html-minifier

See default values for above options here.

Usage

Simple example:

let jlto = require("jlto");
let template = `
{{ hello }}
{{   "<John   &   Paul> ?"     | escape   }}
{{ '2.7'   | round }}{%  if  product  %}Product exists.{%  endif  %}
`;
let optimizedTemplate = jlto.optimizeString(template);
// optimizedTemplate:
// `
//{{hello}}
//{{"<John   &   Paul> ?"|escape}}
//{{'2.7'|round}}{%if product%}Product exists.{%endif%}
// `

Example of using minifyHtml option:

let jlto = require("jlto");
let template = `
<div {% if id %}id="{{ id | escape('html_attr') }}"{% endif %} class="section-container {{ classes | join(' ') | html_attribute }}">
  <div class="section-writables">
    {% for writable in writables  %}
      {{ writable | write | raw }}
    {% endfor %}
  </div>
</div>`;
let optimizedTemplate = jlto.optimizeString(template, {minifyHtml: true});
// optimizedTemplate:
// `<div {%if id%} id="{{id|escape('html_attr')}}" {%endif%} class="section-container {{classes|join(' ')|html_attribute}}"><div class="section-writables"> {%for writable in writables%} {{writable|write|raw}} {%endfor%} </div></div>`

Example of "nunjucks" templates minification with the custom GruntJS task:

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, see the LICENSE file for more information.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free