Linking to invisible page - best practice question


#1

Hi everyone,

I’m new here and just started to develop my first project with kirby. The website ist bilingual, in German and French. In the footer I have links to two invisible pages, the legal stuff (Impressum and Datenschutz in German). For the footer I’ve created a snippet, and to display the links in the correct language I use this construct, which check for the active language and then spits out the German or French version of these two links:

<?php 
          if ($site->language()->code() == 'de') {
            echo "<i class='fas fa-angle-right'></i> <a class='navbar-item' href='" . $page->site()->page('impressum')->url() . "'>" . $page->site()->page('impressum')->title() . "</a><br>";
            echo "<i class='fas fa-angle-right'></i> <a class='navbar-item' href='" . $page->site()->page('datenschutz')->url() . "'>" . $page->site()->page('datenschutz')->title() . "</a>";
        
          }
          if ($site->language()->code() == 'fr') {
            echo "<i class='fas fa-angle-right'></i> <a class='navbar-item' href='" . $page->site()->page('impressum')->url() . "'>" . $page->site()->page('impressum')->title() . "</a><br>";
            echo "<i class='fas fa-angle-right'></i> <a class='navbar-item' href='" . $page->site()->page('datenschutz')->url() . "'>" . $page->site()->page('datenschutz')->title() . "</a>";   
          }
        ?>

What I do is fetching the URL and Title in the context of the active language and construct the links to them.

This solution works, but I would like to know if there is a more elegant or simple way to achieve the same, in the sense of a best practices approach?

Advice is welcome, thanks!

René


#2

Yes there is… you should really use a language switcher, and language strings for things you can’t translate via the blueprints - this is useful for things like form error messages. Kirby will take care of the correct page links then, without the need for all that. Echoing html isn’t really best practice :slight_smile:

This page from the cookbook is also useful.


#3

The correct language is automatically picked up, no matter if you use a language switch navigation or not.

Your code is a bit complicated though. you can get a specific page like this

<?php if($imprint = page('impressum')): ?>
<i class="fas fa-angle-right"></i><a class="navbar-item" href="<?= $imprint->url() ?>"><?=  $imprint->title() ?></a>
<?php endif ?>

This includes a check if the page exists, so you don’t run into issues if it is ever removed or renamed.


#4

Thanks, for your reply.

Actually I did use the language switcher for my main navigation. But how do I use it for invisible pages?


#5

What do you mean, for your main navigation? What type of language switch do you use? It shouldn’t matter if the page is invisible or not, but I don’t know your code.


#6

Thanks texnixe, your code snippet works and is more elegant than mine, exactly what I was looking for!

Don’t mind about the main navigation, that one works fine and already gives me the links in the correct language, without complicated code, using the method described in the docs. It was meant as an answer to Jim.

I was a bit stuck with referencing invisible pages, and didn’t came accross $imprint in the docs.


#7

$imprint is just a made-up variable name, you can name it anything you like. I just like variable names that tell me what’s in them.

Apart from that , as far as fetching visible and invisible pages is concerned, there is no difference between these pages. It’s just a flag that helps to filter pages.

If you have more pages that you want to fetch, it might make sense to store them in an array and then loop through that:

<?php
$footerPages = ['impressum', 'datenschutz'];

foreach($footerPages as $p):
  if($p = page($p): ?>
  <i class="fas fa-angle-right"></i><a class="navbar-item" href="<?= $p->url() ?>"><?=  $p->title() ?></a>
  <?php endif ?>
<?php endforeach ?>

#8

Oh, now I see what you did, I can access pages just by page('nameofpage'). Cool, thanks!


#9

I’ve added some more information in my last post.


#10

I get a parse error with your code, seems there is nothing in the $p variable from the foreach loop.


#11

Ah, a missing closing parenthesis, one of my favourites, this line should be:

  if($p = page($p)): ?>

#12

Oh, and I’m good not catching missing parenthesis, even when checking a line of code ten times. Thanks for you help!