Hello everyone! I am writing you since I am stuck in building a template. I am doing a multilanguage template, so the parts in different languages are saved in different toml files, e.g. en.toml, it.toml, de.toml, etc. The approach I have used is to pass the language as argument, and then the template reads the words in that language and stores them in a state variable so that I can access to it within the document itself. Here is a piece of code that works:
Instead, this is a part of the code that is not working:
#let sections = (context language.get().sections.actions, context language.get().sections.reactions, context language.get().sections.limited_usage, context language.get().sections.equip, context language.get().sections.legendary_act)
#context language.get().sections.actions
#for section in sections {
if section in stats.keys() {
...etc
The things that I am inserting into the array sections should be strings, but if I print the type of those elements the result is content, and I cannot convert it to string or extract the string from it. If I pick the fields, I get (:), which should be an empty dictionary so I do not know how to get the value.
It’s because anything that depends on context becomes an opaque content once you go outside of context {…}.
You have to wrap the whole context-dependent part with context {…}, like this:
#let get-sections() = (language.get().sections.actions, language.get().sections.reactions, language.get().sections.limited_usage, language.get().sections.equip, language.get().sections.legendary_act)
#context for section in get-sections() {
if section in stats.keys() {
…
}
...
}
Alternatively, if language does not change across the document, you can use a plain old context-free variable instead.
#let template(language: …, body) = {
let sections = (language.sections.actions, language.sections.reactions, language.sections.limited_usage, language.sections.equip, language.sections.legendary_act)
...
body
}
You are running into a similar situation addressed here:
In your case, for each of the context language.get(). ... parts, the context at the beginning means that the result will always be of type content. The solution will probably include defining this sections variable within a known context and using it directly afterwards:
#context {
let sections = (language.get().sections.actions, /*lots more*/)
for section in sections {
if section in stats.keys() {
//...
}
}
}
Something you will run into right away is that the section variable will not be available outside of the context that it is defined within.