Skip to content

Elegant way to display HTML attributes with Antlers only when a variable is set ? #9323

Answered by jackmcdade
potsky asked this question in Q&A
Discussion options

You must be logged in to vote

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 custom anchor 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.

<a href="{{ url }}" target="{{ $target || '_self' }}" rel="{{ $rel }} class="{{ $icon ?= "icon-" + $icon }}">

You may end up with an empty rel or class, but that's still valid and functional HTML.

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@potsky
Comment options

Answer selected by potsky
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants