SendGrid Handlebars嵌套#equals语句匹配异常问题求助
问题解决:SendGrid Handlebars 条件判断仅第一个分支生效
问题分析
你当前的嵌套{{#equals}}写法在SendGrid中出现逻辑异常:仅当rollupRegion为South时匹配正确,其他值均进入最外层else分支,这大概率是嵌套助手的上下文解析问题,或是equals助手的嵌套写法兼容性问题。
修复方案
方案1:改用eq助手+else if结构(推荐)
SendGrid支持Handlebars内置的eq比较助手,使用else if的线性判断结构替代嵌套,逻辑更清晰且避免上下文问题:
<table border="0" width="100%"> <tr> <td style="text-align: center" align="center"> {{#if (eq this.rollupRegion 'South')}} <img src="http://cdn.mcauto-images-production.sendgrid.net/.../0945bd18-ff2e-4ce1-8f3f-72cf4877d66d/1063x524.jpg" width="70%"> {{else if (eq this.rollupRegion 'Mid Atlantic')}} <img src="http://cdn.mcauto-images-production.sendgrid.net/.../d23b3813-2754-403d-88de-38c1c762c966/1063x540.jpg" width="70%"> {{else}} <img src="http://cdn.mcauto-images-production.sendgrid.net/.../7136888c-9800-495e-a325-a165cf7a30d5/1064x540.jpg" width="70%"> {{/if}} </td> </tr> </table>
方案2:先验证变量值准确性
如果问题仍存在,先在模板中临时添加变量输出,确认传入的rollupRegion值是否完全匹配预期:
<!-- 临时添加,测试变量值 --> 当前rollupRegion值:{{this.rollupRegion}} <table border="0" width="100%"> <!-- 原模板内容 --> </table>
检查是否存在大小写错误、前后空格(比如实际值为mid atlantic或Mid-Atlantic),这类细节会导致条件匹配失败。
方案3:调整嵌套equals的上下文引用
若坚持使用嵌套{{#equals}},尝试明确上下文路径,避免内层助手解析变量出错:
<table border="0" width="100%"> <tr> <td style="text-align: center" align="center"> {{#equals this.rollupRegion 'South'}} <img src="http://cdn.mcauto-images-production.sendgrid.net/.../0945bd18-ff2e-4ce1-8f3f-72cf4877d66d/1063x524.jpg" width="70%"> {{else}} {{#equals ../rollupRegion 'Mid Atlantic'}} <img src="http://cdn.mcauto-images-production.sendgrid.net/.../d23b3813-2754-403d-88de-38c1c762c966/1063x540.jpg" width="70%"> {{else}} <img src="http://cdn.mcauto-images-production.sendgrid.net/.../7136888c-9800-495e-a325-a165cf7a30d5/1064x540.jpg" width="70%"> {{/equals}} {{/equals}} </td> </tr> </table>
内容的提问来源于stack exchange,提问作者Catinodeh
相关产品推荐
相关产品推荐

