10000 Add possibility to define namespace without slash at the end of the path by toptalo · Pull Request #609 · twigjs/twig.js · GitHub
[go: up one dir, main page]

Skip to content

Add possibility to define namespace without slash at the end of the path #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions src/twig.path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
102 changes: 85 additions & 17 deletions test/browser/test.namespace.js
10000
Original file line number Diff line number Diff line change
@@ -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();
}
});
});

});
106 changes: 69 additions & 37 deletions test/test.namespaces.js
9303
Original file line number Diff line number Diff line change
@@ -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();
}
});
});

});
0