cms icon indicating copy to clipboard operation
cms copied to clipboard

Children tag pair doesn't return as expected

Open wesort opened this issue 2 years ago • 5 comments

Bug description

To output and loop through child page(s) data when on a parent page.

Have tried to template in a number of ways but nothing . I've seen {{ children }} referenced in the Nav docs but I've not found {{ children }} mentioned explicitly in the way the {{ parent }} tag is.

Using {{ dump }} I see: image

? Related to #5554 ? Related to #5565

How to reproduce

  1. Be on a parent page (example: id: 64ab5c1b-0e3c-42f1-8692-96fabdb5a0dd)
  2. Have templating that outputs any of the child page data
  3. Output to have child id (example: 8afc0251-58f1-4e12-983d-34248504ab8d)

Content (tree)

tree:
  -
    entry: 45158ec1-b72e-4cec-8604-d66cd1400d9d
  -
    entry: 64ab5c1b-0e3c-42f1-8692-96fabdb5a0dd
    children:
      -
        entry: 8afc0251-58f1-4e12-983d-34248504ab8d

[Attempted] Templating NB: I'd like all the child data, but for simplicity here I'm just reaching for id

{{ children }}
  {{ id }}
{{ children }}

or

{{ nav :from="id" }}
  {{ children }}
    <div>children: {{ id }}</div>
  {{ /children }}
{{ /nav }}

or

{{ page:children }}
  <div>children: {{ id }}</div>
{{ /page:children }}

Logs

No response

Versions

Statamic 3.3.1 Pro Laravel 9.5.1 PHP 8.0.16 aryehraber/statamic-uuid 2.1.0 doublethreedigital/duplicator 2.1.1

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

wesort avatar Mar 18 '22 17:03 wesort

Yep just going through this myself... Screen Shot 2022-03-25 at 5 46 38 pm

It's right there, yet I can't figure out how to access it.

sjclark avatar Mar 25 '22 06:03 sjclark

@sjclark - I had a similar problem and made a generic children tag: you can access the children of a node (entry, ..) with

$node->pages()

So for example:

<?php

namespace App\Tags;

use Statamic\Tags\Tags;

class Children extends Tags
{
    public function index(): array
    {
        $node = $this->params->get('node');
        if (!$node || !$node->id || !($pages = $node->pages())) {
            return [];
        }
        $items = collect();
        $pages->all()->each(function ($child) use (&$items) {
            if (!($entry = $child->entry())) {
                return;
            }
            if (!$entry || !$entry->published) {
                return;
            }
            $items->push($entry);
        });

        return $items->toArray();
    }
}

I use this tag with

{{ children :node="someEntry" }}
  {{ unless no_results }}
    <li>{{ title }}</li>
  {{ /unless }}
{{ /children }}

naabster avatar May 31 '22 20:05 naabster

Statamic 3.3.1 Pro

@wesort have you tried on the latest version?

edalzell avatar May 31 '22 20:05 edalzell

Are you guys using the new runtime parser? Might be related to https://github.com/statamic/cms/issues/5565#issuecomment-1142086035

Tried using {{ %children }}?

joshuablum avatar May 31 '22 20:05 joshuablum

Are you guys using the new runtime parser? Might be related to #5565 (comment)

Tried using {{ %children }}?

I get (either pair or single)

Statamic\Tags\TagNotFoundException
Could not find files to load the `children` tag. 

sjclark avatar Aug 19 '22 08:08 sjclark

😂 This hilarity works

{{nav_test = 0}}
{{ nav :from="url" max_depth="1" as="nav_var" }}
   {{ nav_test = (nav_var | count) }}
{{ /nav }}
{{ if nav_test > 0 }}
   Show Subnav
{{/if}}

sjclark avatar Mar 31 '23 03:03 sjclark

any traction on this issue? Still using the nav hack to get around this for now

ozmos avatar Sep 06 '23 06:09 ozmos

{{nav_test = 0}} {{ nav :from="url" max_depth="1" as="nav_var" }} {{ nav_test = (nav_var | count) }} {{ /nav }} {{ if nav_test > 0 }} Show Subnav {{/if}}

@sjclark Bro. This actually helped me trying to figure out some logic for a more complicated sub-nav where {{ children }} wasn't going to get the job done 😅 Thanks!

rhettparkinson avatar May 15 '24 12:05 rhettparkinson

@sjclark Bro. This actually helped me trying to figure out some logic for a more complicated sub-nav where {{ children }} wasn't going to get the job done 😅 Thanks!

😂 likely better ways to do it these days, but glad it helped regardless

sjclark avatar May 15 '24 12:05 sjclark