• I am working on benchpressjs and trying to customize NodeBB templates. I found some issues of benchpressjs.

    #1. about Interpolation, benchpressjs does NOT allow to retrieve array by index, like this:
    <h1>{localNews.topics[0].title}</h1>

    #2, about helper function, I created one like this:
    Benchpress.registerHelper('getByIndexInArray', function (arrayData, i) {
    return arrayData[i];
    });

    and call this helper function like this:
    <h5>{getByIndexInArray(localNews.topics, 1)}</h5>

    But it seems like the index "i" can not be passed into the helper function, since in this helper function, I always got "i" as undefined.

    it seems like benchpressjs is terrible template engine or maybe what I did is wrong? any suggestion/ideas?

  • Global Moderator

    First off, calling their work "terrible" is a really bad strategy if you're trying to get help from someone. I'll still help, because I like being helpful, but please don't act this way in the future.

    #1. about Interpolation, benchpressjs does NOT allow to retrieve array by index, like this:
    <h1>{localNews.topics[0].title}</h1>

    If you're trying to just get a specific single element of an array, you just put the number as the property name, like so:

    <h1>{localNews.topics.0.title}</h1>
    

    I see this isn't documented, so I'll open an issue to document this behavior.

    #2, about helper function, I created one like this:
    Benchpress.registerHelper('getByIndexInArray', function (arrayData, i) {
    return arrayData[i];
    });

    and call this helper function like this:
    <h5>{getByIndexInArray(localNews.topics, 1)}</h5>

    But it seems like the index "i" can not be passed into the helper function, since in this helper function, I always got "i" as undefined.

    Your helper is correct, it's how you're calling it that's the issue. Benchpress doesn't have numeric literals, it only has string literals. Putting just 1 in there is telling Benchpress to look up the property "1" on the global object, and call the helper with that value (which is undefined in your case). Your helper will work if you call it like this instead:

    <h5>{getByIndexInArray(localNews.topics, "1")}</h5>
    

    I will also add a note to document this behavior in the paths and helper section.

    Issue is here: https://github.com/benchpressjs/benchpressjs/issues/89


  • Thanks for the help and sorry if the "terrible" word hurts your feeling, I didn't mean to.

    @PitaJ said in the issue of benchpressjs:

    First off, calling their work "terrible" is a really bad strategy if you're trying to get help from someone. I'll still help, because I like being helpful, but please don't act this way in the future.

    #1. about Interpolation, benchpressjs does NOT allow to retrieve array by index, like this:
    <h1>{localNews.topics[0].title}</h1>

    If you're trying to just get a specific single element of an array, you just put the number as the property name, like so:

    <h1>{localNews.topics.0.title}</h1>
    

    I see this isn't documented, so I'll open an issue to document this behavior.

    #2, about helper function, I created one like this:
    Benchpress.registerHelper('getByIndexInArray', function (arrayData, i) {
    return arrayData[i];
    });

    and call this helper function like this:
    <h5>{getByIndexInArray(localNews.topics, 1)}</h5>

    But it seems like the index "i" can not be passed into the helper function, since in this helper function, I always got "i" as undefined.

    Your helper is correct, it's how you're calling it that's the issue. Benchpress doesn't have numeric literals, it only has string literals. Putting just 1 in there is telling Benchpress to look up the property "1" on the global object, and call the helper with that value (which is undefined in your case). Your helper will work if you call it like this instead:

    <h5>{getByIndexInArray(localNews.topics, "1")}</h5>
    

    I will also add a note to document this behavior in the paths and helper section.

    Issue is here: https://github.com/benchpressjs/benchpressjs/issues/89

Suggested Topics

| |