Skip to content

Commit 5cd19f6

Browse files
committed
Fixes #372
1 parent 34b028f commit 5cd19f6

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/Engines/Nunjucks.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const NunjucksLib = require("nunjucks");
22
const TemplateEngine = require("./TemplateEngine");
3-
const config = require("../Config");
43

54
class Nunjucks extends TemplateEngine {
65
constructor(name, inputDir) {
@@ -69,18 +68,25 @@ class Nunjucks extends TemplateEngine {
6968
this.tags = [shortcodeName];
7069

7170
this.parse = function(parser, nodes, lexer) {
72-
var tok = parser.nextToken();
71+
let args;
72+
let tok = parser.nextToken();
7373

74-
var args = parser.parseSignature(null, true);
75-
parser.advanceAfterBlockEnd(tok.value);
74+
args = parser.parseSignature(true, true);
75+
76+
// Nunjucks bug with non-paired custom tags bug still exists even
77+
// though this issue is closed. Works fine for paired.
78+
// https://github.com/mozilla/nunjucks/issues/158
79+
if (args.children.length === 0) {
80+
args.addChild(new nodes.Literal(0, 0, ""));
81+
}
7682

83+
parser.advanceAfterBlockEnd(tok.value);
7784
return new nodes.CallExtensionAsync(this, "run", args);
7885
};
7986

8087
this.run = function(...args) {
8188
let callback = args.pop();
8289
let [context, ...argArray] = args;
83-
8490
let ret = new nunjucksEngine.runtime.SafeString(
8591
shortcodeFn(...argArray)
8692
);
@@ -98,7 +104,7 @@ class Nunjucks extends TemplateEngine {
98104
this.parse = function(parser, nodes, lexer) {
99105
var tok = parser.nextToken();
100106

101-
var args = parser.parseSignature(null, true);
107+
var args = parser.parseSignature(true, true);
102108
parser.advanceAfterBlockEnd(tok.value);
103109

104110
var body = parser.parseUntilBlocks("end" + shortcodeName);
@@ -111,7 +117,6 @@ class Nunjucks extends TemplateEngine {
111117
let callback = args.pop();
112118
let body = args.pop();
113119
let [context, ...argArray] = args;
114-
115120
let ret = new nunjucksEngine.runtime.SafeString(
116121
shortcodeFn(body(), ...argArray)
117122
);

test/TemplateRenderNunjucksTest.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ test("Nunjucks Render: with Library Override", async t => {
8080
t.is(await fn({ name: "Zach" }), "<p>Zach</p>");
8181
});
8282

83+
test("Nunjucks Shortcode without args", async t => {
84+
let tr = new TemplateRender("njk", "./test/stubs/");
85+
tr.engine.addShortcode("postfixWithZach", function() {
86+
return "Zach";
87+
});
88+
89+
t.is(await tr.render("{% postfixWithZach %}", {}), "Zach");
90+
});
91+
8392
test("Nunjucks Shortcode", async t => {
8493
let tr = new TemplateRender("njk", "./test/stubs/");
8594
tr.engine.addShortcode("postfixWithZach", function(str) {
@@ -119,6 +128,18 @@ test("Nunjucks Paired Shortcode", async t => {
119128
);
120129
});
121130

131+
test("Nunjucks Paired Shortcode without args", async t => {
132+
let tr = new TemplateRender("njk", "./test/stubs/");
133+
tr.engine.addPairedShortcode("postfixWithZach", function(content) {
134+
return content + "Zach";
135+
});
136+
137+
t.is(
138+
await tr.render("{% postfixWithZach %}Content{% endpostfixWithZach %}", {}),
139+
"ContentZach"
140+
);
141+
});
142+
122143
test("Nunjucks Paired Shortcode with Tag Inside", async t => {
123144
let tr = new TemplateRender("njk", "./test/stubs/");
124145
tr.engine.addPairedShortcode("postfixWithZach", function(content, str) {

0 commit comments

Comments
 (0)