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
Accessing fields of a ScopedJson returned by helper #433
Comments
@ilyazzz I'm afraid To implement your scenario, you can try {{ lookup (myhelper) "a" }} |
I have tried using lookup, but it doesn't seem to work. Here's a minimal example. It doesn't print out anything, and I'm not sure why: use handlebars::{Context, Handlebars, Helper, HelperDef, RenderContext, RenderError, ScopedJson};
use serde_json::json;
fn main() {
let mut registry = Handlebars::new();
registry.register_helper("myhelper", Box::new(MyHelper {}));
let result = registry.render_template("{{ lookup (myhelper) \"a\" }}", &TemplateContext {}).unwrap();
println!("{}", result) ;
}
struct MyHelper;
impl HelperDef for MyHelper {
fn call_inner<'reg: 'rc, 'rc>(
&self,
_: &Helper<'reg, 'rc>,
_: &'reg Handlebars,
_: &'rc Context,
_: &mut RenderContext<'reg, 'rc>,
) -> Result<Option<ScopedJson<'reg, 'rc>>, RenderError> {
Ok(Some(ScopedJson::Derived(json!({
"a": 1,
"b": 2,
}))))
}
}
#[derive(serde::Serialize)]
struct TemplateContext; This is a working basic helper in javascript, where the same template syntax does work: Handlebars.registerHelper('myhelper', function (value) {
return { "a": 1, "b": 2 }
}); |
Let me look into this. It seems to be a bug with |
Confirmed there is a bug with parser that it cannot recognize subexpression with no parameters. For now, you can workaround this with |
|
@ilyazzz could you please verify this behaviour with our latest |
@sunng87 yes, it seems to work properly within my |
Maybe I'm misunderstanding how objects returned by helpers are supposed to be used, but I was unable to access the fields of an object that my custom handler returns.
I have a helper that looks like this:
I'm not sure what would be the proper way to access "a" and "b" from it. I have tried using
{{ myhelper["a"] }}
, but it seems to always return[object]
, regardless of accessing the entire helper object or one of it's fields. How do I access the value in it as a string?The text was updated successfully, but these errors were encountered: