diff --git a/.gitignore b/.gitignore index 3c3629e..4faa9b3 100755 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +coverage.html +coverage diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4a83e22 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - "0.11" + - "0.10" + - "0.8" diff --git a/README.md b/README.md new file mode 100644 index 0000000..3fddb66 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +Twigify +===== + +Twigify converts a .html.twig file to a compiled state using twig.js + +The idea is to precompile and package frontend templates for use. + +Tests +===== +Run them use `npm test` after making sure you run `npm install` first diff --git a/package.json b/package.json index 7bea5a5..70e67be 100755 --- a/package.json +++ b/package.json @@ -27,10 +27,14 @@ "twig": "^0.7.2" }, "devDependencies": { + "mocha": "*", + "should": "*", + "istanbul": "*" }, "engines": { "node": ">=0.10" }, "scripts": { + "test": "istanbul cover _mocha -- -R spec && mocha" } } diff --git a/test/example.html.twig b/test/example.html.twig new file mode 100644 index 0000000..aa7f636 --- /dev/null +++ b/test/example.html.twig @@ -0,0 +1 @@ +this is a test, with some {{ variables }} diff --git a/test/tests.js b/test/tests.js new file mode 100644 index 0000000..b8d5bbf --- /dev/null +++ b/test/tests.js @@ -0,0 +1,53 @@ +var should = require('should'), + twigify = require('../twigify'), + twig = require('twig').twig; + +/** + * use this in the tests to render the returned string from Twigify into a Twig + * rendered string, with optionally your Object containing the variables that your + * Twig template requires + * @param {String} twigifyRender String that Twigify returns, that you need + * to render still + * @param {Object} twigVars (Optional) Any variables within an Object that your + * template requires + * @return {String} rendered Twig template as a string + */ +var render = function(twigifyRender, twigVars) { + // Create an empty object if we're passed no twigVars + twigVars = twigVars || {}; + + // eval the Twigify string and then use Twig to render it + return eval(twigifyRender).render(twigVars); +}; + +describe('Twigify', function() { + describe('#twigify()', function() { + it ('should be a function', function() { + twigify.should.be.an.instanceOf(Function); + }); + + it('should render templates minified', function() { + var testMe = twigify.compile('a bc'); + testMe = render(testMe); + + testMe.should.equal('a bc'); + }); + + it('should render variables within the Twig template', function() { + var testMe = twigify.compile('hey {{ name }}'); + testMe = render(testMe, { + name: 'Scott' + }); + + testMe.should.equal('hey Scott'); + }); + + it('should load and render a test file', function() { + var testMe = twigify('test/example.html.twig'); + console.log(testMe); + + testMe.should.equal('this is a test, with some variable PLACEHOLDER!'); + }); + + }); +}); diff --git a/index.js b/twigify.js similarity index 95% rename from index.js rename to twigify.js index 164580c..7053d1a 100755 --- a/index.js +++ b/twigify.js @@ -14,7 +14,9 @@ var minifyDefaults = { function compile(str) { var minified = minify(str, minifyDefaults); - var template = twig({ data: minified }); + var template = twig({ + data: minified + }); var tokens = JSON.stringify(template.tokens)