If/Else condition in HTML attribute

Hey @avanti, I got an answer from a dev and it turns out the solution here is really simple: the docs were wrong. :upside_down_face: Up until a few minutes ago, this page about the syntax of L&L said:

To do this, replace the angle brackets that would usually surround a dynamic tag (like in the example below) with curly braces. Since these attributes usually contain a space, be sure to wrap them in quotes. Closed tags like <Field /> don’t need the slash / to close themselves when they’re inside an attribute.

It turns out this has been fine in 99.99% of cases I’ve seen on the forum, but it’s actually what’s causing the issue in your template. What’s happening in my example is that L&L’s parser sees {Url home} as an opening tag and then tries to look for a closing {/Url} tag. When it doesn’t find one, it assumes that everything after that tag is its children (nested inside it), which breaks the whole tree. When you leave the closing slashes on the tags, this works as expected:

<Set href_value>home</Set>

<a href="{If variable=href_value value=home}{Url home /}{Else /}{Url current /}{/If}">Click here.</a>

I guess what happened was that when Eliot first design the language and wrote the docs, he didn’t expect people to be writing a whole bunch of tags inside an attribute. The trouble is that L&L is so flexible that people like you and me did and (until now) it worked flawlessly! haha

So the moral of the story is that while it’s fine to omit the closing slash most of the time, doing so seems to cause issues when you’ve got lots of tags in your attribute.

I feel kinda bad because I’ve been telling people for over a year that they don’t need closing slashes because that’s what the docs say, but it turns out I was wrong this whole time. I’ve updated the docs to clarify this for now, but it would probably be more clear for everyone if I just combed through the docs and left the closing slashes everywhere just to avoid confusion and issues like this, so I’ll try to do that eventually.

Well done on finding this tiny but significant oversight in the docs! I guess we all learned something by looking into this.

2 Likes