I'm working on a english/spanish site that includes a language switcher link in the site footer.
The code for the switcher grabs the currently displayed entry, checks whether a corresponding entry exists in the other locale. If it does exist, the clicking the switcher will link to the corresponding entry. If not, you'll land on the other locale's homepage. If we're already on the homepage, just redirect to the homepage.
The code, simplified, looks like this:
{% if entry.sectionId == 1 %} // The homepage
// Redirect to homepage
{% else %}
// Do some checking into whether there's a corresponding entry, etc.
{% endif %}
The problem:
I have some pages on my site that aren't entries, and on these pages, I get a template error telling me that Variable "entry" does not exist
I tried rewriting this code so I'd check if that variable exists first:
{% if entry|length %}
// Do the previous code
{% endif %}
But this gives me the same error. How can I write this logic so I don't have to rely on the entry variable existing or not, which should presumably avoid the error.
Thanks!