检查 Jade 中是否存在某个字段

发布于 2024-12-25 10:49:13 字数 2832 浏览 2 评论 0原文

考虑一下 Jade 的这段代码:

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

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

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

这是当前源和更多信息。

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")
}

如何修复并正确写入?

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

再次提前致谢

Consider this snippet of Jade:

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

I want to check if the value of episode[question.name][field] exists. If it does, output the value on the next line.

500 SyntaxError: Unexpected token ILLEGAL

Here is the current source and more information.

h1= title

p Welcome to #{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]}")

and here is the data object passed in

 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")
}

How can this be fixed and written correctly?

so for example if one of the date fields are empty (not filled in), in order to prevent an error , i need to check if its not empty, before outputting the value.

thanks again in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞旧 2025-01-01 10:49:13

表达式不应使用转义:(

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

Expressions shouldn't use escaping:

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

(Assuming proper variable setup, obviously.)


Edit to respond to comment

You're not checking to see if the element exists first before trying to read the field, in both the date display and the input element... While your usecase is a bit opaque still, you need to check for the element first:

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

I think you're trying to show an input element for things without values, and display it otherwise--not quite sure. You may want something closer to this (w/o knowing what you're doing, I made a fake template w/ what I think mirrors your data--I commented the "2-PT" value out to test). Some minor refactoring to clean things up, but not much.

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

After tidying:

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

With the "2-PT" line commented out:

<p class=""><input name="episode[M00104][2-PT]" value="" class=""></p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文