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

Allow for conditionals in templates #2016

Closed
mgeisler opened this issue Feb 10, 2023 · 2 comments
Closed

Allow for conditionals in templates #2016

mgeisler opened this issue Feb 10, 2023 · 2 comments
Labels
C-enhancement Category: Enhancement or feature request

Comments

@mgeisler
Copy link
Contributor

Problem

As far I can understand, the handlebars template system only allows for if-statements that evaluate a single variable. This limitation is on purpose to encourage people to put logic in the language serving the pages.

However, that also (implicitly) assumes that users have an easy way to change the backend. That is not the case for mdbook: changing the data available to the template requires modifying mdbook itself.

As a motivating example, I'm adding translations to Comprehensive Rust 🦀. For that, I would like to make the template react to the current language (exposed in language). I don't think it's possible for me to do something like

if language == "en" { ... } else { ... }

in my template right now? To handle this, I'm moving the logic to JavaScript or to wrapper scripts which drive the translation.

Similarly, I would like to expose a list of other languages to the template. It would use this to build a language picker. I believe doing that would require changing mdbook itself.

Proposed Solution

I would propose making it possible to use more powerful template languages. Something like Askama would enable much more rich templates. I could imagine that the template engine is passed the full book.toml file so that you can freely use new keys and values from it.

Notes

No response

@mgeisler mgeisler added the C-enhancement Category: Enhancement or feature request label Feb 10, 2023
@mgeisler
Copy link
Contributor Author

Hehe, it turns out I was mistaken above. The Rust version of handlebars has "helpers" which allow me to write

{{#if (eq language "en")}}

in my template (see google/comprehensive-rust#394). It took me longer than I would like to admin to find that 😄 I don't seem to be able to find this info on the "original" documentation for the Handlebars language: https://handlebarsjs.com/guide/expressions.html.

@mgeisler
Copy link
Contributor Author

mgeisler commented Aug 2, 2023

I'll close this since I found the syntax for conditionals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement or feature request
Projects
None yet
Development

No branches or pull requests

1 participant