Url() by given template

Hey guys, i’m trying to figure out if something like

<?= $site->children()->template('cases')->url()?>

works? In this case it doesn’t work. I build a one pager (One-pager | Kirby CMS) and i want to be sure if someone changes the url or name of a page that the snippets are shown correctly. Because nobody can change the template for the snippets it is the safest option but every snippet is shown by its section id (<section id="cases">) so it has to be dynamically.

Is there a solution?

<?= $site->children()->url()?> also doesn't work

A collection doesn’t have a URL.

It is best if you use something that can’t be changed for the snippets, e.g. the intended template rather than the UID. Or a name in a field that can’t be changed.

hey thanks! yeah as i wrote, i tried to stick with the intended template but i won’t let me access the page url.

Tried

<?php foreach($site->pages() as $page): ?>
<?php if($intendedtemplate = $page->template('cases')): ?>
<?= $intendedtemplate->url() ?>
<?php endif ?>
<?php endforeach ?>

just gets me a “Call to undefined method Kirby\Cms\Template::url()”

$page->template() doesn’t take an argument and it only returns a string, i.e. the name of the template. I’m not sure what you are trying to do here. A page object has an url method, or a file.

Do you want to echo the URL of a page with a given template?

Ay sorry, thought it would work like it works with $files.

Yes that is exactly what i want! :slight_smile:

The easiest way is the to filter by template, then loop through the collection (instead of looping through all and then use an if statement to check for the template):

<?php foreach($site->pages()->template('cases') as $page): ?>
  <?= $page->url() ?>
<?php endforeach ?>

Okay thank you very much! Got it to work right now. I changed the ID from uid to template and your foreach loop helps me when i use href on a button e.g. from main page snippet cases.php where only 6 cases are shown to template cases.php where all cases are shown. Again, thank you very much! Saved my life :slight_smile: