Not getting a If statement checking an ACF checkbox to work

<Loop type="stenforetag" orderby_field="pnr">
<If type="stenforetag" field="leverantorer_byggsten" exists>
    <p><Field image size=thumbnail />
    <strong><a href="{Field url}"><Field title /></a></strong>, <Field pnr />, <Field ort /></p>
    <hr style="border-top: dotted 1px;" />
</If>
</Loop>```

Only the companies with the checkbox "leverantorer_byggsten" checked should be displayed, now i get all companies. have also used "acf_checkbox" instead of just "field". 

What am i missing?

Hi Magnus, welcome to the forum!

Just like in HTML, each tag has specific attributes that work with it. For example, you couldn’t take the href attribute and put it inside an img tag, because href is only a valid attribute when you use it inside the a tag.

It’s the same thing with L&L. In this case, you’ve used the type attribute inside the If tag, which isn’t a valid attribute of that tag. You may want to give the If tag documentation another read to see what attributes work with that tag. You may also want to give this part of the docs a read about the different ways you can filter a loop and why you might choose one over the other. To get you started, here’s a possible way you could write your Loop tag to make things more efficient (you wouldn’t need the nested If tag), but you’ll want to read the docs to make sure you understand what’s going on:

<Loop type=stenforetag orderby_field=pnr custom_field=leverantorer_byggsten custom_field_compare=not custom_field_value="">

The other thing you might want to check is what your ACF leverantorer_byggsten checkbox field is actually outputting in its different states. You’re currently using this logic:

<If field="leverantorer_byggsten" exists>

But even if your checkbox is unchecked, the field will still exist so the inner content of the If tag will be rendered. You’ll need to play around with that or look into your ACF settings to see how you’ve set that up, but if I remember correctly, lets say your checkbox item is called “Checked” then that’ll be the value returned in that field when the box is checked, so your logic should maybe be something more like:

<If field="leverantorer_byggsten" is value="Checked">

You’ll need to change “Checked” to whatever your checkbox item is called. Alternatively, you could do something like this to make the If statement true when your checkbox field isn’t empty:

<If field="leverantorer_byggsten" is_not value="">
<hr style="border-top: dotted 1px;" />
<Loop type="stenforetag" orderby_field="pnr">
  <If field="{Get local=stentyp}" is value="1" >
    <p><strong><a href="{Field url}"><Field title /></a></strong>, <Field pnr />, <Field ort /></p>
    <hr style="border-top: dotted 1px;" />
  </If>
</Loop>

Hello Benjamin! Solved it. Also added an input value so I only need one version of this script.
One thing this plug-in is very finicky about is where Field is capitalised or not.

Here is the result. Stenleverantörer – Byggsten - STEN Sveriges Stenindustriförbund So exciting! :smiley:

(There are some stuff left to fix on the sire after a total crash the other day.)

That’s awesome, so glad to hear you figured it out! It seems like you’ve decided to go in a different direction where you’re using some kind of local variable in your conditional logic. It’s not clear from your template how that’s working but I’m glad to know you’ve got it working! Exciting indeed :smiley:

That’s just down to the syntax of L&L, it’s built in such a way that it expects all dynamic tags to be capitalized. I should clarify that it’s not really finicky though: dynamic tags are always capitalized (that’s unique to L&L), whereas attributes are never capitalized (that’s not something that’s unique to L&L, it’s the same way in HTML). So when you write <Field title /> it needs to be capitalized because Field is a dynamic tag. If you write <If field=some_field_name is value="some value"> then in that case field (just like is and value) is just an attribute of the If tag and isn’t capitalized because it’s not a tag. Tags and attributes are completely different things, and only L&L tags ever need to be capitalized.

As far as why tags are always capitalized in L&L, I think that was probably done partially to easily differentiate L&L tags from HTML tags and partially to avoid any potential conflict between them as the language expands in the future. Looking into it, it seems that xHTML explicitly required tags to be lowercase but then regular HTML doesn’t actually care whether a tag has upper or lowercase letters. I’m not privy to the design thought that went into building the syntax of the language the way it is but I’m sure there was a good reason! You’re right that it would probably be technically possible to make L&L less picky about whether its dynamic tags are capitalized or not, but I think forcing dynamic tags to start with capital letters is a good practice to make it so that if someone is skimming your markup and they see a tag they don’t recognize, they’ll know immediately based on its capitalization whether that tag comes from L&L or from HTML.

Thanks for the feedback though, I’m always looking for ways to make the documentation and explanations clearer so I’ve made a note of your confusion about the syntax to try to improve the docs so that’s clearer.

1 Like