2 Replies - 378 Views - Last Post: 05 July 2018 - 11:54 AM

#1 kayut   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 20-January 17

Handlebars.js - If {{ image_url }} doesn't contain "questionma

Posted 05 July 2018 - 05:45 AM

Hi,

As you can see in this example, which I created on Codepen, some of the data don't have an image and whenever there's no image, a place holder image is shown:

Handlebars.js Example

Is there any way in Handlebars.js to say that show the data only if the {{ image_url }} doesn't contain "questionmark"?

Something like this?

{{#each this}}
{{#if image_url !contains "questionmark"}}

    <li class='list-container'>
      <div class="image-container">
          <img src="{{ image_url }}">
      </div>
      <div class="name-container">
        {{ name }}
      </div>
      <div class='role-container'>{{{ role }}}</div>
    </li>

{{/if}}
{{/each}}


This post has been edited by kayut: 05 July 2018 - 05:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Handlebars.js - If {{ image_url }} doesn't contain "questionma

#2 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,618
  • Joined: 12-December 12

Re: Handlebars.js - If {{ image_url }} doesn't contain "questionma

Posted 05 July 2018 - 06:26 AM

You seem to be attempting this already with {{#if image_url !contains "questionmark"}}; what happens when you run it? Are there errors?

Do you have a link to the documentation for #if to save someone hunting for it?
Was This Post Helpful? 0
  • +
  • -

#3 kayut   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 20-January 17

Re: Handlebars.js - If {{ image_url }} doesn't contain "questionma

Posted 05 July 2018 - 11:54 AM

I couldn't find any way to achieve this with Handlebars.js, so I did it in Javascript

(function removeNoImage() {
  for(let key in data.staff) {
    if(data.staff[key].image_url.indexOf('questionmark') !== -1) {
      delete data.staff[key];
    }
  }
})();


This post has been edited by kayut: 05 July 2018 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1