wycats / handlebars-site

56 stars 66 forks source link

arguments passed failed when using the helper functions inside embedded script of the template #190

Closed stang1013 closed 6 years ago

stang1013 commented 6 years ago

Say I have a helper function:

HandleBars.registerHelp('format_time', (date_value)=>{
  if (date_value) return dataFormat(date_value, "HH:MM:ss");
  return "";
});

I have an hbs file(has embedded JavaScript) like this:

< table id ="dtable" >
< tbody >...< / tbody >
< /table >
 < script>
         function renderTime (data, type, row, meta) {
              //data passed is "Sat Feb 03 2018 12:12:12 GMT-0700 (Mountain Standard Time) "
              return `{{format_time data}}`;
         };
  < script>

In the renderTime function, we passed in actual value to data, say '2018-02-03 12:12:12', what format_date helper's date_value is always undefined, you can see the container.helpers's format_date's first argument is undefined.

nknapp commented 6 years ago

If I understand correctly, you expect that the helper format_time is called by the renderTime-function. But this is not the case. The helper is called, when you render the template. The "renderTime" function will contain the result-value of the helper as a string literal.

I expect that data is not part of the object that you pass to Handlebars' render-function. Then, indeed, format_time will be called with undefined. The render-result will be:

< table id ="dtable" >
< tbody >...< / tbody >
< /table >
 < script>
         function renderTime (data, type, row, meta) {
              //data passed is "Sat Feb 03 2018 12:12:12 GMT-0700 (Mountain Standard Time) "
              return ``;
         };
  < script>

When you call the function, if will always return an empty string.

stang1013 commented 6 years ago

Hi Nil, Thanks for response, I just want to know, in javascript section, how to pass variable to handlebar helper function? e.g in test.hbs

pass real value of dt into the helper instead of undefined? currently I’m using extra ajax call which is not elegant.

Best Regards Steven

On Feb 13, 2018, at 2:26 PM, Nils Knappmeier notifications@github.com wrote:

If I understand correctly, you expect that the helper format_time is called by the renderTime-function. But this is not the case. The helper is called, when you render the template. The "renderTime" function will contain the result-value of the helper as a string literal.

I expect that data is not part of the object that you pass to Handlebars' render-function. Then, indeed, format_time will be called with undefined. The render-result will be:

< table id ="dtable" > < tbody >...< / tbody > < /table > < script> function renderTime (data, type, row, meta) { //data passed is "Sat Feb 03 2018 12:12:12 GMT-0700 (Mountain Standard Time) " return ``; }; < script> When you call the function, if will always return an empty string.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

nknapp commented 6 years ago

I'm afraid that is not possible. If you render the template on the server, the helper function is not available in the browser. You have to make an ajax request.