Switching non-paired html tags

Hello,

I have a real special one again. :wink:

Let us asume I have following code:

<div class="blub>
  content
</>

Now I need a switch to control the div. If I have a special field the div should not appear. So I’m building this in “good old” manner of Custom Content Shortcode:

<If field=showit exists><div class="blub"></If>
  content
<If field=showit exists></div></If>

You get it - this does not work because the L&L-editor is telling me that tags must be paired.

How to fix this? I have absolutely no idea.

Regards,
Besim

So you want the content to show either way but only have the div if the condition is met?

Why not just use:

<If field=showit exists>
  <div class="blub">
    content
  </div>
<Else />
   content
</If>
2 Likes

Hello Matthew,

That would be obvious, but I have several loops in the content and would have to maintain that twice with your suggestion.

Regards,
Besim

Would it work if the div is there but has a different class? If so you can just set the class using a local variable, that would allow the template to pass the validation.

<If field=showit exists>
  <Set local=div_class>blub</Set>
<Else />
  <Set local=div_class>anotherclass</Set> 
</If>
  
<div class="{Get local=div_class}">
  content
</div>

I’m aware it’s not quite what you asked, but if the div is mainly for styling it would do the same job.

1 Like

Another thought - could you set the loop content as a local variable and then output that within the div (or not) depending on whether or not the field exists?

<Set local=loop_content>
  content
</Set>

<If field=showit exists>
  <div class="blub">
   <Get local=loop_content />
  </div>
<Else />
  <Get local=loop_content />
</If>

This seems more complicated than setting the class with the local variable if it’s just for styling, but if you can’t have the div at all if the field doesn’t exist then in theory this would be a way to do it (I gave it a quick test with a relatively simple loop and it worked fine).

2 Likes

Hello Matthew,

this would be a nice solution, I will try that (but logically it will works for sure).

Regards,
Besim

Or, if this doesn’t work because of some change in context between where the content is saved and called, you can use <Set template=loop_content> instead, and when you call it with <Get> the fields within will run in the context in which they’re called instead of the context in which they’re set