How do I output structure content as Object?

The Problem is that some time ago I somehow managed to output the content of a Structure as a Object (via var_dump).
I noticed that the Value of this Object is what I was looking for and I had to check for
The result of me checking that Object for a value is below.

<?php if($page->leistungusp()->value != ""): ?>
    <div class="usp-s">
         <?php foreach(yaml($page->leistungusp()) as $usp): ?>
             <li><?php echo kirbytext($usp['text']); ?></li>
         <?php endforeach ?>
    </div>
<?php endif; ?>

But I simply cannot get it to work again that I output the object last last time again with var_dump.

There are two methods you can use to output a structure field, yaml() and toStructure()/structure(), the latter gives you a collection object.

<?php
$structure = $page->leistungusp()->structure();
var_dump($structure);
?>
<div class="usp-s">
    <?php foreach($structure as $usp): ?>
          <li><?php echo $usp->text()->kt(); ?></li>
    <?php endforeach ?>
</div>

https://getkirby.com/docs/cheatsheet/field-methods/toStructure
https://getkirby.com/docs/cookbook/the-structure-field

1 Like

Thank you very much but if I use your example sadly the server will try to allocate 84055704 bytes and therefor crash.

Which Kirby version are you using? And how many entries does your structure field have?

1 Like

I am using v2.4.1 and I have 2 entries in the structure (currently just a placeholder) which contains pure text

If i use yaml i will receive The following output. But as I stated i would need the Object of the “leistungusp” structure.

That’s weird… (and some more characters)

2 Likes

It is indeed… The really weird thing is that I know that somehow I managed to output it 2 weeks ago somehow.
But i’ve been strugeling for some time now to perform the same.

Well, the code I posted above I have used a hundred times and I don’t really understand why your server should run into a memory issue with only two entries.

Please check the memory_limit setting in your php.ini, maybe it is ridiculously low…

1 Like

What else is there in your template?

1 Like

Thanks! Deleting everything else helped. But I will get a object that includes the WHOLE Page not just the “leistungusp” structure is it supossed to be like that?

The output I want to recreate is pretty much something like this (not accurate representation):

 Object
 (
      [page] => technische-sauberkeit
      [text] => Sample text one
      [value] =>
 )

This is what a var_dump() returns in a Starterkit (limited to two entries):

object(Structure)[115]
  public 0 => 
    object(Structure)[131]
      public 'title' => 
        object(Field)[132]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'title' (length=5)
          public 'value' => string 'Contact' (length=7)
      public 'text' => 
        object(Field)[133]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'text' (length=4)
          public 'value' => string 'Feel free to drop us a line if you have any questions.' (length=54)
      public 'url' => 
        object(Field)[134]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'url' (length=3)
          public 'value' => string 'mailto:support@getkirby.com' (length=27)
      public 'linktext' => 
        object(Field)[135]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'linktext' (length=8)
          public 'value' => string 'Write an email' (length=14)
      public 'icon' => 
        object(Field)[136]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'icon' (length=4)
          public 'value' => string 'contact.svg' (length=11)
  public 1 => 
    object(Structure)[137]
      public 'title' => 
        object(Field)[138]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'title' (length=5)
          public 'value' => string 'Forum' (length=5)
      public 'text' => 
        object(Field)[139]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'text' (length=4)
          public 'value' => string 'Join the Kirby community and connect with others.' (length=49)
      public 'url' => 
        object(Field)[140]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'url' (length=3)
          public 'value' => string 'https://forum.getkirby.com/' (length=27)
      public 'linktext' => 
        object(Field)[141]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'linktext' (length=8)
          public 'value' => string 'Visit Kirby’s forum' (length=21)
      public 'icon' => 
        object(Field)[142]
          public 'page' => string 'contact' (length=7)
          public 'key' => string 'icon' (length=4)
          public 'value' => string 'forum.svg' (length=9)

But this has 5 fields… If you use dump() instead of var_dump() you get some cleaner output.

For me it will not just output the object but rather all information about the whole $page
The used Code is attached

<?php
    $structure = $page->leistungusp()->structure();
    var_dump($structure);
 ?>

Now that I think about it, you are then probably on a PHP version below 5.6… if you update, you will get more concise output.

Kirby objects such as $page, $site or $file have a lot of connections to other objects. The result is that a lot of stuff is printed when debugging those objects with var_dump(), print_r() or dump().
Starting with Kirby 2.4, the most complex objects are now printed with their most important information only. Please note that this new feature only works with PHP 5.6 or later.
https://getkirby.com/changelog/kirby-2-4-0

You are right i use 5.4.

What is weird that I did manage to output just that object 2 weeks ago. Maybe the system Admin did a test for > PHP 5.6 for a short time but i highly doubt it…

I’d recommend updating in any case, PHP 5.4 is no longer supported and is therefore a security risk. If you can, update to PHP 7.

1 Like

Yes we currently are preparing for the update to PHP7.
So that will happen anyway, i just found it weird, that somehow this worked for me some time back. But I’ll just leave it as it is for now.

Thanks for all of your help.

Unfortunately, my time machine is out of order… :wink:

1 Like