Skip to content

Commit

Permalink
Fix twig {% if(x) %} and {% elseif(x) %} blocks parsing error (#570)
Browse files Browse the repository at this point in the history
* Fix twig `{% if(x) %}` and `{% elseif(x) %}` blocks parsing error

* Unified tests

* Fix comments
  • Loading branch information
toptalo authored and dave-irvine committed Jun 26, 2018
1 parent f8988ae commit 4138385
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/twig.logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = function (Twig) {
* Format: {% if expression %}
*/
type: Twig.logic.type.if_,
regex: /^if\s+([\s\S]+)$/,
regex: /^if\s?([\s\S]+)$/,
next: [
Twig.logic.type.else_,
Twig.logic.type.elseif,
Expand Down Expand Up @@ -119,7 +119,7 @@ module.exports = function (Twig) {
* Format: {% elseif expression %}
*/
type: Twig.logic.type.elseif,
regex: /^elseif\s+([^\s].*)$/,
regex: /^elseif\s?([^\s].*)$/,
next: [
Twig.logic.type.else_,
Twig.logic.type.elseif,
Expand Down Expand Up @@ -159,9 +159,9 @@ module.exports = function (Twig) {
},
{
/**
* Else if type logic tokens.
* Else type logic tokens.
*
* Format: {% elseif expression %}
* Format: {% else %}
*/
type: Twig.logic.type.else_,
regex: /^else$/,
Expand Down Expand Up @@ -349,9 +349,9 @@ module.exports = function (Twig) {
},
{
/**
* End if type logic tokens.
* End for type logic tokens.
*
* Format: {% endif %}
* Format: {% endfor %}
*/
type: Twig.logic.type.endfor,
regex: /^endfor$/,
Expand Down
30 changes: 22 additions & 8 deletions test/test.logic.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
var Twig = (Twig || require("../twig")).factory(),
twig = twig || Twig.twig;

describe("Twig.js Logic ->", function() {
describe("{% set key = expression %} ->", function() {
var test_template = twig({data: '{% set list = _context %}{{ list|json_encode }}'}),
d = {a: 10, b: 4, c: 2},
output = test_template.render(d),
expected = {a: 10, b: 4, c: 2};

output.should.equal(JSON.stringify(expected));
describe("Twig.js Logic ->", function () {
describe("set ->", function () {
it("should define variable", function () {
twig({
data: '{% set list = _context %}{{ list|json_encode }}'
}).render({a: 10, b: 4, c: 2}).should.equal(JSON.stringify({a: 10, b: 4, c: 2}));
});
});

describe("if ->", function () {
it("should ignore spaces", function () {
twig({data: '{% if (1 == 1) %}true{% endif %}'}).render().should.equal('true');
twig({data: '{% if(1 == 1) %}true{% endif %}'}).render().should.equal('true');
});
});

describe("elseif ->", function () {
it("should ignore spaces", function () {
twig({data: '{% if (1 == 2) %}false{% elseif (1 == 1) %}true{% endif %}'}).render().should.equal('true');
twig({data: '{% if (1 == 2) %}false{% elseif(1 == 1) %}true{% endif %}'}).render().should.equal('true');
});
});

});

0 comments on commit 4138385

Please sign in to comment.