Skip to content
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

Fix twig {% if(x) %} and {% elseif(x) %} blocks parsing error #570

Merged
merged 3 commits into from
Jun 26, 2018
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
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');
});
});

});