Page->create() does not return object with correct template

a ‘dashbox’ is a child (=subpage) of ‘dashboard’.
both template and blueprint files exist and are valid yml.
kirby version is 2.3.0.

i create a new page with an template like this…

// $dashboard is root folder to create stuff.
$newUriForDashbox = $dashboard->uri() . DS . $newUsername;
$dashbox = $dashboard->create($newUriForDashbox, 'dashbox', [
    'user' => $user->username(), 
    'title' => $newUsername,
    'erstelltam' => date('c'),
    'erstelltvon' => $user->username(),
    'erstelltvisitor' => visitor::ip().' '.visitor::userAgent(),
    'finalisiert' => false
]);
echo $dashbox->template(); // = default, but dashbox was expected

the file is created correctly, path and filename (matching template name).
but the returned object is not the one created, but an ‘default’ page.

when i get the object manually again after creating like above all is well.

$dashbox = $pages->find($newUriForDashbox);
echo $dashbox->template(); // = dashbox
echo $dashbox->intendedTemplate(); // = content of $newUsername (=foldername)

whats going wrong when calling create()?

I think you should use the $pages->create() method instead of the static method $page->create():

$dashbord = page('dashboard');
$dashbox = $dashboard->children()->create($newUsername, 'dashbox', [
    'user' => $user->username(), 
    'title' => $newUsername,
    'erstelltam' => date('c'),
    'erstelltvon' => $user->username(),
    'erstelltvisitor' => visitor::ip().' '.visitor::userAgent(),
    'finalisiert' => false
]);
```

resulting dashbox is still of template default.

Yeah, you are right, this seems really weird. I just tested this and found that with one template I used, the correct template was shown, but with all other templates it failed, showing “default” instead.

While $dashboard->intendedTemplate() shows the uid, which is not correct either.

1 Like

i have 41 blueprints/templates. could this be an issue? like memorywise?

most of my templates have a ‘-’ (minus) within their filename.

maybe kirby->registry is somehow broken by my code? https://github.com/getkirby/kirby/blob/master/core/page.php#L1009

I don’t think so, I tested this with about 12 - 18 blueprints/templates, none of which had any special characters in their filenames. But maybe we should test with a fresh starterkit first.

i think i encountered something similar before. i did forget to create the template file and only had the blueprint. then the create function did not work either. could it be that i have a missing (check, seems not) or broken template/blueprint? my buleprints are (almost) valid yaml just check again. some contain a regex passage like this which is NOT (edited) accepted by yaml validators.

telefon:
  width: 1/3
  label: Telefon
  type: tel
  required: true
  help: Bitte geben Sie Telefon und Faxnummern komplett mit Vorwahl, jedoch ohne Sonderzeichen wie z.B. Klammern oder Punkte ein.
  validate:
    match: "/^\d[\d ]+\d$/"

I just tested this in a fresh starterkit with the available blueprint/template pairs and it worked perfectly, so the question is why it does not work in the other cases, in the other project where I tested this first, all blueprint/template pairs were available as well and nothing special in yaml.

Have you tried without the validate option?

it did work before with the vaidate. today i just added two new templates/blueprints (with no regex) and now its broken. i checked my repo not much else changed. does kirby cache this ‘registry’ between session?

why does getting the template directly after creating it work with find() but not when using the page() function? page() is used for the return value of create(), right?

 // HERE: create page omitted then...
 $dashbox = kirby()->site()->page($newUriForDashbox);
 echo '1:'.$dashbox->template(); // = default, which is wrong. should be 'dashbox'
 $dashbox = $pages->find($newUriForDashbox);
 echo '2:'.$dashbox->template(); // = dashbox, which is right

But you said above, that even then the $page->intendedTemplate() method does not output the intended template but the page uid? While it should output the name of the text file, not the foldername.

I think the problem must be something else, as it all works perfectly as it should with a fresh starterkit. Only I have no idea at the moment, what could be causing this issue. Maybe @lukasbestle to the rescue?

mabe because of this?

becauses its called here:

No, this is the page method:

/**
   * Returns the name of the content text file / intended template
   * So even if there's no such template it will return the intended name.
   *
   * @return string
   */
  public function intendedTemplate() {
    if(isset($this->cache['intendedTemplate'])) return $this->cache['intendedTemplate'];
    return $this->cache['intendedTemplate'] = $this->content()->exists() ? $this->content()->name() : 'default';
  }

And the text files do get named correctly …

using children()->create() calls static page::create, right?

about the intendedTemplate, maybe cache need refresh after page create?