-
Notifications
You must be signed in to change notification settings - Fork 297
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
Context for global functions #299
Comments
Seems like this is related #219 |
I'd be happy to have a PR for #219, especially if it can fix your issues |
An issue still: the filter/tester/functions with context requires a cc @andy128k |
|
#[derive(Debug)]
pub struct GetPage<'a> {
pages: Arc<HashMap<&'a String, SerializingPage<'a>>>,
}
impl<'a> GetPage<'a> {
pub fn new(library: &'a Library) -> Self {
let mut pages = HashMap::new();
for page in library.pages_values() {
pages.insert(
&page.file.relative,
library.get_page(&page.file.path).unwrap().to_serialized(library),
);
}
Self {pages: Arc::new(pages)}
}
}
impl<'a> Function for GetPage<'a> {
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
let path = required_arg!(
String,
args.get("path"),
"`get_page` requires a `path` argument with a string value"
);
match self.pages.get(&path) {
Some(p) => Ok(to_value(p).unwrap()),
None => Err(format!("Page `{}` not found.", path).into()),
}
}
} Trying to register it gives a conflicting lifetime error because: pub fn register_tera_global_fns(&mut self) {
self.tera.register_function("trans", global_fns::Trans::new(self.config.clone()));
self.tera.register_function("get_page", global_fns::GetPage::new(&self.library));
} The lifetime of |
If we can tie the lifetime to the traits to the Tera instance instead of |
Giving a lifetime to an instance of |
Hi! Why do we need to use an external variable and the synchronization for this? Can't we pass reference to Synchronization will mean, that we can not render templates on web server in parallel. Because each client needs different value of the global variable. (for example different languages). |
Are you referring to the content of that issue or the current version? |
Sorry, I didn't understand your question. I mean Basically I want something like this: <p> translate("foobar") <p> let mut ctx = Context::new();
ctx.insert("LANG", "en");
t.render(template_name, ctx) so basically lambdas that we use in Upd. Is there a better solution for this use case in your latest release? |
Hello, I use fluent-rs for internationalization.
For now tera supports to register only global static functions without context which help process incoming data, but what if I need some additional context data?
In template:
{{ global_lang(key_name) }}
In code:
context.add("key_name", "localization-key-name")
So I want to call
global_lang
withlocalization-key-name
string as argument and inside access external object which stores all locale keys, etc and return result string data.The text was updated successfully, but these errors were encountered: