Permalink
Browse files

Merge pull request #14 from abalandin/master

add "lodash" template engine; add --variable option.
  • Loading branch information...
Sam Breed
Sam Breed committed Jan 27, 2014
2 parents 21b9815 + 021042e commit d890c7ef1d2151f411ffd12352f87b62e629efc2
Showing with 34 additions and 5 deletions.
  1. +4 −1 bin/jst.js
  2. +22 −0 lib/engines/lodash.js
  3. +1 −0 package.json
  4. +7 −4 readme.md
View
@@ -8,7 +8,7 @@ var nopt = require("nopt")
, join = Path.join
, _ = require('underscore')
, engines = require('../lib/index')
- , allowedengine = ['string', 'underscore', '_', 'jquery-tmpl', 'handlebars', 'hbs', 'dust', 'hogan', 'mustache', 'mu']
+ , allowedengine = ['string', 'underscore', '_', 'lodash', 'jquery-tmpl', 'handlebars', 'hbs', 'dust', 'hogan', 'mustache', 'mu']
, knownOpts = { "template" : allowedengine
, "inputdir" : Path
, "output" : Path
@@ -17,6 +17,7 @@ var nopt = require("nopt")
, "include" : String
, "stdout" : Boolean
, "verbose" : Boolean
+ , "variable" : String
}
, description = { "template" : "format : " + allowedengine.join('|')
, "inputdir" : "directory containings the templates to compile"
@@ -26,6 +27,7 @@ var nopt = require("nopt")
, "include" : "Glob patterns for templates files to include in `inputdir`"
, "stdout" : "Print the result in stdout instead of writing in a file"
, "verbose" : "Print logs for debug"
+ , "variable" : "Set templateSettings.variable"
}
, defaults = { "inputdir" : process.cwd()
, "watch" : false
@@ -42,6 +44,7 @@ var nopt = require("nopt")
, "I" : ["--include"]
, "s" : ["--stdout"]
, "v" : ["--verbose"]
+ , "V" : ["--variable"]
}
, options = nopt(knownOpts, shortHands, process.argv, 2)
, inputdir;
View
@@ -0,0 +1,22 @@
+var jst = require('../index.js')
+ , _ = require('lodash');
+
+// Returns a string of js with the compiled template
+module.exports = function( options, nm, file_contents ){
+
+ // override those settings
+ ['evaluate', 'interpolate', 'escape'].forEach(function(key){
+ if( !options[key] ) return;
+ if( !_.isRegExp(options[key]) ) options[key] = new RegExp(options[key]);
+ });
+
+ var func;
+ try {
+ func = options.namespace + '["'+ nm +'"] = '+ _.template(file_contents, null, options).source + ';\n'
+ } catch( e ){
+ console.error( 'Error processing '+ nm, e, e.stack);
+ return '/* Unable to compile ' + nm + ' */\n';
+ }
+
+ return func;
+};
View
@@ -20,6 +20,7 @@
"handlebars": "~1.0.2beta",
"jqtpl": "~1.0.9",
"underscore": "~1.3.3",
+ "lodash": "~2.3.0",
"async": "~0.1.18",
"coffee-script": "~1.2.0",
"watchr": "~1.0.0",
View
@@ -55,16 +55,17 @@ or
Usage :
- $ jst [--template format: string|underscore|_|jquery-tmpl|handlebars|hbs|dust|hogan|mustache|mu] [INPUT_DIR] [OUTPUT?]
+ $ jst [--template format: string|underscore|_|lodash|jquery-tmpl|handlebars|hbs|dust|hogan|mustache|mu] [INPUT_DIR] [OUTPUT?]
- --template -t format : string|underscore|_|jquery-tmpl|handlebars|hbs|dust|hogan|mustache|mu
+ --template -t format : string|underscore|_|lodash|jquery-tmpl|handlebars|hbs|dust|hogan|mustache|mu
--inputdir -i directory containings the templates to compile $PWD
--output -o output where templates will be compiled
--watch -w watch `inputdir` for change false
--namespace -ns object in the browser containing the templates window.JST
--include -I Glob patterns for templates files to include in `inputdir` **/*
--stdout -s Print the result in stdout instead of writing in a file false
--verbose -v Print logs for debug false
+ --variable -V Set _.tempateSettings.variable value
## Node usage.
@@ -79,6 +80,7 @@ Universal-JST export a list of engines.
hbs: [Function: build],
'jquery-tmpl': [Function: build],
underscore: [Function: build],
+ lodash: [Function: build],
dust: [Function: build],
mu: [Function: build],
mustache: [Function: build],
@@ -179,9 +181,10 @@ Partials are working. Each template is also a partial, i.e. `Handlebars.partials
`--helpers /path/to/a/dir/containing/helpers` look at this example [helper](universal-jst/tree/master/example/handlebars/helpers), [template](universal-jst/tree/master/example/handlebars/templates/helpers.hbs)
-### Underscore
+### Underscore or Lodash
-`--evaluate`, `--interpolate`, `--escape` are accepted. Those strings will be transformed into a new RegExp
+`--evaluate`, `--interpolate`, `--escape` are accepted. Those strings will be transformed into a new RegExp.
+`--variable` option can be specified to prevent `with` construction usage.
## Contributing

0 comments on commit d890c7e

Please sign in to comment.