How render I get NOT-encoded view or use it as HTML::a() options?

The yii\helpers\Html::a()'s description for $options says (emphasis mine):

The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using encode(). (…)

How can I avoid that and force Yii 2 to not-encode values of these attributes?

I am generating a rich-text title to use Bootstraps 5’s Tooltip like that:

<?php $tooltip = (is_array($response) && count($response) > 0) ? [
    'data-bs-html' => 'true',
    'title' => HTML::decode($this->render('/command/_response', [
        'response' => $response,
        'options' => ['style' => "text-align: left"]
    ])),
] : []; ?>

It is then put as $options in a():

<td><?= Html::a($type' . ' (' . $status . ')', [
    '/command/view',
    'id' => $command->id
], $tooltip) ?></td>

Neither style="text-align: left" nor view’s internal <div class="rendered-command-response"> gets respected by browser, because it ends up encoded:

<a href="/root/app/web/controller/action?id=1" title="
&lt;div class=&quot;rendered-command-response&quot;&gt;
         &lt;div style=&quot;text-align: left&quot;&gt; 
                L1 = &lt;strong&gt;11.11&lt;/strong&gt; mm 
                L2 = &lt;strong&gt;22.22&lt;/strong&gt; mm 
                L3 = &lt;strong&gt;33.33&lt;/strong&gt; mm 
                P1 = &lt;strong&gt;32.1&lt;/strong&gt; mm 
                P2 = &lt;strong&gt;22.37&lt;/strong&gt; mm 
                P3 = &lt;strong&gt;12.34&lt;/strong&gt; mm 
         &lt;/div&gt; 
&lt;/div&gt;" data-bs-html="true">

How can I work-around this problem and get correct HTML content of Bootstrap 5’s tooltip?

I have similar code inside gridview column and it decodes it for me somewhere :slight_smile:

As a test, maybe surround whole ‘a’ tag with decode method?

<td><?= Html::decode(Html::a($type' . ' (' . $status . ')', [
    '/command/view',
    'id' => $command->id
], $tooltip)) ?></td>
1 Like

Your idea works fine in terms that nothing get decoded. But only trying it let me realize how completely false shot is that, what I am trying to achieve here.

The only thing that gets decoded is: "&quot;. And given the fact that I am _putting this content inside title="" element, I pretty much do want this to be encoded.

Otherwise the whole thing gets ruined, because DOM processor inside my browsers gets something like:

<a href="/command/view?id=1" data-bs-html="true" data-bs-original-title="
<div class="rendered-command-response">

     <div style="text-align: left"> 
...
<br /></div>">Link tekst</a>

And breaks completely on such thing.

So, it seems that Bootstrap 5 Tooltip supports HTML (rich content) inside title attribute, but only as long as this is simple HTML code. With elements having no attributes at all and not using quotes.