Problem with reading content.txt YAML

I have added some content like this in my page txt file. It’s like subvalues to a parent.

Test:
  Value: my_value
  Another: 1

----

Test2:
  Value: test_value
  Another: 2

Is this correct? Now I try to get Value or Another from Test or Test2

print_r( yaml( $page->content()->get('test') ) );

This did not give me what I expected. Another is not a child of Value.

Array
(
    [Value] => Array
        (
            [Another] => 1
        )

)

How do I do this correctly? Change the txt or change my function call in my template, or both? How?

Update

I looked into this page and found the answer:

http://getkirby.com/blog/structured-field-content

It needs some more stuff, a “headline” before the “blocks”.

Title: Elsewhere
----
Profiles:

Twitter:
  Username: bastianallgeier
  Link:     http://twitter.com/bastianallgeier

If I use this method, then I can’t sort the pages by a value that is indented (subfield)? For example, can I sort pages by “Twitter Link” in this example?

I don’t think you need these subheaders, my structure fields usually look like this, e.g.

Testimonials: 


- 
  testimonial: some text
  source: >
    a_source

And these are sortable like this:

<?php foreach($testimonials->sortBy('source', 'asc') as $testimonial) : ?>

Then you sort on your testimonials, not your pages? Look at my original snippet. This is what I would like to do:

// Sort by an indented value
foreach( $page->children()->sortBy('test2', 'another', 'asc') as $item ) {
    echo $item->another();
}

I know above don’t work because sortBy does not take 3 params. I just try to show you my mind.

My current workaround is this:

Test_value: my_value

----

Test_another: 1

----

Test2_value: test_value

----

Test2_another: 2

It’s not awesome but it work. It generates more content to the txt file because of the prefixing thing.

// Untested but something like this would work
foreach( $page->children()->sortBy('test2_another', 'asc') as $item ) {
    echo $item->test2_another();
}

If there is a better way I would be happy to hear about it.

The reason for the nested array structure instead of a flat one is this time fortunately not plugin loading order (haha :smiley:) but the content file parser. The field content gets trimmed, so the leading whitespace before the first line is removed. This results in a relative indentation of the second line, so it becomes a child of the first line.
If you remove the indentation all together, it should work as expected.

1 Like

:smile: Very funny @lukasbestle

Yaml

Test:
Value: my_value
Another: 1

----

Test2:
Value: test_value
Another: 2

Template

print_r( yaml( $page->content()->get('test') ) );

Yes, that worked!

Childs cannot be used for filtering?

I also need the pages to be filtered by child fields, so I’m probably stuck with my workaround anyway, if there are no magic solution.

What I would like to write:

$page->children()->filterBy('test', 'another', '1');

Update

Or… maybe I could use a custom filter method here. At least i would be beautiful in my txt files.

Yes, I would use a custom collection filter for that. Nice and clean. :slight_smile:

1 Like