New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

thunder

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thunder

A lightning fast template parser for node.js

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29
increased by38.1%
Maintainers
1
Weekly downloads
 
Created
Source

thunder

A lightning fast template parser for node.js

Description

thunder is one of the fastest template parsers for node.js. Checkout the benchmarks for its performance. The usage is quite simple, evaluation, interpolation, and interpolation with html escaping. All variables and functions must start with it for performance sake. thunder works well with Express, check out the examples folder for the setup.

Installation

via npm:

$ npm install thunder

Syntax

Evaluation

Evaluate javascript expression

<? ?>

<? if( it.user ){ ?>
  <p>User exist</p>
<? } ?>

Interpolation

Simple output ( no escape )

<?= ?>

// script = '<script>alert( 'this is harmful' );</script>';
<?= it.script ?>
// prints out <script>alert( 'this is harmful' );</script>

Interpolation with html escaping

Simple output ( escape ) | & < > " --> &amp; &lt; &gt; &quot;

<?- ?>

// script = '<script>alert( 'this is gonna be fine' );</script>';
<?- it.script ?>
// prints out &lt;script&gt;alert( 'this is gonna be fine' );&lt;script/&gt;

Usage

Require the module before using

var thunder = require( 'thunder' );

thunder.compiled_text( input, options );

returns the text ready to be compiled for the compile function

Arguments

input

type: String
desc: Input string to be compiled

options:

type: Object
props:
  compress:
    type: Boolean
    default: false
    desc: Whether to compress the output HTML
Example code
var input         = '<div>Hello, this is <?= it.name ?> :)</div>';
var compiled_text = thunder.compiled_text( input );

console.log( compiled_text );
// var __t__='<div>Hello, this is ';__t__+= it.name ;__t__+=' :)</div>';return __t__;

thunder.compile( input, options );

returns the compiled function

Arguments

input

type: String
desc: Input string to be compiled

options

type: Object
props:
  compress:
    type: Boolean
    default: false
    desc: Whether to compress the output HTML
Example code
var input  = '<div>Hello, this is <?= it.name ?> :)</div>';
var render = thunder.compile( input );

// it actually turns to the following function
// function ( locals ){
//   var __t__='<div>Hello, this is ';__t__+= locals.name ;__t__+=' :)</div>';return __t__;
// };

thunder.cached( input, options );

returns the cached compiled function

Arguments

input

type: String
desc: Input string to be compiled

options

type: Object
props:
  compress:
    type: Boolean
    default: false
    desc: Whether to compress the output HTML
Example code
var input  = '<div>Hello, this is <?= it.name ?> :)</div>';
var render = thunder.cached( input );

// it actually turns to the following function and will be cached
// so that next time the text does not need to be compiled again
// function ( locals ){
//   var __t__='<div>Hello, this is ';__t__+= locals.name ;__t__+=' :)</div>';return __t__;
// };

thunder.render( input, locals, options );

returns the output

Arguments

input

type: String
desc: Input string to be compiled

locals

type: Object
desc: Variables to be passed to the compiled function

options

type: Object
props:
  compress:
    type: Boolean
    default: false
    desc: Whether to compress the output HTML
  cached:
    type: Boolean
    default: false
    desc: Whether to cache the compiled function
Example code
var input   = '<div>Hello, this is <?= it.name ?> :)</div>',
var locals  = { name : 'Bibi' };

var options = {
  cached : true,
  compress : true
};

var output  = thunder.render( input, locals, options );

console.log( output );
// <div>Hello, this is Bibi :)</div>

Express

app.configure( function(){
  ...
  app.set( 'view engine', 'html' );
  app.register( '.html', require( 'thunder' ));
  // optional
  app.set( 'view options', {
    compress : true
  });
  ...
});

To use express partial, helper and dynamic helper just call the method but start with it.

// partial
<?= it.partial( 'common/_nav' ) ?>

// helper
<a class="<?= it.selected( 'somewhere', it.nav_selected )?>" href="/somewhere">Somewhere</a>

Examples

Checkout the examples folder for more details.

simple

$ cd /path/to/thunder/examples/simple
$ node run.js

complex

$ cd /path/to/thunder/examples/complex
$ node run.js

express

$ cd /path/to/thunder/examples/express
$ npm install -lf
$ node app.js

Benchmarks

The followings are some well-known template parsers that I took for Benchmarks. You are welcome to fork it and add more. There are 2 main parts, the compiling speed and the rendering speed. The compiled templates are cached in jqtpl, Swig and thunder. Therefore their benchmarks for compiling is much faster. You can change the compile method from cached to compile to see the none-cached speed for thunder.

To run the benchmarks just type the following commands in the terminal

$ git clone git://github.com/dreamerslab/thunder.git
$ cd thunder/benchmarks/
$ npm install -lf
$ node run.js
  • doT
  • EJS
  • haml-js
  • Haml.js
  • Jade
  • jqtpl
  • jst
  • nTenjin
  • Swig
  • thunder

License

(The MIT License)

Copyright (c) 2011 dreamerslab <ben@dreamerslab.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 20 Jun 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc