diff --git a/src/twig.path.js b/src/twig.path.js index b9d25b9a..2e8760c4 100644 --- a/src/twig.path.js +++ b/src/twig.path.js @@ -14,32 +14,33 @@ module.exports = function (Twig) { * the previously registered namespaces. * * @param {string} template The Twig Template - * @param {string} file The file path, may be relative and may contain namespaces. + * @param {string} _file The file path, may be relative and may contain namespaces. * * @return {string} The canonical version of the path */ - Twig.path.parsePath = function(template, _file) { + Twig.path.parsePath = function (template, _file) { var k = null, - value = null, namespaces = template.options.namespaces, file = _file || "", - hasNamespaces = namespaces && typeof namespaces === 'object'; + hasNamespaces = namespaces && typeof namespaces === "object"; - if (hasNamespaces){ + if (hasNamespaces) { for (k in namespaces) { - if (file.indexOf(k) === -1) { - continue - } + if (file.indexOf(k) === -1) { + continue + } // check if keyed namespace exists at path's start - var colon = new RegExp('^' + k + '::'); - var atSign = new RegExp('^@' + k); + var colon = new RegExp("^" + k + "::"); + var atSign = new RegExp("^@" + k + "/"); + // add slash to the end of path + var namespacePath = namespaces[k].replace(/([^\/])$/, "$1/"); if (colon.test(file)) { - file = file.replace(k + '::', namespaces[k]); + file = file.replace(colon, namespacePath); return file; } else if (atSign.test(file)) { - file = file.replace('@' + k, namespaces[k]); + file = file.replace(atSign, namespacePath); return file; } } @@ -52,21 +53,22 @@ module.exports = function (Twig) { * Generate the relative canonical version of a url based on the given base path and file path. * * @param {Twig.Template} template The Twig.Template. - * @param {string} file The file path, relative to the base path. + * @param {string} _file The file path, relative to the base path. * * @return {string} The canonical version of the path. */ - Twig.path.relativePath = function(template, file) { + Twig.path.relativePath = function (template, _file) { var base, base_path, sep_chr = "/", new_path = [], - file = file || "", + file = _file || "", val; if (template.url) { - if (typeof template.base !== 'undefined') { - base = template.base + ((template.base.charAt(template.base.length-1) === '/') ? '' : '/'); + if (typeof template.base !== "undefined") { + // add slash to the end of path + base = template.base.replace(/([^\/])$/, "$1/"); } else { base = template.url; } @@ -78,15 +80,15 @@ module.exports = function (Twig) { file = file.replace(/\//g, sep); if (template.base !== undefined && file.match(relative) == null) { - file = file.replace(template.base, ''); + file = file.replace(template.base, ""); base = template.base + sep; } else { base = path.normalize(template.path); } - base = base.replace(sep+sep, sep); + base = base.replace(sep + sep, sep); sep_chr = sep; - } else if ((template.name || template.id) && template.method && template.method !== 'fs' && template.method !== 'ajax') { + } else if ((template.name || template.id) && template.method && template.method !== "fs" && template.method !== "ajax") { // Custom registered loader base = template.base || template.name || template.id; } else { @@ -101,9 +103,9 @@ module.exports = function (Twig) { while (base_path.length > 0) { val = base_path.shift(); - if (val == ".") { + if (val === ".") { // Ignore - } else if (val == ".." && new_path.length > 0 && new_path[new_path.length-1] != "..") { + } else if (val === ".." && new_path.length > 0 && new_path[new_path.length - 1] !== "..") { new_path.pop(); } else { new_path.push(val); diff --git a/test/browser/test.namespace.js b/test/browser/test.namespace.js index b01adf77..2240f057 100644 --- a/test/browser/test.namespace.js +++ b/test/browser/test.namespace.js @@ -1,36 +1,104 @@ var Twig = (Twig || require("../twig")).factory(), - twig = twig || Twig.twig; + twig = twig || Twig.twig; describe("Twig.js Namespaces ->", function() { - it("should support namespaces defined with ::", function(done) { - twig({ + it("should support namespaces defined with ::", function(done) { + twig({ namespaces: { 'test': 'templates/namespaces/' }, - href: 'templates/namespaces_coloncolon.twig', + path: 'templates/namespaces_coloncolon.twig', load: function(template) { // Render the template template.render({ - test: "yes", - flag: true + test: "yes", + flag: true }).should.equal("namespaces"); done(); - } - }); - }); + } + }); + }); - it("should support namespaces defined with @", function(done) { - twig({ + it("should support namespaces defined with :: and without slash at the end of the path", function(done) { + twig({ + namespaces: { 'test': 'templates/namespaces' }, + path: 'templates/namespaces_coloncolon.twig', + load: function(template) { + // Render the template + template.render({ + test: "yes", + flag: true + }).should.equal("namespaces"); + + done(); + } + }); + }); + + it("should support namespaces defined with @", function(done) { + twig({ namespaces: { 'test': 'templates/namespaces/' }, - href: 'templates/namespaces_@.twig', + path: 'templates/namespaces_@.twig', + load: function(template) { + // Render the template + template.render({ + test: "yes", + flag: true + }).should.equal("namespaces"); + + done(); + } + }); + }); + + it("should support namespaces defined with @ and without slash at the end of the path", function(done) { + twig({ + namespaces: { 'test': 'templates/namespaces' }, + path: 'templates/namespaces_@.twig', load: function(template) { // Render the template template.render({ - test: "yes", - flag: true + test: "yes", + flag: true }).should.equal("namespaces"); done(); - } - }); - }); + } + }); + }); + + it("should support non-namespaced includes with namespaces configured", function(done) { + twig({ + namespaces: { 'test': 'templates/namespaces/' }, + path: 'templates/namespaces_without_namespace.twig', + load: function(template) { + // Render the template + template.render({ + test: "yes", + flag: true + }).should.equal("namespaces\nnamespaces"); + + done(); + } + }); + }); + + it("should support multiple namespaces", function(done) { + twig({ + namespaces: { + 'one': 'templates/namespaces/one/', + 'two': 'templates/namespaces/two/' + }, + path: 'templates/namespaces_multiple.twig', + load: function(template) { + // Render the template + template.render({ + test: "yes", + flag: true + }).should.equal("namespace one\nnamespace two"); + + done(); + } + }); + }); + }); diff --git a/test/test.namespaces.js b/test/test.namespaces.js index 93731e50..fafe4273 100644 --- a/test/test.namespaces.js +++ b/test/test.namespaces.js @@ -1,72 +1,104 @@ var Twig = (Twig || require("../twig")).factory(), - twig = twig || Twig.twig; + twig = twig || Twig.twig; describe("Twig.js Namespaces ->", function() { - it("should support namespaces defined with ::", function(done) { - twig({ + it("should support namespaces defined with ::", function(done) { + twig({ namespaces: { 'test': 'test/templates/namespaces/' }, path: 'test/templates/namespaces_coloncolon.twig', load: function(template) { // Render the template template.render({ - test: "yes", - flag: true + test: "yes", + flag: true }).should.equal("namespaces"); done(); - } - }); - }); + } + }); + }); + + it("should support namespaces defined with :: and without slash at the end of path", function(done) { + twig({ + namespaces: { 'test': 'test/templates/namespaces' }, + path: 'test/templates/namespaces_coloncolon.twig', + load: function(template) { + // Render the template + template.render({ + test: "yes", + flag: true + }).should.equal("namespaces"); - it("should support namespaces defined with @", function(done) { - twig({ + done(); + } + }); + }); + + it("should support namespaces defined with @", function(done) { + twig({ namespaces: { 'test': 'test/templates/namespaces/' }, path: 'test/templates/namespaces_@.twig', load: function(template) { // Render the template template.render({ - test: "yes", - flag: true + test: "yes", + flag: true }).should.equal("namespaces"); done(); - } - }); + } + }); + }); + + it("should support namespaces defined with @ and without slash at the end of path", function(done) { + twig({ + namespaces: { 'test': 'test/templates/namespaces' }, + path: 'test/templates/namespaces_@.twig', + load: function(template) { + // Render the template + template.render({ + test: "yes", + flag: true + }).should.equal("namespaces"); + + done(); + } + }); }); it("should support non-namespaced includes with namespaces configured", function(done) { - twig({ + twig({ namespaces: { 'test': 'test/templates/namespaces/' }, path: 'test/templates/namespaces_without_namespace.twig', load: function(template) { // Render the template template.render({ - test: "yes", - flag: true + test: "yes", + flag: true }).should.equal("namespaces\nnamespaces"); done(); - } - }); - }); + } + }); + }); - it("should support multiple namespaces", function(done) { - twig({ - namespaces: { - 'one': 'test/templates/namespaces/one/', - 'two': 'test/templates/namespaces/two/' - }, - path: 'test/templates/namespaces_multiple.twig', - load: function(template) { - // Render the template - template.render({ - test: "yes", - flag: true - }).should.equal("namespace one\nnamespace two"); + it("should support multiple namespaces", function(done) { + twig({ + namespaces: { + 'one': 'test/templates/namespaces/one/', + 'two': 'test/templates/namespaces/two/' + }, + path: 'test/templates/namespaces_multiple.twig', + load: function(template) { + // Render the template + template.render({ + test: "yes", + flag: true + }).should.equal("namespace one\nnamespace two"); - done(); - } - }); - }); + done(); + } + }); + }); });