检查Jade中是否存在字段

时间:2012-01-06 17:20:28

标签: javascript pug

考虑一下Jade的片段:

if(#{episode[question.name][field]})
   #{episode[question.name][field]}

我想检查episode[question.name][field]的值是否存在。如果是,则输出下一行的值。

  

500语法错误:意外的标记ILLEGAL

以下是当前来源和更多信息。

h1= title

p欢迎来到#{title}

ol#questions
    each question in questions
        li  
            ul#question
                li: h3  (#{question.name})  #{question.description}
                - if(question.options)
                    ul#options
                        for option, i in question.options
                            if(typeof(option) === "object")
                                li: h3  #{i}
                                ul#option
                                        each soption in option
                                            li
                                                input(type='checkbox',  name='episode[#{question.name}][#{soption}]',   checked='#{episode[question.name][soption]}')
                                                //
                                                label
                                                    #{soption}  
                            else
                                li
                                    input(type='checkbox',  name='episode[#{question.name}][#{option}]',    checked='#{episode[question.name][option]}')
                                    //
                                    label
                                        #{option}
                if(typeof(question.fields) !== 'undefined')
                    for field, i in question.fields
                        if(field === "Date")
                            p.date
                                if(#{episode[question.name][field]} !== "undefined")                    
                                    #{episode[question.name][field]}
                        else
                            p                       
                                input(name='episode[#{question.name}][#{field}]', class='', value="#{episode[question.name][field]}")

这是传递的数据对象

 episode =   {
  "form": {
    "formid": "4efd9c4cae999dcf0a12c461",
    "name": "ROC"
  },
  "patientid": ObjectId("4ef6835a7a5869082a80bc95"),
  "certperiodid": ObjectId("4f020ff0850b21bc2b000001"),
  "M0080": {
    "2-PT": "on",
    "3-SLP\/ST": "on",
    "4-OT": "on"
  },
  "M0090": {
    "Date": "2012-01-02"
  },
  "M0100": {
    "1 - Start of care--further visits planned": "on",
    "3 - Resumption of care (after inpatient stay)": "on"
  },
  "M00104": {
    "Date": "2012-05-02"
  },
  "_id": ObjectId("4f071a0424ddf0dd66000003")
}

如何正确修复和编写?

所以例如,如果其中一个日期字段为空(未填写),为了防止出错,我需要在输出值之前检查它是否为空。

提前再次感谢

1 个答案:

答案 0 :(得分:2)

表达式不应该使用转义:

- if (episode[question.name][field])
  foo

(假设显然有适当的变量设置。)


编辑以回复评论

在日期显示和输入元素中尝试读取字段之前,您没有检查元素是否存在...当您的用例仍然有点不透明时,您需要检查元素第一:

- if (episode[question.name] && episode[question.name][field])",

你试图为没有值的东西显示输入元素,否则显示它 - 不太确定。你可能想要更接近这一点(不知道你正在做什么,我做了一个虚假的模板,我认为镜像你的数据 - 我评论了"2-PT"值来测试)。一些小的重构来清理东西,但并不多。

var s = [
  "ol#questions",
  "  each question in questions",
  "    li",
  "      ul.question",
  "        li: h3  (#{question.name})  #{question.description}",
  "        if question.fields",
  "          each field, i in question.fields",
  "            - var foo = field === 'Date' ? 'date' : ''",
  "            p(class='#{foo}')",
  "              if episode[question.name]",
  "                - var field_value = episode[question.name][field] || '' ",
  "                if field_value",
  "                  #{field_value}",
  "                else",
  "                  input(name='episode[#{question.name}][#{field}]', class='', value='#{field_value}')",
  ""
].join("\n");

var locals = {
  "question": {
    "name": "M00104"
  },
  "episode": {
    "M00104": {
      // "2-PT": "on",
      "Date": "2012-01-02"
    }
  },
  "questions": [
    {
      "name": "M00104",
      "description": "Do we have it?",
      "fields": ["Date", "2-PT"]
    },
  ]
}

var jade = require('jade');
var fn = jade.compile(s);
console.log(fn(locals));

整理后:

<ol id="questions">
  <li>
    <ul class="question">
      <li>
        <h3>(M00104) Do we have it?</h3>
      </li>

      <li style="list-style: none; display: inline">
        <p class="date">2012-01-02</p>

        <p class="">on</p>
      </li>
    </ul>
  </li>
</ol>

"2-PT"行注释掉:

<p class=""><input name="episode[M00104][2-PT]" value="" class=""></p>