# Url() by given template

**URL:** <https://forum.getkirby.com/t/url-by-given-template/12857>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 13, 2019, 10:03am UTC](https://forum.getkirby.com/t/url-by-given-template/12857 "2019-02-13T10:03:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marksw](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marksw](https://forum.getkirby.com/u/marksw)\
**Post date:** [February 13, 2019, 10:03am UTC](https://forum.getkirby.com/t/url-by-given-template/12857/1 "2019-02-13T10:03:41Z")

</div>

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](https://getkirby.com/docs/cookbook/content-structure/one-pager)) 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?

---

<div class="post-metadata">

**Author:** ![marksw](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marksw](https://forum.getkirby.com/u/marksw)\
**Post date:** [February 13, 2019, 10:13am UTC](https://forum.getkirby.com/t/url-by-given-template/12857/2 "2019-02-13T10:13:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 13, 2019, 10:14am UTC](https://forum.getkirby.com/t/url-by-given-template/12857/3 "2019-02-13T10:14:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![marksw](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marksw](https://forum.getkirby.com/u/marksw)\
**Post date:** [February 13, 2019, 10:49am UTC](https://forum.getkirby.com/t/url-by-given-template/12857/4 "2019-02-13T10:49:48Z")

</div>

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()”

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 13, 2019, 11:03am UTC](https://forum.getkirby.com/t/url-by-given-template/12857/5 "2019-02-13T11:03:11Z")

</div>

> [@marksw](#):
>
> $page-\>template(‘cases’)

`$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?

---

<div class="post-metadata">

**Author:** ![marksw](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marksw](https://forum.getkirby.com/u/marksw)\
**Post date:** [February 13, 2019, 11:06am UTC](https://forum.getkirby.com/t/url-by-given-template/12857/6 "2019-02-13T11:06:35Z")

</div>

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

Yes that is exactly what i want! 🙂

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 13, 2019, 11:33am UTC](https://forum.getkirby.com/t/url-by-given-template/12857/7 "2019-02-13T11:33:43Z")

</div>

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):

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

```

---

<div class="post-metadata">

**Author:** ![marksw](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marksw](https://forum.getkirby.com/u/marksw)\
**Post date:** [February 13, 2019, 12:32pm UTC](https://forum.getkirby.com/t/url-by-given-template/12857/8 "2019-02-13T12:32:42Z")

</div>

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 🙂
