Confusion about toLayouts()

Hello everyone,
maybe someone can help me out here. I am running Kirby 3.9.0. In my template, I am using the functionality under

combined with the layout settings under

What I would be expecting now is that a line like
<section class="<?= $layout->class() ?>" id="<?= $layout->id() ?>">
would contain the class and id I entered for that layout.

What I get is
<section class="the_class_i_entered" id="c3b87039-4a2d-4a5f-a1cc-3c579eeaed49">
where the id is not the one I entered.

I’m pretty sure I made a mistake somewhere in there, but I can’t figure out what’s wrong.
A var_dump of $layout gives me

object(Kirby\Cms\Layout)#277 (6) { ["id":protected]=> string(36) "c3b87039-4a2d-4a5f-a1cc-3c579eeaed49" ["params":protected]=> array(6) { ["attrs"]=> array(3) { ["class"]=> string(10) "the_class_i_entered" ["id"]=> string(7) "the_id_i_entered"  ["image"]=> array(0) { } } ["columns"]=> array(2) { [0]=> array(3) { ["blocks"]=> array(1) { [0]=> array(4) { ["content"]=> array(1) { ["text"]=> …

(I cut that off after ["text"]=> )

I feel like I did this by the book, but apparently, I didn’t. Can anyone see where I went wrong?

Many thanks in advance!

You need to use ->attrs() method:

<?= $layout->attrs()->id() ?>
<?= $layout->attrs()->class() ?>
1 Like

And it works. :sunglasses: Thank you very much for your help!

I have one additional question. I know now how to write an ID into a snippet.
But what if I want to check for a particular ID?

When I var_dump($layout->attrs()->id()), I get

object(Kirby\Cms\Field)#327 (1) { ["id"]=> string(16) "the_id_i_entered" }

And when I compare that with a string, it will never be identical. Do I have to transform the string into an object for that, or is there an easier way?

You can transform the object into a string with ->value() instead.

1 Like

Thank you! I am glad that I can ask these types of questions and there is always someone here willing to help. I really appreciate that, and I hope I can give back some time soon.

1 Like