Get representation of page

Hi!

Is it possible to get the content representation of a page within the template? Like $page->representation()?

I’m using content representations to display content in different ways depending on whether the user is viewing (/my-page) or editing (my-page.admin) the page. I use very distinct code for the main part of the page, but for things like menu things don’t change across representations so I rely on snippets. Within those snippets, very small things do change, like adding “(editing)” next to the title. I don’t want to have to duplicate the entire snippet for each representation. It would be nice to be able to do:

if($page->representation() == 'admin') {
  echo '(editing)';
}

Thanks!

This is not necessary, you can pass variables to your snippet. So when calling the snippet in the “admin” representation, you can call your snippet like this:

snippet('whatever', ['showEditButton' => true])

In the snippet

$showEditButton = isset($showEditButton) ? $showEditButton : false;
if($showEditButton) {
  echo '(editing)';
}

Thanks @texnixe! That’s actually how I’ve been doing it until now, but this new site is larger: ~10 templates * 3 representations = ~30 places where I have to manually pass, at least, a $representation var. It’s not the end of the world, of course, but I was hoping there would be a way to get that value more elegantly.

It’s a bit of a different question, but to generate URLs I’m facing a similar challenge in that I have to build them manually like:

<?= $page->url() ?>.admin

Instead of something like:

<?= $page->url('admin') ?>

I always find it quite elegant to use custom page methods for stuff like that. This is how a $page->representationType() method could look like:

// (untested)
'representationType' => function () {
  $url = kirby()->request()->url();
  return F::extension($url);
}

Keep in mind representation() is already taken and it’s a returning the representation itself, not checking for the type. Same goes for the url() method. It expects a language string, not an appendix for the url. But of course it’s possible to write a custom page method for this use case, too.

Thanks @thguenther! I’ve tested your code and it works. :slightly_smiling_face: