Elegant way to display HTML attributes with Antlers only when a variable is set ? #9323
-
Hello ! I am new to Antlers and I am trying to find a way to avoid this: <a href="{{ url }}" {{ if target }} target="{{ $target }}" {{ /if }}> I have seen the {{ svg src="hamburger" class="{wide ? 'w-full' : void}" }} Any advice? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Since the default value for the
|
Beta Was this translation helpful? Give feedback.
-
Thanx @jackmcdade! Sure but there is no way to disable the output of
but if there is no way, this is not a problem. We were just asking because we were quite sure would be possible, Antlers seems so nice and more powerful than blade. |
Beta Was this translation helpful? Give feedback.
-
There's no way to programmatically alter the
You may end up with an empty |
Beta Was this translation helpful? Give feedback.
There's no way to programmatically alter the
<a>
element from inside an attribute because you're simply doing server-side string manipulation. Antlers doesn't dynamically alter the DOM like JavaScript. You could write a customanchor
tag or something and then use void that way, or you can just use null coalescence like this to avoid having to write if/unless statements if you'd prefer.You may end up with an empty
rel
orclass
, but that's still valid and functional HTML.