8000 Merge pull request #609 from toptalo/issue-608 · twigjs/twig.js@93cef76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 93cef76

Browse files
authored
Merge pull request #609 from toptalo/issue-608
Add possibility to define namespace without slash at the end of the path
2 parents a4b85d6 + bdbe1f1 commit 93cef76

File tree

3 files changed

+178
-76
lines changed

3 files changed

+178
-76
lines changed

src/twig.path.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,33 @@ module.exports = function (Twig) {
1414
* the previously registered namespaces.
1515
*
1616
* @param {string} template The Twig Template
17-
* @param {string} file The file path, may be relative and may contain namespaces.
17+
* @param {string} _file The file path, may be relative and may contain namespaces.
1818
*
1919
* @return {string} The canonical version of the path
2020
*/
21-
Twig.path.parsePath = function(template, _file) {
21+
Twig.path.parsePath = function (template, _file) {
2222
var k = null,
23-
value = null,
2423
namespaces = template.options.namespaces,
2524
file = _file || "",
26-
hasNamespaces = namespaces && typeof namespaces === 'object';
25+
hasNamespaces = namespaces && typeof namespaces === "object";
2726

28-
if (hasNamespaces){
27+
if (hasNamespaces) {
2928
for (k in namespaces) {
30-
if (file.indexOf(k) === -1) {
31-
continue
32-
}
29+
if (file.indexOf(k) === -1) {
30+
continue
31+
}
3332

3433
// check if keyed namespace exists at path's start
35-
var colon = new RegExp('^' + k + '::');
36-
var atSign = new RegExp('^@' + k);
34+
var colon = new RegExp("^" + k + "::");
35+
var atSign = new RegExp("^@" + k + "/");
36+
// add slash to the end of path
37+
var namespacePath = namespaces[k].replace(/([^\/])$/, "$1/");
3738

3839
if (colon.test(file)) {
39-
file = file.replace(k + '::', namespaces[k]);
40+
file = file.replace(colon, namespacePath);
4041
return file;
4142
} else if (atSign.test(file)) {
42-
file = file.replace('@' + k, namespaces[k]);
43+
file = file.replace(atSign, namespacePath);
4344
return file;
4445
}
4546
}
@@ -52,21 +53,22 @@ module.exports = function (Twig) {
5253
* Generate the relative canonical version of a url based on the given base path and file path.
5354
*
5455
* @param {Twig.Template} template The Twig.Template.
55-
* @param {string} file The file path, relative to the base path.
56+
* @param {string} _file The file path, relative to the base path.
5657
*
5758
* @return {string} The canonical version of the path.
5859
*/
59-
Twig.path.relativePath = function(template, file) {
60+
Twig.path.relativePath = function (template, _file) {
6061
var base,
6162
base_path,
6263
sep_chr = "/",
6364
new_path = [],
64-
file = file || "",
65+
file = _file || "",
6566
val;
6667

6768 if (template.url) {
68-
if (typeof template.base !== 'undefined') {
69-
base = template.base + ((template.base.charAt(template.base.length-1) === '/') ? '' : '/');
69+
if (typeof template.base !== "undefined") {
70+
// add slash to the end of path
71+
base = template.base.replace(/([^\/])$/, "$1/");
7072
} else {
7173
base = template.url;
7274
}
@@ -78,15 +80,15 @@ module.exports = function (Twig) {
7880
file = file.replace(/\//g, sep);
7981

8082
if (template.base !== undefined && file.match(relative) == null) {
81-
file = file.replace(template.base, '');
83+
file = file.replace(template.base, "");
8284
base = template.base + sep;
8385
} else {
8486
base = path.normalize(template.path);
8587
}
8688

87-
base = base.replace(sep+sep, sep);
89+
base = base.replace(sep + sep, sep);
8890
sep_chr = sep;
89-
} else if ((template.name || template.id) && template.method && template.method !== 'fs' && template.method !== 'ajax') {
91+
} else if ((template.name || template.id) && template.method && template.method !== "fs" && template.method !== "ajax") {
9092
// Custom registered loader
9193
base = template.base || template.name || template.id;
9294
} else {
@@ -101,9 +103,9 @@ module.exports = function (Twig) {
101103

102104
while (base_path.length > 0) {
103105
val = base_path.shift();
104-
if (val == ".") {
106+
if (val === ".") {
105107
// Ignore
106-
} else if (val == ".." && new_path.length > 0 && new_path[new_path.length-1] != "..") {
108+
} else if (val === ".." && new_path.length > 0 && new_path[new_path.length - 1] !== "..") {
107109
new_path.pop();
108110
} else {
109111
new_path.push(val);

test/browser/test.namespace.js

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,104 @@
11
var Twig = (Twig || require("../twig")).factory(),
2-
twig = twig || Twig.twig;
2+
twig = twig || Twig.twig;
33

44
describe("Twig.js Namespaces ->", function() {
5-
it("should support namespaces defined with ::", function(done) {
6-
twig({
5+
it("should support namespaces defined with ::", function(done) {
6+
twig({
77
namespaces: { 'test': 'templates/namespaces/' },
8-
href: 'templates/namespaces_coloncolon.twig',
8+
path: 'templates/namespaces_coloncolon.twig',
99
load: function(template) {
1010
// Render the template
1111
template.render({
12-
test: "yes",
13-
flag: true
12+
test: "yes",
13+
flag: true
1414
}).should.equal("namespaces");
1515

1616
done();
17-
}
18-
});
19-
});
17+
}
18+
});
19+
});
2020

21-
it("should support namespaces defined with @", function(done) {
22-
twig({
21+
it("should support namespaces defined with :: and without slash at the end of the path", function(done) {
22+
twig({
23+
namespaces: { 'test': 'templates/namespaces' },
24+
path: 'templates/namespaces_coloncolon.twig',
25+
load: function(template) {
26+
// Render the template
27+
template.render({
28+
test: "yes",
29+
flag: true
30+
}).should.equal("namespaces");
31+
32+
done();
33+
}
34+
});
35+
});
36+
37+
it("should support namespaces defined with @", function(done) {
38+
twig({
2339
namespaces: { 'test': 'templates/namespaces/' },
24-
href: 'templates/namespaces_@.twig',
40+
path: 'templates/namespaces_@.twig',
41+
load: function(template) {
42+
// Render the template
43+
template.render({
44+
test: "yes",
45+
flag: true
46+
}).should.equal("namespaces");
47+
48+
done();
49+
}
50+
});
51+
});
52+
53+
it("should support namespaces defined with @ and without slash at the end of the path", function(done) {
54+
twig({
55+
namespaces: { 'test': 'templates/namespaces' },
56+
path: 'templates/namespaces_@.twig',
2557
load: function(template) {
2658
// Render the template
2759
template.render({
28-
test: "yes",
29-
flag: true
60+
test: "yes",
61+
flag: true
3062
}).should.equal("namespaces");
3163

3264
done();
33-
}
34-
});
35-
});
65+
}
66+
});
67+
});
68+
69+
it("should support non-namespaced includes with namespaces configured", function(done) {
70+
twig({
71+
namespaces: { 'test': 'templates/namespaces/' },
72+
path: 'templates/namespaces_without_namespace.twig',
73+
load: function(template) {
74+
// Render the template
75+
template.render({
76+
test: "yes",
77+
flag: true
78+
}).should.equal("namespaces\nnamespaces");
79+
80+
done();
81+
}
82+
});
83+
});
84+
85+
it("should support multiple namespaces", function(done) {
86+
twig({
87+
namespaces: {
88+
'one': 'templates/namespaces/one/',
89+
'two': 'templates/namespaces/two/'
90+
},
91+
path: 'templates/namespaces_multiple.twig',
92+
load: function(template) {
93+
// Render the template
94+
template.render({
95+
test: "yes",
96+
flag: true
97+
}).should.equal("namespace one\nnamespace two");
98+
99+
done();
100+
}
101+
});
102+
});
103+
36104
});

test/test.namespaces.js

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,104 @@
11
var Twig = (Twig || require("../twig")).factory(),
2-
twig = twig || Twig.twig;
2+
twig = twig || Twig.twig;
33

44
describe("Twig.js Namespaces ->", function() {
5-
it("should support namespaces defined with ::", function(done) {
6-
twig({
5+
it("should support namespaces defined with ::", function(done) {
6+
twig({
77
namespaces: { 'test': 'test/templates/namespaces/' },
88
path: 'test/templates/namespaces_coloncolon.twig',
99
load: function(template) {
1010
// Render the template
1111
template.render({
12-
test: "yes",
13-
flag: true
12+
test: "yes",
13+
flag: true
1414
}).should.equal("namespaces");
1515

1616
done();
17-
}
18-
});
19-
});
17+
}
18+
});
19+
});
20+
21+
it("should support namespaces defined with :: and without slash at the end of path", function(done) {
22+
twig({
23+
namespaces: { 'test': 'test/templates/namespaces' },
24+
path: 'test/templates/namespaces_coloncolon.twig',
25+
load: function(template) {
26+
// Render the template
27+
template.render({
28+
test: "yes",
29+
flag: true
30+
}).should.equal("namespaces");
2031

21-
it("should support namespaces defined with @", function(done) {
22-
twig({
32+
done();
33+
}
34+
});
35+
});
36+
37+
it("should support namespaces defined with @", function(done) {
38+
twig({
2339
namespaces: { 'test': 'test/templates/namespaces/' },
2440
path: 'test/templates/namespaces_@.twig',
2541
load: function(template) {
2642
// Render the template
2743
template.render({
28-
test: "yes",
29-
flag: true
44+
test: "yes",
45+
flag: true
3046
}).should.equal("namespaces");
3147

3248
done();
33-
}
34-
});
49+
}
50+
});
51+
});
52+
53+
it("should support namespaces defined with @ and without slash at the end of path", function(done) {
54+
twig({
55+
namespaces: { 'test': 'test/templates/namespaces' },
56+
path: 'test/templates/namespaces_@.twig',
57+
load: function(template) {
58+
// Render the template
59+
template.render({
60+
test: "yes",
61+
flag: true
62+
}).should.equal("namespaces");
63+
64+
done();
65+
}
66+
});
3567
});
3668

3769
it("should support non-namespaced includes with namespaces configured", function(done) {
38-
twig({
70+
twig({
3971
namespaces: { 'test': 'test/templates/namespaces/' },
4072
path: 'test/templates/namespaces_without_namespace.twig',
4173
load: function(template) {
4274
// Render the template
4375
template.render({
44-
test: "yes",
45-
flag: true
76+
test: "yes",
77+
flag: true
4678
}).should.equal("namespaces\nnamespaces");
4779

4880
done();
49-
}
50-
});
51-
});
81+
}
82+
});
83+
});
5284

53-
it("should support multiple namespaces", function(done) {
54-
twig({
55-
namespaces: {
56-
'one': 'test/templates/namespaces/one/',
57-
'two': 'test/templates/namespaces/two/'
58-
},
59-
path: 'test/templates/namespaces_multiple.twig',
60-
load: function(template) {
61-
// Render the template
62-
template.render({
63-
test: "yes",
64-
flag: true
65-
}).should.equal("namespace one\nnamespace two");
85+
it("should support multiple namespaces", function(done) {
86+
twig({
87+
namespaces: {
88+
'one': 'test/templates/namespaces/one/',
89+
'two': 'test/templates/namespaces/two/'
90+
},
91+
path: 'test/templates/namespaces_multiple.twig',
92+
load: function(template) {
93+
// Render the template
94+
template.render({
95+
test: "yes",
96+
flag: true
97+
}).should.equal("namespace one\nnamespace two");
6698

67-
done();
68-
}
69-
});
70-
});
99+
done();
100+
}
101+
});
102+
});
71103

72104
});

0 commit comments

Comments
 (0)
0