Menu icon Foundation
[panini] Check if a variable in your data exist

Hi

Been reading up on https://zurb.com/university/lessons/advanced-panini-tips-and-tricks and was wondering if it's possible to check if a certain variable exists in your data (.yml) and then take action.

For example, I have set up my data for ecommerce products, and many of the variables are mandatory, like product name, price, image, description. But I also have variables that may be true for some products, if it's new, if it's on sale, or just out of stock.

so It could look something like this (.yml):

items:
-
name: 'Magic Rainbow Dust'
price: 330
desc: 'Something about this item'
deal: '4 for 3'
-
name: 'Wirt's leg'
price: 220
desc: 'Did he lose it again?'
-
name: 'Crystallized Unicorn Poo '
price: 1321
desc: 'Just don't eat it'
deal: 'limited edition'

 

 

 

And my HTML would look something like this:

{{#each product-data.items}}
  <h1>{{name}}</h1>
  <span class="deal">{{deal}}</span>
  <span class="price">{{price}}</span>
  <p>{{desc}}</p>
{{/each}}

 

And as you can see, when I have no deal for a certain item, it will still output the <span class="deal"></span>

So my question is, is it possible to check if a variable in you data exist, and if it does, output it, else just skip it.

Any help much appreciated. Thanks again for a wonderful framework.

paninidata

Hi

Been reading up on https://zurb.com/university/lessons/advanced-panini-tips-and-tricks and was wondering if it's possible to check if a certain variable exists in your data (.yml) and then take action.

For example, I have set up my data for ecommerce products, and many of the variables are mandatory, like product name, price, image, description. But I also have variables that may be true for some products, if it's new, if it's on sale, or just out of stock.

so It could look something like this (.yml):

items:
-
name: 'Magic Rainbow Dust'
price: 330
desc: 'Something about this item'
deal: '4 for 3'
-
name: 'Wirt's leg'
price: 220
desc: 'Did he lose it again?'
-
name: 'Crystallized Unicorn Poo '
price: 1321
desc: 'Just don't eat it'
deal: 'limited edition'

 

 

 

And my HTML would look something like this:

{{#each product-data.items}}
  <h1>{{name}}</h1>
  <span class="deal">{{deal}}</span>
  <span class="price">{{price}}</span>
  <p>{{desc}}</p>
{{/each}}

 

And as you can see, when I have no deal for a certain item, it will still output the <span class="deal"></span>

So my question is, is it possible to check if a variable in you data exist, and if it does, output it, else just skip it.

Any help much appreciated. Thanks again for a wonderful framework.