0

カスタム ハンドルバー ヘルパーを作成しましたが、パラメーターの値を定義しないと次のエラーが発生します。

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;
}

これは、3 つのパラメーターすべてを次のように定義すると機能します。

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

パラメータ コンソールを定義しないと、「Handlebars TypeError: Cannot read property 'fn' of undefined」が表示されます。

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

ここで私が間違っていることについてのアイデアはありますか?

更新:以下に示すように「null」でもチェックされますが、それでも同じエラーが発生します。

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

3 に答える 3