mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-19 22:40:48 -07:00
Iterate keys of object when used with for...in loop
This commit is contained in:
+19
-2
@@ -38,8 +38,10 @@ module.exports = function (liquid) {
|
|||||||
render: function (scope, hash) {
|
render: function (scope, hash) {
|
||||||
var collection = Liquid.evalExp(this.collection, scope)
|
var collection = Liquid.evalExp(this.collection, scope)
|
||||||
|
|
||||||
if (!Array.isArray(collection) ||
|
if (isObject(collection)) {
|
||||||
(Array.isArray(collection) && collection.length === 0)) {
|
collection = Object.keys(collection)
|
||||||
|
}
|
||||||
|
if (isEmpty(collection)) {
|
||||||
return liquid.renderer.renderTemplates(this.elseTemplates, scope)
|
return liquid.renderer.renderTemplates(this.elseTemplates, scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,3 +92,18 @@ module.exports = function (liquid) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isObject(collection) {
|
||||||
|
if (collection) {
|
||||||
|
const ctr = collection.constructor
|
||||||
|
return ctr === Object || ctr == null
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
function isEmpty(collection) {
|
||||||
|
if (Array.isArray(collection)) {
|
||||||
|
return !collection.length
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user