diff --git a/compiler.go b/compiler.go
index <HASH>..<HASH> 100644
--- a/compiler.go
+++ b/compiler.go
@@ -489,6 +489,9 @@ func (c *compiler) evalForExpression(node *ast.ForExpression) (interface{}, erro
 			}
 		}
 	default:
+		if iter == nil {
+			return nil, nil
+		}
 		if it, ok := iter.(Iterator); ok {
 			octx := c.ctx
 			i := 0
diff --git a/for_test.go b/for_test.go
index <HASH>..<HASH> 100644
--- a/for_test.go
+++ b/for_test.go
@@ -75,3 +75,13 @@ func Test_Render_For_Array_Key_Value(t *testing.T) {
 	r.NoError(err)
 	r.Equal("0a1b2c", s)
 }
+
+func Test_Render_For_Nil(t *testing.T) {
+	r := require.New(t)
+	input := `<% for (i,v) in nilValue {return v} %>`
+	ctx := NewContext()
+	ctx.Set("nilValue", nil)
+	s, err := Render(input, ctx)
+	r.NoError(err)
+	r.Equal("", s)
+}
return safely if trying to range over nil