Search code examples
twiggrav

Grav Page Routing in Twig Template in Multilanguage Setup


Problem

Using Grav CMS (v1.5.8) I want to redirect the user to other pages. The page is a multilanguage setup and depending on the selected language, the route to the the base / start pages for the languages are simply:

www.example.com/

and

www.example.com/en

From a page, let's say error, I want to direct the user back to this start page. I realised this using the following code in the twig template:

<a href="{{ uri.base }}/{{ grav.language.getLanguage }}" class="btn">

which generates the correct url including the language string maintaining the current language.

Question

Is there any Grav API that handles generating / getting the root including the current language in a short way?


Solution

  • I figured that the redirect in a multi-language environment works if you use simply

    page.find('<the page you want to direct to>').url

    So in case of my question, redirecting to home would be

    <a href="{{ page.find('home') }}" class="btn">

    which resolves with the current language string.