handlebars-lang / handlebars.js

Minimal templating on steroids.
http://handlebarsjs.com
MIT License
17.82k stars 2.04k forks source link

Breaking change when upgrading from 4.5.3 #1922

Closed pgrever closed 1 year ago

pgrever commented 1 year ago

Before filing issues, please check the following points first:

The following code worked in 4.5.3. When I tried to update to the latest it is failing with the error "TypeError: expected the first argument to be a number". It appears that this started failing in 4.6.0.

const Handlebars = require('handlebars');
const handlebarsHelpers = require('handlebars-helpers');

handlebarsHelpers({ handlebars: Handlebars });

Handlebars.registerHelper('for', (from, to, incr, block) => {
    let accum = '';
    for (let i = from; i < to; i += incr) {
        accum += block.fn(i);
    }
    return accum;
});

const template =
'{\
    "pages": [\
        {{#for 0 pageCount 1}}\
            {{#unless (eq this (subtract pageCount 1))}},{{/unless}}\
        {{/for}}\
    ]\
}';

const data = { pageCount: 1 };
const templateFn = Handlebars.compile(template, { noEscape: true, compat: true });
templateFn(data);

Version of handlebars-helpers is 0.10.0

jaylinski commented 1 year ago

This is an error from the handlebars-helpers library:

https://github.com/helpers/handlebars-helpers/blob/5d3405f178a9ec4c90df3c7dc07b4faf9a09dfd7/lib/math.js#L86

I guess this error is thrown because there is now a strict type-check somewhere.

The reason for the error is that pageCount is undefined inside your for-helper. You have to make variables available in your helper like this:

https://handlebarsjs.com/playground.html#format=1&currentExample=%7B%22template%22%3A%22%7B%7B%23for%200%20pageCount%201%7D%7D%5Cn%20%20pageCount%3A%20%7B%7Bcount%7D%7D%20%7B%7Bindex%7D%7D%5Cn%20%20%7B%7B%23unless%20(eq%20index%20(subtract%20count%201))%7D%7D%2C%7B%7B%2Funless%7D%7D%5Cn%7B%7B%2Ffor%7D%7D%22%2C%22partials%22%3A%5B%5D%2C%22input%22%3A%22%7B%20pageCount%3A%205%20%7D%22%2C%22output%22%3A%22%20%20pageCount%3A%205%200%5Cn%20%20%2C%5Cn%20%20pageCount%3A%205%201%5Cn%20%20%2C%5Cn%20%20pageCount%3A%205%202%5Cn%20%20%2C%5Cn%20%20pageCount%3A%205%203%5Cn%20%20%2C%5Cn%20%20pageCount%3A%205%204%5Cn%20%20%5Cn%22%2C%22preparationScript%22%3A%22Handlebars.registerHelper('for'%2C%20(from%2C%20to%2C%20incr%2C%20block)%20%3D%3E%20%7B%5Cn%20%20%20%20let%20accum%20%3D%20''%3B%5Cn%20%20%20%20for%20(let%20i%20%3D%20from%3B%20i%20%3C%20to%3B%20i%20%2B%3D%20incr)%20%7B%5Cn%20%20%20%20%20%20%20%20accum%20%2B%3D%20block.fn(%7Bindex%3A%20i%2C%20count%3A%20to%7D)%3B%5Cn%20%20%20%20%7D%5Cn%20%20%20%20return%20accum%3B%5Cn%7D)%3B%5Cn%5CnHandlebars.registerHelper('subtract'%2C%20(a%2C%20b)%20%3D%3E%20%7B%5Cn%20%20return%20a%20-%20b%3B%5Cn%7D)%3B%5Cn%5CnHandlebars.registerHelper('eq'%2C%20(a%2C%20b%2C%20options)%20%3D%3E%20%7B%5Cn%20%20return%20a%20%3D%3D%20b%3B%5Cn%7D)%3B%22%2C%22handlebarsVersion%22%3A%224.7.7%22%7D

pgrever commented 1 year ago

Just to clarify... This was working without error in 4.5.3. But as of 4.6.0 it does not. The release notes indicate that there were not any breaking changes between these versions (hence only a minor version roll). However, this appears to be a breaking change to me. Am I missing something, or should this still behave the same?

pgrever commented 1 year ago

FYI, I have verified that in 4.5.3 pageCount was available with the the for helper. Is there some reason that data properties are no longer available inside the helpers? This definitely seems like a breaking change.

jaylinski commented 1 year ago

Just to clarify... This was working without error in 4.5.3. But as of 4.6.0 it does not. The release notes indicate that there were not any breaking changes between these versions (hence only a minor version roll). However, this appears to be a breaking change to me. Am I missing something, or should this still behave the same?

There was a breaking change (for security reasons):

pgrever commented 1 year ago

OK, thank you. Based upon terminology I did not realize that our use case was the same as the one mentioned in the release notes.