Search code examples
javascriptnode.jshandlebars.jshandlebarshelperzurb-ink

Handlebars TypeError: Cannot read property 'fn' of undefined


I created a custom handlebars helper and I'm getting the following error when I do not define value for parameters.

module.exports = function(src, color, classatr, options) {

    if (typeof src === 'undefined') src = '';
    if (typeof color === 'undefined') color = '';
    if (typeof classatr === 'undefined') classatr = '';

    var bg = '<table class="'+ classatr +'" cellpadding="0" cellspacing="0" border="0" width="100%">\
      <tr>\
        <td background="'+ src +'" bgcolor="'+ color +'" valign="top">\
          <!--[if gte mso 9]>\
          <v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="mso-width-percent:1000;">\
            <v:fill type="tile" src="'+ src +'" color="#'+ color +'" />\
            <v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0">\
          <![endif]-->\
          <div>'
          + options.fn(this) +
          '</div>\
          <!--[if gte mso 9]>\
            </v:textbox>\
          </v:rect>\
          <![endif]-->\
        </td>\
      </tr>\
    </table>';

    return bg;
}

This works if I define all three parameters as such:

{{#bg-img 'assets/img/hero-header.jpg' '000000' 'my-class'}}
    <container>
        <row>
            <columns>
            </columns>
        </row>
    </container>
{{/bg-img}}

If I do not define a parameter console shows "Handlebars TypeError: Cannot read property 'fn' of undefined".

{{#bg-img }}
    <container>
        <row>
            <columns>
            </columns>
        </row>
    </container>
{{/bg-img}}

Any ideas as to what I'm doing wrong here?

Update: Also checked with "null" as suggested below but still same error.

if (typeof src === 'undefined' || src === null) src = '';
if (typeof color === 'undefined' || color === null) color = '';
if (typeof classatr === 'undefined' || classatr === null) classatr = '';

Solution

  • use null where you don't wanna provide any parameter. so, the following code should work :

    {{#bg-img null null null}}
        <container>
            <row>
                <columns>
                </columns>
            </row>
        </container>
    {{/bg-img}}