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

Accessing fields of a ScopedJson returned by helper #433

Closed
ilya-zlobintsev opened this issue May 14, 2021 · 7 comments
Closed

Accessing fields of a ScopedJson returned by helper #433

ilya-zlobintsev opened this issue May 14, 2021 · 7 comments
Milestone

Comments

@ilya-zlobintsev
Copy link

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:

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,
        })))
}

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?

@sunng87
Copy link
Owner

sunng87 commented May 16, 2021

@ilyazzz I'm afraid {{myhelper["a"]}} is not valid handlebars expression. You can test it on the playground from javascript version: http://tryhandlebarsjs.com/

To implement your scenario, you can try lookup with subexpression, like:

{{ lookup (myhelper) "a" }}

@ilya-zlobintsev
Copy link
Author

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 }
});

@sunng87
Copy link
Owner

sunng87 commented May 19, 2021

Let me look into this. It seems to be a bug with lookup helper

@sunng87 sunng87 added the bug label May 19, 2021
@sunng87
Copy link
Owner

sunng87 commented May 19, 2021

Confirmed there is a bug with parser that it cannot recognize subexpression with no parameters. For now, you can workaround this with {{lookup (myhelper 0) "a" }}

@sunng87 sunng87 added this to the 4.0 milestone May 19, 2021
@ilya-zlobintsev
Copy link
Author

if also seems to be affected, where {{ #if (myhelper) }} doesn't work and {{ #if (myhelper 0) }} does

sunng87 added a commit that referenced this issue May 20, 2021
@sunng87
Copy link
Owner

sunng87 commented May 20, 2021

@ilyazzz could you please verify this behaviour with our latest master?

@ilya-zlobintsev
Copy link
Author

@sunng87 yes, it seems to work properly within my with expression now. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants