mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -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) {
|
||||
var collection = Liquid.evalExp(this.collection, scope)
|
||||
|
||||
if (!Array.isArray(collection) ||
|
||||
(Array.isArray(collection) && collection.length === 0)) {
|
||||
if (isObject(collection)) {
|
||||
collection = Object.keys(collection)
|
||||
}
|
||||
if (isEmpty(collection)) {
|
||||
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