Code Monkey home page Code Monkey logo

handlebars-async-helpers's People

Contributors

dependabot[bot] avatar ericalli avatar gastonrobledo avatar gleuch avatar natlibfi-jonollil avatar nordluf avatar pocketken avatar tobnieder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

handlebars-async-helpers's Issues

Issue with accessing values outside Each

If we use each for an array of objects and wants to access values outside that array of object, then this will throw an error, stating that the property is undefined. E,g, in my case I have the following object, which is causing an issue

{ valueToAccess: "test", arrayOfObjects: [ {name: "Tom", ...}, {name: "Jerry", ...}, ...] }

Taking the above example, we use the following code in the handlebar, but it will not let us access the value.
{{#each arrayOfObjects}} {{name}} is trying to access the value {{valueToAccess}}, which will return undefined instead of test. {{/each}}

Async Helper is not working for Vue

Using Handlebar for Vue application. Since we need some async call from the template itself. This repo is not compatible with Vue/javascript since the require is not defined . Is there any way to achieve it in Vue.

Is it really needed to use the patched variable?

According to the readme, it is required to use the patched handlebars variable:

const handlebars = require('handlebars'),
      asyncHelpers = require('handlebars-async-helpers')

const asyncHandlebars = asyncHelpers(handlebars)
const template = asyncHandlebars.compile('123');
const rendered = await template({});

Anyway, to my testings it seems like this is not required, as even the original handlebars is now patched:

const handlebars = require('handlebars'),
      asyncHelpers = require('handlebars-async-helpers')

asyncHelpers(handlebars)

const template = handlebars.compile('123');
const rendered = await template({});

May you confirm? I wonder what method would be the best. By using handlebars at least we have type checking but compile() is not a Promise.

Purpose

Hello, I really appreciate your work but I wonder what the purpose of this library is over LoicMahieu/handlebars-async. Is there anything missing or not working in the other library? Or does this library make anything better than the other?

Btw. there is a typo in your readme, it says npm install handlerbars-async-helpers instead of npm install handlebars-async-helpers

TypeScript typings

Handlebars contains TypeScript typings which totally get lost by using this module. Could you add typings that extend handlebars?

Bug(?): Support for nested async values in context

Hi there, first off thanks for throwing this together -- it has been quite handy for the project I am currently working on.

I noticed some unexpected behaviour / perhaps a bug -- it seems that while you can have helpers that return async values and the template will render correctly, async values in the context are hit and miss. For example, this works:

const ctx = { somePromisedValue: Promise.resolve('yup') };
const template = handlebarsWithAsync.compile('{{ somePromisedValue }}');
console.log(await template(ctx));
// output = yup

However, if I try and make use of nesting:

const ctx = { somePromisedValue: Promise.resolve({ v: 'yup' }) };
const template = handlebarsWithAsync.compile('{{ somePromisedValue.v }}');
console.log(await template(ctx));
// output = 

It looks like this is a simple fix; I was able to get it working by shimming container.lookupProperty similarly to how you have shimmed escapeExpression. I'll open a PR shortly with a fix and additional tests.

Thanks!

Need Helper Async for JQUERY

Using Handlebar for Jquery application. Since we need some async call from the template itself. This repo is not compatible with jquery/javascript since the require is not defined . Is there any way to achieve it in jquery.

"each" helper throws "TypeError: ret.join is not a function" when the context is an empty list

This error appears to happen when empty sources are passed to the each helper that is overriden by this library.

It's this line that crashes :

return ret.join('')

The problem is that when the items count is 0, options.inverse() is called on the context and replaces the return value.
But by default, when each has no else condition, it's a noop that does not return an array but an empty string, making ret.join() crash.

Handlebars does not have this problem because they don't have to join : https://github.com/handlebars-lang/handlebars.js/blob/785a63e0a84bc5d6617fd1619baa975f1380ddbd/lib/handlebars/helpers/each.js#L80

This solution works, for example:

image

Inline partials are not working

Using inline partial as shown here I get an error:

Error: The partial myPartial could not be found

It works if I use the regular handlebars object instead.

Code:

const handlebars = require('handlebars'),
    asyncHelpers = require('handlebars-async-helpers')
const hb = asyncHelpers(handlebars)

hb.registerHelper('sleep', async () => new Promise((resolve) => {
    setTimeout(() => resolve('Done!'), 1000)
}))

async function main() {
    const template = hb.compile(`
    {{#*inline "myPartial"}}
      {{firstname}}
    {{/inline}}
    {{#each people}}
      {{> myPartial this}}
    {{/each}}
    `);
    const result = await template({
            people: [
                {firstname: "Nils"},
                {firstname: "Yehuda"},
            ],
        }
    );
    console.log(result);
}

main();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.