So I assume what you mean is that the value of each field (for example <Field foo />
) could be either ✗ or ✓ and based on the value of that field, you want to change the class of the surrounding div. Is that right? Performance-wise I don’t think there would be any benefit to simplifying this since you’d need to run the conditional logic each time anyway. I’ve tried a bunch of approaches and while I’m convinced it would be possible to save all your fields (foo,bar,foobar,etc
) to a list and then somehow loop through the list for each ACF group, I think it would probably involve a lot of repetitive setting of variables and would end up being inefficient. Maybe there’s a clean way, but I’ve spent a bit thinking about it anything is jumping out at me.
Edit: actually, this just came to me, but I’m not sure if there’s a performance tradeoff to doing it this way. I also haven’t tested this so you might have to troubleshoot my template. You could create a separate template (lets say the template is called group-item
) with something like this that has conditional logic that works based on a local variable I’ve decided to call field_variable
:
<If field="{Get local=field_variable}" is value="✓">
<Set item_class>check</Set>
<Else />
<Set item_class>cross</Set>
</If>
<div class="field {Get item_class}">
<Field name="{Get local=field_variable}" />
</div>
And then inside the loop in the template you shared earlier, you could just reference that template and set the field_variable
local variable for each field name, like this:
<Loop acf_group="basic">
<Template name="group-item" field_variable="foo" />
<Template name="group-item" field_variable="bar" />
<Template name="group-item" field_variable="foobar" />
</Loop>
That’s less repetitive than the 30+ If
tags you initially suggested, but it might also not perform as well so you’ll want to use the Timer tag to compare approaches and see which one load faster. This would be even cleaner if instead of having to create a second L&L template you could just use a template variable, but I don’t think template variables allow you to pass local variables within them unfortunately.