8000 Avoid attribute() to fail on undefined object · drzraf/twig.js@ec635df · GitHub
[go: up one dir, main page]

Skip to content

Commit ec635df

Browse files
author
Raphaël Droz
committed
Avoid attribute() to fail on undefined object
1 parent 39680b8 commit ec635df

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/twig.functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ module.exports = function (Twig) {
188188
}
189189

190190
// Array will return element 0-index
191-
return object[method] || undefined;
191+
return object ? (object[method] || undefined) : undefined;
192192
},
193193
max(values, ...args) {
194194
if (Twig.lib.is('Object', values)) {

test/test.functions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ describe('Twig.js Functions ->', function () {
274274
})
275275
.should.equal('');
276276
});
277+
278+
it('should return undef for undefined object', function () {
279+
twig({data: '{{ attribute(arr, "bar") }}'}).render({})
280+
.should.equal('');
281+
});
277282
});
278283
describe('template_from_string ->', function () {
279284
it('should load a template from a string', function () {

0 commit comments

Comments
 (0)
0