Using a logic function with a material attribute

I am trying to make a cabinet dynamic component with two material settings: either it is painted white or it has a wood grain texture. If the wood grain texture is applied, the cabinet should generate two grains-- horizontal on the rail components and vertical on the stile components.

I have inserted a small group with a sample of each material (white, horizontal grain, vertical grain) into the model. My plan was to go individually into each nested component and set the material as a logic function. Something like =if(cabinet!woodmaterial=wood,vertical grain,white) so that I can assign each part as either horizontal or vertical that way. However, I keep getting an error that the material is not in the model despite it working if I just type in “vertical grain” for the material. Is there a workaround for this? Thank you.

In case someone has this same question, I figured it out. You can create unique attributes for each material in the parent component and set their definitions as the material. That way for each nested component you can define the material in terms of just the different attributes rather than using the actual material names. I hope that makes sense. This is definitely just a workaround, but I would still be interested if anyone else has a better method.