Adding php code inside a class attribute

I know hugo is a static site generator but it does have some support for php code. So far I am able to add php codes in the template like

{{ safeHTML <?= $isTrue ? 'highlight' : "" ?> }}

however, when i put the same code inside the class attribute like

Hello

I get this

Hello

I tried safeHTML, safeHTMLAttr, safeCSS and even tried safeJS just to see what happens but they all failed like the one described above.

I’ve already ran out of ideas I hope to find new ones here

TIA

What you’re trying to do is an unsafe operation. Go Templates will not allow you to output a PHP script in a HTML class attribute and it’s for a good reason.

But why are you trying to do the above? Maybe there is another way…

I know for a fact that hugo is for generating static pages but since it provided a way to output php too I am using it to output both php and html files. And while using it for such I encountered this need.

Ok. I see. But as I said in my reply above: there is no way around it in Go Templates.

But then again calling PHP scripts from within HTML attributes sounds like an unholy invocation to !Z͙A̮̥̳̹͉̙L͕̝̦̭̹ͅGO̭̲̩̘͝!̭̩̲͚̀ ̶̰̲̳!͏!

Surely there must be another way to achieve what you seek, without risking the destruction of us all. :wink:

That’s a bit disappointing. But I did find a way around it. It’s not pretty but it works.

For those who face the same issue this is my solution.

{{ safeHTML  `<?php if ($isTrue) : ?>`  }} 
<div class="highlight">
{{ safeHTML `<?php else: ?>` }}
<div>
{{ safeHTML `<?php endif; ?>` }}
...
</div>

I’m still hoping for a better solution to come along soon :slight_smile: