Comparing version 0.0.2 to 0.1.0
@@ -0,4 +1,13 @@ | ||
# History | ||
## 0.1.0 / 2012-07-08 | ||
- [new feature] Client-side support including `precompile` & `watch` | ||
- [update packages] Update testing packages to the latest version | ||
## 0.0.2 / 2012-02-15 | ||
- [refactoring] Fix typo | ||
- [refactoring] Fix typo | ||
@@ -9,2 +18,2 @@ | ||
- Initial release | ||
- Initial release |
/*! | ||
* thunder | ||
* Copyright(c) 2011 dreamerslab <ben@dreamerslab.com> | ||
* Copyright(c) 2012 dreamerslab <ben@dreamerslab.com> | ||
* MIT Licensed | ||
* | ||
* @fileoverview | ||
* A lighting fast template parser for node.js. | ||
* A lightning fast JavaScript template engine. | ||
*/ | ||
var cache = {}; | ||
// split `escape` into another obj speeds up the `no escape` parsing | ||
var escape = { | ||
;( function ( root ){ | ||
var cache = {}; | ||
rules : { | ||
'&' : '&', | ||
'<' : '<', | ||
'>' : '>', | ||
'"' : '"' | ||
}, | ||
// split `escape` into another obj speeds up the `no escape` parsing | ||
var escape = { | ||
fn : function ( input ){ | ||
return typeof( input ) != 'string' ? | ||
input : input.replace( /[&<>"]/g, function ( match ){ | ||
return escape.rules[ match ]; | ||
}); | ||
} | ||
}; | ||
rules : { | ||
'&' : '&', | ||
'<' : '<', | ||
'>' : '>', | ||
'"' : '"' | ||
}, | ||
fn : function ( input ){ | ||
return typeof( input ) != 'string' ? | ||
input : input.replace( /[&<>"]/g, function ( match ){ | ||
return escape.rules[ match ]; | ||
}); | ||
} | ||
}; | ||
var thunder = { | ||
module.exports = { | ||
// for client-side | ||
html_to_text : function ( input ){ | ||
return input. | ||
replace( /\"/g, '\\\"' ). | ||
replace( /\n/g, '\\n\\\n' ); | ||
}, | ||
version : '0.0.1', | ||
compiled_text : function ( input, options ){ | ||
var arr = ( options && options.compress === true ? | ||
// compress | ||
input.replace( /\n\r\t|\s+/g, ' ' ) : | ||
// chage the new line to unix version | ||
input.replace( /\n\r|\r/g, '\n' )). | ||
split( '<?' ).join( '?>\x1b' ).split( '?>' ); | ||
compiled_text : function ( input, options ){ | ||
var arr = ( options && options.compress === true ? | ||
// compress | ||
input.replace( /\n\r\t|\s+/g, ' ' ) : | ||
// chage the new line to unix version | ||
input.replace( /\n\r|\r/g, '\n' )). | ||
split( '<?' ).join( '?>\x1b' ).split( '?>' ); | ||
var str = ''; | ||
var i = 0; | ||
var j = arr.length; | ||
var tmp; | ||
var str = ''; | ||
var i = 0; | ||
var j = arr.length; | ||
var tmp; | ||
// string concat is faster than array `push` | ||
for(; i < j; i++ ){ | ||
tmp = arr[ i ]; | ||
str += tmp.charAt( 0 ) != '\x1b' ? | ||
// `\t` (tab) is ok, we need to handle with `\n` (line feed) | ||
"__t__+='" + tmp.replace( /\'|\\/g, '\\$&' ).replace( /\n/g, '\\n\\\n' ) + "'" : | ||
( tmp.charAt( 1 ) == '=' ? | ||
';__t__+=' + tmp.substr( 2 ) + ';' : | ||
( tmp.charAt( 1 ) == '-' ? | ||
';__t__+=e(' + tmp.substr( 2 ) + ");" : | ||
';' + tmp.substr( 1 ))); | ||
} | ||
// string concat is faster than array `push` | ||
for(; i < j; i++ ){ | ||
tmp = arr[ i ]; | ||
str += tmp.charAt( 0 ) != '\x1b' ? | ||
// `\t` (tab) is ok, we need to handle with `\n` (line feed) | ||
"__t__+='" + tmp.replace( /\'|\\/g, '\\$&' ).replace( /\n/g, '\\n\\\n' ) + "'" : | ||
( tmp.charAt( 1 ) == '=' ? | ||
';__t__+=' + tmp.substr( 2 ) + ';' : | ||
( tmp.charAt( 1 ) == '-' ? | ||
';__t__+=e(' + tmp.substr( 2 ) + ");" : | ||
';' + tmp.substr( 1 ))); | ||
} | ||
// `replace` is faster than `split` -> `join` | ||
return ( 'var __t__="";' + str + ';return __t__;' ). | ||
replace( /__t__\+\=\'\'\;/g, '' ). | ||
replace( /var __t__\=\"\"\;__t__\+\=/g, 'var __t__=' ); | ||
}, | ||
// `replace` is faster than `split` -> `join` | ||
return ( 'var __t__="";' + str + ';return __t__;' ). | ||
replace( /__t__\+\=\'\'\;/g, '' ). | ||
replace( /var __t__\=\"\"\;__t__\+\=/g, 'var __t__=' ); | ||
}, | ||
compile : function ( input, options ){ | ||
var str = this.compiled_text( input, options ); | ||
var fn; | ||
compile : function ( input, options ){ | ||
var str = this.compiled_text( input, options ); | ||
var fn; | ||
try{ | ||
// must save this new Function to fn, | ||
// do not just invoke in the return function, it's slow. | ||
// ex. return new Function( 'it', 'e', str )( locals, escape.fn ); <-- never do that | ||
fn = new Function( 'it', 'e', str ); | ||
}catch( err ){ | ||
console.log( '[thunder] Having trouble with creating a template function: \n' + str ); | ||
throw err; | ||
} | ||
try{ | ||
// must save this new Function to fn, | ||
// do not just invoke in the return function, it's slow. | ||
// ex. return new Function( 'it', 'e', str )( locals, escape.fn ); <-- never do that | ||
fn = new Function( 'it', 'e', str ); | ||
}catch( err ){ | ||
console.log( '[thunder] Having trouble with creating a template function: \n' + str ); | ||
throw err; | ||
} | ||
return function ( locals ){ | ||
return fn( locals, escape.fn ); | ||
}; | ||
}, | ||
return function ( locals ){ | ||
return fn( locals, escape.fn ); | ||
}; | ||
}, | ||
cached : function ( input, options ){ | ||
if( !cache[ input ]){ | ||
cache[ input ] = this.compile( input, options ); | ||
} | ||
cached : function ( input, options ){ | ||
if( !cache[ input ]){ | ||
cache[ input ] = this.compile( input, options ); | ||
return cache[ input ]; | ||
}, | ||
render : function ( input, locals, options ){ | ||
var method = options && options.cached === true ? | ||
'cached' : 'compile'; | ||
return this[ method ]( input, options )( locals ); | ||
} | ||
}; | ||
return cache[ input ]; | ||
}, | ||
// browser support | ||
if( typeof exports === 'undefined' ) return root.call( thunder ); | ||
render : function ( input, locals, options ){ | ||
var method = options && options.cached === true ? | ||
'cached' : 'compile'; | ||
// requirejs | ||
if( typeof define !== 'undefined' ){ | ||
return define( function ( require, exports, module ){ | ||
module.exports = thunder; | ||
}); | ||
} | ||
return this[ method ]( input, options )( locals ); | ||
} | ||
}; | ||
/** | ||
* @public | ||
*/ | ||
thunder.version = JSON.parse( | ||
require( 'fs' ).readFileSync( __dirname + '/../package.json', 'utf8' ) | ||
).version; | ||
/** | ||
* Exports module. | ||
*/ | ||
module.exports = thunder; | ||
})( this ); |
{ | ||
"name" : "thunder", | ||
"version" : "0.0.2", | ||
"description": "A lightning fast template parser for node.js", | ||
"version" : "0.1.0", | ||
"description": "One of the fastest JavaScript template engine for Node.js and browsers.", | ||
"bin" : { | ||
"thunder": "./bin/thunder" | ||
}, | ||
"keywords" : [ | ||
@@ -10,3 +13,8 @@ "template", "view", "view paser", "paser", "html", "html parser", | ||
"author" : "dreamerslab <ben@dreamerslab.com>", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"cli-color": "0.1.7", | ||
"mkdirp" : "0.3.3", | ||
"rmdir" : "0.0.3", | ||
"watchr" : "2.1.2" | ||
}, | ||
"repository" : { | ||
@@ -13,0 +21,0 @@ "type": "git", |
188
Readme.md
# thunder | ||
A lightning fast template parser for node.js | ||
A lightning fast JavaScript template engine. | ||
@@ -9,14 +9,6 @@ | ||
**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. | ||
**thunder** is one of the fastest JavaScript template engine for Node.js and browsers. 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 | ||
@@ -26,3 +18,3 @@ | ||
Evaluate javascript expression | ||
Evaluate JavaScript expression | ||
@@ -57,11 +49,143 @@ > `<? ?>` | ||
## Installation | ||
### With node.js | ||
> via npm: | ||
// in the project root | ||
$ npm install thunder | ||
### In the browser | ||
> Install `thunder` in global to use the command line tools. You will need them to compile the HTML templates to JavaScript strings. By default `thunder` looks for all the `.html` files in the dir `templates` in the same dir where the command is called, compile them to JavaScript strings and save them to `views` dir. You can specify the `input` and `output` dir to wherever you want by passing the `-i` and `-o` arguments. Also the default compiled strings are `requirejs` modules. By passing `-r=false` it will use the file name as the template name and attach it to `window` object. | ||
// make sure you have `node.js` installed | ||
$ npm intall thunder -g | ||
$ thunder | ||
Usage: thunder [command] [argument(s)] | ||
Commands: | ||
-v, --version Display coke version | ||
h, help Display usage information | ||
b, build [args] Precompile templates | ||
w, watch [args] Run coke server | ||
Arguments for `build` & `watch commands` | ||
-i, --input=/new/input/dir Default: ./templates | ||
-o, --output=/new/output/dir Default: ./views | ||
-r, --requirejs=false Default: true | ||
> Copy `thunder.min.js` to your public JavaScript dir. | ||
## Usage | ||
> Require the module before using | ||
For advance usages please checkout the API block. | ||
var thunder = require( 'thunder' ); | ||
### With node.js | ||
var thunder = require( 'thunder' ); | ||
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> | ||
### In the browser | ||
- Normal useage | ||
<!----> | ||
// Include necessary JS files | ||
<script src="/js/lib/thunder.min.js"></script> | ||
<script src="/js/views/index.js"></script> | ||
<script> | ||
var input = index, | ||
var locals = { name : 'Bibi' }; | ||
var options = { cached : true }; | ||
var output = thunder.render( input, locals, options ); | ||
console.log( output ); | ||
// <div>Hello, this is Bibi :)</div> | ||
</script> | ||
- With `requirejs` | ||
<!----> | ||
<script src="/js/lib/require.js" data-main="/js/main.js"></script> | ||
<script> | ||
requirejs.config({ | ||
baseUrl : '/js/lib', | ||
paths : { views : '../views' } | ||
}); | ||
</script> | ||
<script> | ||
define( function ( require, exports, module ){ | ||
var thunder = require( 'thunder' ); | ||
var input = require( 'views/index' ), | ||
var locals = { name : 'Bibi' }; | ||
var options = { cached : true }; | ||
var output = thunder.render( input, locals, options ); | ||
console.log( output ); | ||
// <div>Hello, this is Bibi :)</div> | ||
}); | ||
</script> | ||
## API | ||
### thunder.html_to_text( input ); | ||
Compile the HTML templates to JavaScript strings. It is only used for client-side templates. | ||
#### Arguments | ||
> input | ||
type: String | ||
desc: Input string to be compiled | ||
### thunder.compiled_text( input, options ); | ||
returns the text ready to be compiled for the `compile` function | ||
Returns the text ready to be compiled for the `compile` function. | ||
@@ -94,3 +218,3 @@ #### Arguments | ||
returns the compiled function | ||
Returns the compiled function. | ||
@@ -125,3 +249,3 @@ #### Arguments | ||
returns the cached compiled function | ||
Returns the cached compiled function. | ||
@@ -157,3 +281,3 @@ #### Arguments | ||
returns the output | ||
Returns the output. | ||
@@ -187,11 +311,11 @@ #### Arguments | ||
var input = '<div>Hello, this is <?= it.name ?> :)</div>', | ||
var locals = { name : 'Bibi' }; | ||
var input = '<div>Hello, this is <?= it.name ?> :)</div>', | ||
var locals = { name : 'Bibi' }; | ||
var options = { | ||
cached : true, | ||
cached : true, | ||
compress : true | ||
}; | ||
var output = thunder.render( input, locals, options ); | ||
var output = thunder.render( input, locals, options ); | ||
@@ -203,24 +327,2 @@ console.log( output ); | ||
## 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 | ||
@@ -227,0 +329,0 @@ |
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23792
10
301
390
4
3
1
3
3
+ Addedcli-color@0.1.7
+ Addedmkdirp@0.3.3
+ Addedrmdir@0.0.3
+ Addedwatchr@2.1.2
+ Addedbal-util@1.11.2(transitive)
+ Addedcli-color@0.1.7(transitive)
+ Addedes5-ext@0.8.2(transitive)
+ Addedmkdirp@0.3.3(transitive)
+ Addedrmdir@0.0.3(transitive)
+ Addedwatchr@2.1.2(transitive)