I'm trying to render different colors in tailwind for a category button on blog cards using if/elseif statements. I'm really sure if this is the best way to do this so if anyone has a better solution I'm all ears.
However, my main question is this: the category slug and category name are being rendered correctly but the colors for the buttons are not. Why might this be?
The issue in the code is that the assignment operator = is being used instead of the comparison operator == in the if/elseif statements. The assignment operator will always return true, so the first if statement will always be executed.
To fix this, you need to change the assignment operator = to the comparison operator == in each if/elseif statement.
By using the comparison operator ==, the if/elseif statements will correctly compare the category ID with the specified values and render the appropriate colors for the buttons.
i would create an accessor on the model that returns a string to be used in the class statement based on the postCategory
But you will need to whitelist the colours in tailwind config
Also categories of 1,2,3,4 is a code smell. Who apart from you knows what these numbers mean?
If these are IDs from a database table then they could easily be 5,6,7,8 if you had some small hiccup creating the database table