pugpugjs

How do I use jade(pug)'s "if statement" inside a css block?


Say I have a jade template like:

style.
  .someclass1{
    font-size:10px;
  }
  .someclass2{
    font-size:10px;
  }
  .someclass3{
    font-size:10px;
  }
  .someclass4{
    font-size:10px;
  }
  .someclass5{
    font-size:10px;
  }

And I want to make .someclass3 with different results from different options when rendering the jade template.

Something like:

style.
  .someclass1{
    font-size:10px;
  }
  .someclass2{
    font-size:10px;
  }
  if(options.key1=='a')
    .someclass3{
      font-size:10px;
    }
  else
    .someclass3{
      font-size:8px;
    }
  .someclass4{
    font-size:10px;
  }
  .someclass5{
    font-size:10px;
  }

It's not working.

I have to break the style tag into two and do something like this:

style.
  .someclass1{
    font-size:10px;
  }
  .someclass2{
    font-size:10px;
  }
if(options.key1=='a')
  style.
    .someclass3{
      font-size:10px;
    }
else
  style.
    .someclass3{
      font-size:8px;
    }
style.
  .someclass4{
    font-size:10px;
  }
  .someclass5{
    font-size:10px;
  }

This will result in a 3-style tags HTML, which is actually only needing one.

Anyone has good way to do this?


Solution

  • You can do this. Instead of putting the . immediately after the style tag (which tells pug that the entire block is plain text), you can put the . before each block of plain text. eg.

    style
      .
        .someclass1{
          font-size:10px;
        }
        .someclass2{
          font-size:10px;
        }
      if options.key1=='a'
        .
          .someclass3{
            font-size:10px;
          }
      else
        .
          .someclass3{
            font-size:8px;
          }
      .
        .someclass4{
          font-size:10px;
        }
        .someclass5{
          font-size:10px;
        }