Menu icon Foundation
Accessing vars outside of a Panini #each loop

According to handlbars docs, I should be able to use ../ to jump outside of the object in an #each loop to get another variable.

For example, this...

---

img_path: assets/img

list:

    - image: fancy_nancy.png
      alt: Transparency is the best!
    - image: jpgs/tiny_dancer.jpg
      alt: Small files for great justice!
---

{{#each list}}
  <img src="{{../pth}}/list/{{this.image}}" alt="{{this.alt}}">
{{/each}}

...should render...

<img src="assets/img/list/fancy_nancy.png" alt="Transparency is the best!">
<img src="assets/img/list/jpgs/tiny_dancer.jpg" alt="Small files for great justice!">

...but it drops the path instead.

 

Or am I doing something wrong?

 

paniniHandlebars#each loops

According to handlbars docs, I should be able to use ../ to jump outside of the object in an #each loop to get another variable.

For example, this...

---

img_path: assets/img

list:

    - image: fancy_nancy.png
      alt: Transparency is the best!
    - image: jpgs/tiny_dancer.jpg
      alt: Small files for great justice!
---

{{#each list}}
  <img src="{{../pth}}/list/{{this.image}}" alt="{{this.alt}}">
{{/each}}

...should render...

<img src="assets/img/list/fancy_nancy.png" alt="Transparency is the best!">
<img src="assets/img/list/jpgs/tiny_dancer.jpg" alt="Small files for great justice!">

...but it drops the path instead.

 

Or am I doing something wrong?

 

Rafi Benkual almost 3 years ago

 Hi Jeremy 

Are you trying to point to the location of the data? 

Jeremy Reynolds almost 3 years ago

I tried a bunch of variations yesterday, and I think I've figured it out now.

My goal is to just grab the data from outside of the each array. ../ wasn't stepping outside of the context like it is supposed to, but I wasn't getting any compiling errors in npm. And then it just started working again. Weird.

I'm using it to replace {{root}}, which only adapts to my local file structure. {{path}} lets me test locally and then switch to my remote server's structure when building for deployment.