# Call to a member function addLoop() on null

**URL:** https://discourse.roots.io/t/call-to-a-member-function-addloop-on-null/11052
**Category:** sage
**Tags:** sage9
**Created:** 2017-12-01T16:08:15Z
**Posts:** 8

## Post 1 by @ilja — 2017-12-01T16:08:15Z

I run code like this in a template in the latest Sage beta theme

@foreach ($elements as $element)  
{{$element}}  
@endforeach

I get this error: Call to a member function addLoop() on null

I looked into the compiled template and found the offending code:  
$\_\_env-\>addLoop($\_\_currentLoopData);

Is anybody else experiencing the same issue, or is it just me?

---

## Post 2 by @Webstractions — 2017-12-01T17:16:21Z

That error means the variable `$elements` is null. Possibly you have not created that variable in a controller yet?

---

## Post 3 by @ilja — 2017-12-02T05:37:50Z

Thanks for trying, but nope, i did create a var directly in the template. normal \<?php foreach ?\> works, @foreach does not. I appears that $\_\_env is a null, why is beyond me.

---

## Post 4 by @Webstractions — 2017-12-02T18:04:38Z

If there isn’t a controller for your template, then the data variables will not be created. This includes the `$__env` variable.

---

## Post 5 by @ilja — 2017-12-02T19:12:02Z

OK, I added a function to app controller. tried accessing it as a variable in the template. same error occurs. besides i thought that creating arrays directly in the template and looping over them with @foreach was still legal.

---

## Post 6 by @shaneparsons — 2018-08-20T14:35:27Z

Did you ever figure this out? I’m having a very similar issue.

I don’t understand why the variable (an array in my case) is accessible through a normal php foreach, but not a blade one…

---

## Post 7 by @ilja — 2018-08-20T15:14:16Z

This was quite a bit of time ago. From what I can recall, I either updated composer deps in the sage theme folder or just reinstalled it. Sorry not the best answer, let me know what works for you.

---

## Post 8 by @mejta — 2018-08-21T14:17:37Z

See [https://github.com/roots/sage/pull/2095](https://github.com/roots/sage/pull/2095)
