Search code examples
node.jspug

how to output a block of code depending on a variable in pug (jade)


how can I correctly display a block of code in a mixin depending on the class in a variable? help`s me please. My example doesn't work. I attached the mixin and then i showed the mixin call.

mixin media(data)
  article.social-media__items 
    each media in data.medias
      if media.class=='social-media__content--site'
        article(class="social-media__content" + media.class)
          .social-media__text
            p #[a(href="#") #{media.span}] #{media.text}
      else 
        article(class="social-media__content" + media.class)
          .social-media__text
            p #[span #{media.span}] #{media.text}

+media({'medias':[{
  'class' : ' social-media__content--news',
  'span' : 'lorem_ipsum-title',
  'text' : '- lorem_ipsum-text',
}, {
   'class' : ' social-media__content--site',
   'span' : 'www.sitename.com',
   'text' : '— lorem_ipsm-text',
 }]})

Solution

  • There's an extra space in your media.class value. Change your conditional to match the value.

    Instead of this:

    if media.class=='social-media__content--site'
    

    Do this:

    if media.class==' social-media__content--site'