【问题标题】:Howto include template code by condition in jade?如何在jade中按条件包含模板代码?
【发布时间】:2017-12-12 10:35:28
【问题描述】:

您好,我正在使用 Meteor 和一些玉 (blaze) 模板:

template(name="userFormsLayout")
  section.auth-layout
    section.auth-dialog
     +samlLogin
     //+Template.dynamic(template=content)

我想通过条件包含评论部分:

template(name="userFormsLayout")
  section.auth-layout
    section.auth-dialog
     +samlLogin
     var cond = true
     if(cond)
       +Template.dynamic(template=content)

我也有这个模板的 javascript

Template.userFormsLayout.helpers({
  languages() {
    return _.map(TAPi18n.getLanguages(), (lang, tag) => {
      const name = lang.name;
      return { tag, name };
    });
  },

  isCurrentLanguage() {
    const t9nTag = i18nTagToT9n(this.tag);
    const curLang = T9n.getLanguage() || 'en';
  return t9nTag === curLang;
  },
});

谁能帮忙?

【问题讨论】:

  • 您是否在实际模板中设置了 var cond?
  • 查看更新,看起来一模一样,好的,我尝试了一些带括号的不同版本...但最终总是出错
  • cond的值从何而来?
  • 无处,我只是在玉文件中破解了它。 var cond = true

标签: javascript meteor pug meteor-blaze


【解决方案1】:

Meteor 的方法是在助手中设置变量cond。示例:

助手:

Template.userFormsLayout.helpers({
  cond() {
    // a function that returns 'true' or 'false'
    return true;
  },
});

模板:

template(name="userFormsLayout")
  section.auth-layout
    section.auth-dialog
     +samlLogin
     if(cond)
       +Template.dynamic(template=content)

【讨论】:

  • 你也可以这样返回自定义对象吗?
  • 是的,如果您返回了{ name: 'test', value: true },那么您可以在模板中使用{{cond.name}} 或{{cond.value}}
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
相关资源
最近更新 更多