I’ve been playing with yiisoft/html and it seems the order you write the following works/doesn’t work as expected
Does the documentation point out that the order you write these matters? I am using this inside of a WordPress project, sorry I haven’t made a bare minimum example that excludes everything else.
Div::tag()
->id('my-id')
->class('my-class')
->attributes(['tabindex' => '-1'])
->content('My content')
->render();
Outputs:
<div tabindex="-1">My content</div>
Whereas this
Div::tag()
->id('my-id')
->attributes(['tabindex' => '-1'])
->class('my-class')
->content('My content')
->render();
Outputs:
<div class="my-class" tabindex="-1">My content</div>
And lastly:
Div::tag()
->attributes(['tabindex' => '-1'])
->id('my-id')
->class('my-class')
->content('My content')
->render();
Outputs:
<div id="my-id" class="my-class" tabindex="-1">My content</div>
All I have in my composer.json is.
{
"require": {
"yiisoft/html": "^3.11"
}
}
I did a minimum example.
composer.json.
{
"require": {
"yiisoft/html": "^3.11"
}
}
index.php
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
use Yiisoft\Html\Tag\Div;
class MyHelpers {
public static function MyData() {
$a = Div::tag()
->id('my-id')
->class('my-class')
->attributes(['tabindex' => '-1'])
->content('My content')
->render();
$b = Div::tag()
->id('my-id')
->attributes(['tabindex' => '-1'])
->class('my-class')
->content('My content')
->render();
$c = Div::tag()
->attributes(['tabindex' => '-1'])
->id('my-id')
->class('my-class')
->content('My content')
->render();
return $a . "\n" . $b . "\n" . $c;
}
}
echo MyHelpers::MyData();
Outputs the following:
<div tabindex="-1">My content</div>
<div class="my-class" tabindex="-1">My content</div>
<div id="my-id" class="my-class" tabindex="-1">My content</div>
The issue was, I was using attributes() and not addAttributes().
This behavior is expected and comes from the difference between attributes() and addAttributes() in yiisoft/html
attributes() → replaces all previously set attributes
addAttributes()→ merges attributes with existing ones