[SOLVED] If eq .Site.Language not working

Goodmorning everyone,

I’m having a problem understanding why the following code in my baseof.html isn’t working:

...
        {{- if eq .Site.Language "en" -}}
        <a href="{{ .Site.BaseURL }}">{{ .Site.BaseURL }}</a>
        {{- if .Param "showrss" }}<br><br><a href="/index.xml"><img src="/rss.svg" style="max-height:1.5em" alt="RSS Feed" title="Subscribe via RSS for updates."></a>{{ end }}
        {{- end -}}

        {{ if eq .Site.Language "it" }}
        <a href="https://lorenzocesana.xyz/it">https://lorenzocesana.xyz/it</a>
        {{- if .Param "showrss" }}<br><br><a href="/it/index.xml"><img src="/rss.svg" style="max-height:1.5em" alt="RSS Feed" title="Subscribe via RSS for updates."></a>{{ end }}
        {{ end }}
...

My scope here is: if the .Site.Language is “it”, then show the html that is between the if and the end; if is in “en” show that code instead.
I tried to print out the .Site.Language variable and is set as expected, “it” in the italian version and “en” in the english version.

As of now the output is blank: nothing at all (regarding the html between these two blocks) is displayed on the webpage.

I point out that the absence of “-” in the second statement is only because I was trying it out many combinations.

Thanks in advance

Think of .Site.Language as an object. You are comparing an object to a string.

Do this instead:

{{ if eq .Site.Language.Lang "en" }}
  ...
{{ end }}

With this site configuration…

[languages.en]
  languageCode = 'en-US'
  languageDirection = 'ltr'
  languageName = 'English'
  weight = 2

The methods on the Language object return:

.Site.Language.Lang               en (string)
.Site.Language.LanguageCode       en-US (string)
.Site.Language.LanguageDirection  ltr (string)
.Site.Language.LanguageName       English (string) 
.Site.Language.Weight             1 (int)      
2 Likes

All clear.
Now it works perfectly, many thanks!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.