Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

404 page doesn't get lang variable #1281

Closed
behnam opened this issue Dec 30, 2020 · 3 comments
Closed

404 page doesn't get lang variable #1281

behnam opened this issue Dec 30, 2020 · 3 comments

Comments

@behnam
Copy link

behnam commented Dec 30, 2020

Feature Request

Environment

Zola version: zola 0.12.2

Expected Behavior

Would be great to have config.default_language passed to the 404.html template as lang variable, so that the 404 page can re-use the language-enabled base template without too much extra work.

The rational for setting the value as such would be that, in the absence of actual content info, the site would fall back to the "default language".

Additionally, later, we could have the lang variable to be set for 404 pages depending on the language-specific part of the URL. For example, if a site has both en and hi enabled, then 404 pages for urls starting with /en/ would get lang en, while those for urls starting with /hi/ getting hi as the lang.

Current Behavior

Failed to build the site
Error: Failed to render '404.html' (error happened in 'base.html').
Reason: Variable `lang` not found in context while rendering '404.html'

Step to reproduce

On templates/404.html:

{% extends "base.html" %}

{% block content %}
Page not found.
{% endblock content %}

On templates/base.html:

<!DOCTYPE html>
<html lang="{{ lang }}">
...
</html>

Alternate Solutions

An alternate solution would be to "set" the value of lang variable on the extends call, like:

{% extends "base.html" lang=config.default_language %}

However, I believe Tera doesn't support that at the moment.

I you think this would be a better solution, we can file an issue on https://github.com/Keats/tera .

Wdyt?

@Keats
Copy link
Collaborator

Keats commented Dec 31, 2020

I think passing config.default_language as lang for the 404 is probably fine? It just need to be documented.
In the meantime you can do {{ lang | default(value=config.default_language) }} in your template.

@behnam
Copy link
Author

behnam commented Dec 31, 2020

Thanks, @Keats. Sounds good.

I'm using this for now, on top of the base template:

{% set lang = lang | default(value=config.default_language) %}

Btw, I just realized we have this in the docs: https://www.getzola.org/documentation/templates/overview/

A few variables are available on all templates except feeds and the sitemap:

  • config: the configuration without any modifications
  • current_path: the path (full URL without base_url) of the current page, always starting with a /
  • current_url: the full URL for the current page
  • lang: the language for the current page

Meaning, we should either add 404 to the list of exceptions here, or make sure 404 also gets all these variables, including lang.

@Keats
Copy link
Collaborator

Keats commented Dec 31, 2020

Hmm I need to think about it, i'm leaning toward setting lang == default_language

@Keats Keats closed this as completed in 779a15a Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants