Sachin Nair
Sachin Nair

Reputation: 1

Condition in class

I am working on a project of automating sending emails based on html template from google sheet data using Appscript.

Below code worked for me

<td class="<?= r1 == "No" ? 'red-bg' : 'green-bg' ?>"><?= r1 ?></td> 

I want to add another color parameter. If r1 is equal to "Partial" turn to amber color.

Appreciate your help

Upvotes: 0

Views: 53

Answers (2)

Spyros Palaiokostas
Spyros Palaiokostas

Reputation: 367

The ?: operator is called the ternary operator. You can nest them but it is not very clear. For example:

<td class="<?=
  r1 == "No" ? 'red-bg' :
  r1 == "Partial" ? 'amber-bg' :
 'green-bg'
?>"><?= r1 ?></td> 

Otherwise, you can extract this logic to a function

Upvotes: 1

Shawn Northrop
Shawn Northrop

Reputation: 6036

I'm not sure if you can chain the conditional but you could try this:

<td class="<?= r1 == "No" ? 'red-bg' : r1 == "Partial" ? 'amber' : 'green-bg' ?>"

Upvotes: 0

Related Questions