Language switcher on frontend

I want to have “-” between de FR - NL language switcher.
How can i do this?

<div class="language">
	<ul>
		<?php foreach($site->languages() as $language): ?>
		<li<?php e($site->language() == $language, ' class="active"') ?>> <a href="<?php echo $page->url($language->code()) ?>"><?php echo html($language->name()) ?></a> </li>
		<?php endforeach ?>
	</ul>
</div>

Here you go. I did this really quick, just adopt it to your needs. Cheers!

<div class="language">
    <ul>
    <?php
        $languages = $site->languages();

        $i = 0;
        $len = count($languages);

        foreach ( $languages as $language ) {

            echo '<li><a href="' . $page->url($language->code()) . '">' . html($language->name()) . '</a>' . e($i != $len - 1, ' - ') . '</li>';

            $i++;

        }
    ?>
    </ul>
</div>

Thx for the reply but when I test this, the “-” comes on the end. like this "FR NL-"
How can I resolve this?

Since the dash is just styling, you can use this CSS with your existing template code:

.language li:not(:last-child):after {
  content: ' - ';
}
1 Like

I agree with @lukasbestle on this.

BTW: The code above is not quite correct, because count($languages) will not work as expected, because $languages is an object, not an array, so the correct way to count the number of languages would be

$languages->count();

If you really wanted to this via php, you could do this:

<div class="language">
 <ul>
  <?php
    $languages = $site->languages();
    foreach ( $languages as $language ): ?>
      <li>
         <a href="<?= $page->url($language->code()) ?>"><?= html($language->name()) ?></a> <?php e($language != $languages->last(), ' - ') ?>
      </li>
    <?php endforeach ?>
  </ul>
</div>

That’s weird, I just tested this on my Kirby install and it appears correctly. You can try changing != to ==. But I would suggest you follow @lukasbestle way of doing it, it’s better than doing it PHP.

Oh, I wasn’t aware of that! Thanks @texnixe! Learning something new everyday :thumbsup: