diff --git a/Readme.md b/Readme.md index 83f236a..af90591 100644 --- a/Readme.md +++ b/Readme.md @@ -39,6 +39,7 @@ | [pug](https://github.com/pugjs/pug) | [`npm install pug`](https://www.npmjs.com/package/pug) | [(website)](http://jade-lang.com/) / **(formerly jade)** | | [qejs](https://github.com/jepso/QEJS) | [`npm install qejs`](https://www.npmjs.com/package/qejs) | - | | [ractive](https://github.com/ractivejs/ractive) | [`npm install ractive`](https://www.npmjs.com/package/ractive) | - | + | [razor](https://github.com/kinogam/kino.razor) | [`npm install razor`](https://www.npmjs.com/package/razor) | - | | [react](https://github.com/facebook/react) | [`npm install react`](https://www.npmjs.com/package/react) | - | | [slm](https://github.com/slm-lang/slm) | [`npm install slm`](https://www.npmjs.com/package/slm) | - | | ~~[swig](https://github.com/paularmstrong/swig)~~ | [`npm install swig`](https://www.npmjs.com/package/swig) | **(unmaintained)** | @@ -52,7 +53,6 @@ | [walrus](https://github.com/jeremyruppel/walrus) | [`npm install walrus`](https://www.npmjs.com/package/walrus) | [(website)](http://documentup.com/jeremyruppel/walrus/) | | [whiskers](https://github.com/gsf/whiskers.js) | [`npm install whiskers`](https://www.npmjs.com/package/whiskers) | - | - __NOTE__: you must still install the engines you wish to use, add them to your package.json dependencies. ## API diff --git a/lib/consolidate.js b/lib/consolidate.js index a198484..5024519 100644 --- a/lib/consolidate.js +++ b/lib/consolidate.js @@ -561,6 +561,59 @@ exports.swig.render = function(str, options, cb) { }); }; +/** + * Razor support. + */ + +exports.razor = function(path, options, cb) { + return promisify(cb, function(cb) { + var engine = requires.razor; + if (!engine) { + try { + engine = requires.razor = require('razor-tmpl'); + + } catch (err) { + + throw err; + + } + } + try { + + var tmpl = cache(options) || cache(options, (locals) => { + console.log('Rendering razor file', path); + return engine.renderFileSync(path, locals); + }); + cb(null, tmpl(options)); + } catch (err) { + cb(err); + } + }); +}; + +/** + * razor string support. + */ + +exports.razor.render = function(str, options, cb) { + return promisify(cb, function(cb) { + + try { + var engine = requires.razor = require('razor-tmpl'); + } catch (err) { + throw err; + } + + try { + var tf = engine.compile(str); + var tmpl = cache(options) || cache(options, tf); + cb(null, tmpl(options)); + } catch (err) { + cb(err); + } + }); +}; + /** * Atpl support. */ @@ -619,7 +672,9 @@ exports.twig.render = function(str, options, cb) { return promisify(cb, function(cb) { var engine = requires.twig || (requires.twig = require('twig').twig); var templateData = { - data: str + data: str, + allowInlineIncludes: options.allowInlineIncludes, + path: options.path }; try { var tmpl = cache(templateData) || cache(templateData, engine(templateData)); @@ -1678,6 +1733,33 @@ exports.teacup.render = function(str, options, cb) { }); }; +/** + * Squirrelly support. + */ + +exports.squirrelly = fromStringRenderer('squirrelly'); + +/** + * Squirrelly string support. + */ + +exports.squirrelly.render = function(str, options, cb) { + return promisify(cb, function(cb) { + var engine = requires.squirrelly || (requires.squirrelly = require('squirrelly')); + try { + for (var partial in options.partials) { + engine.definePartial(partial, options.partials[partial]); + } + for (var helper in options.helpers) { + engine.defineHelper(helper, options.helpers[helper]); + } + var tmpl = cache(options) || cache(options, engine.Compile(str, options)); + cb(null, tmpl(options, engine)); + } catch (err) { + cb(err); + } + }); +}; /** * expose the instance of the engine */ diff --git a/package.json b/package.json index dcf3346..cb34bc0 100644 --- a/package.json +++ b/package.json @@ -65,12 +65,14 @@ "pug": "^2.0.0-beta6", "qejs": "^3.0.5", "ractive": "^0.8.4", - "react": "^15.3.2", + "razor-tmpl": "^1.3.1", + "react": "^15.6.2", "react-dom": "^15.3.2", "should": "*", "slm": "^0.5.0", - "swig-templates": "^2.0.2", + "squirrelly": "^5.0.1", "swig": "^1.4.1", + "swig-templates": "^2.0.2", "teacup": "^2.0.0", "templayed": ">=0.2.3", "tinyliquid": "^0.2.30", @@ -78,9 +80,9 @@ "twig": "^0.10.0", "underscore": "^1.3.3", "vash": "^0.12.2", + "velocityjs": "^0.8.2", "walrus": "^0.10.1", - "whiskers": "^0.4.0", - "velocityjs": "^0.8.2" + "whiskers": "^0.4.0" }, "keywords": [ "engine", diff --git a/test/consolidate.js b/test/consolidate.js index 375bacc..193f329 100644 --- a/test/consolidate.js +++ b/test/consolidate.js @@ -68,3 +68,7 @@ require('./shared').test('marko'); require('./shared').test('bracket'); require('./shared').test('teacup'); require('./shared').test('velocityjs'); +require('./shared').test('razor'); +require('./shared').test('squirrelly'); +require('./shared/partials').test('squirrelly'); +require('./shared/helpers').test('squirrelly'); diff --git a/test/fixtures/razor/user.razor b/test/fixtures/razor/user.razor new file mode 100644 index 0000000..d939f72 --- /dev/null +++ b/test/fixtures/razor/user.razor @@ -0,0 +1 @@ +
@locals.user.name
diff --git a/test/fixtures/squirrelly/helpers.squirrelly b/test/fixtures/squirrelly/helpers.squirrelly new file mode 100644 index 0000000..94a91a6 --- /dev/null +++ b/test/fixtures/squirrelly/helpers.squirrelly @@ -0,0 +1,2 @@ +{{myhelper(options.user.name)}} +{{/myhelper}} \ No newline at end of file diff --git a/test/fixtures/squirrelly/partials.squirrelly b/test/fixtures/squirrelly/partials.squirrelly new file mode 100644 index 0000000..322719a --- /dev/null +++ b/test/fixtures/squirrelly/partials.squirrelly @@ -0,0 +1 @@ +{{include(partial)/}} \ No newline at end of file diff --git a/test/fixtures/squirrelly/user.squirrelly b/test/fixtures/squirrelly/user.squirrelly new file mode 100644 index 0000000..f5b9962 --- /dev/null +++ b/test/fixtures/squirrelly/user.squirrelly @@ -0,0 +1 @@ +{{user.name}}
\ No newline at end of file diff --git a/test/shared/helpers.js b/test/shared/helpers.js index ddde9e9..5d5f23b 100644 --- a/test/shared/helpers.js +++ b/test/shared/helpers.js @@ -1,6 +1,7 @@ var cons = require('../../'); var handlebars = require('handlebars'); +var Sqrl = require('squirrelly'); var fs = require('fs'); var readFile = fs.readFile; var readFileSync = fs.readFileSync; @@ -34,6 +35,23 @@ exports.test = function(name) { done(); }); }); + } else if (name === 'squirrelly') { + user = { name: 'Tobi' }; + + // Use case: return safe HTML that won’t be escaped in the final render. + it('should support helpers', function(done) { + var str = fs.readFileSync('test/fixtures/' + name + '/helpers.' + name).toString(); + Sqrl.defineHelper('myhelper', function(args, content, blocks) { + return args[0].slice(1, -1); + }); + var options = { user: user }; + + cons[name].render(str, options, function(err, html) { + if (err) return done(err); + html.should.equal('strong>Tobi