pug 中的“组中的每个元素”不显示任何值

硅克里斯

我正在尝试从 node.js 中 res.render 提供的 JSON 对象填充 pug 中的动态行/列(以前称为表)。

我的问题是,简单的值很容易显示,但没有填充更复杂的操作,例如动态数量元素的行/列定义(这里恰好是特定有声读物的媒体文件)。

这是我的模板文件中带有 each... 块的片段:

div.form-group
  div.row
    div.col-2
      label(for='varMediaTitle') Titel:
    div.col
      input#MediaTitle.form-control(type='text', placeholder=varMediaTitle, name='varMediaTitle', required)
div.form-group
  div.row
    div.col-4
      label(for='MediaFileName') Audio Dateien:
  div.row
    div.col-1 Nr
    div.col-5 Name
    div.col-2 Groesse
  each mediaFile in varMediaFiles
    div.row
      each itemM in mediaFile
        div.col-1
          p #{itemM.part}
        div.col-5
          p #{itemM.name}
        div.col-2
        p #{itemM.size}
  div.row
    div.col
      input#MediaFileName.form-control(type='file', name='MediaFileName', required)

如上所述,模板中的简单元素,例如我只是在输入字段中显示的媒体标题(没有迭代),可以正确显示,但是 varMediaFiles 中每个 mediaFile 的块没有显示任何内容。

有趣的是,在检查生成的 html 时,我可以看到正确的行数。也就是说,创建的行数与我的 json 文件中的元素数相对应。

chrome 中的 html 检查

在这里你可以看到我从 node.js 函数调用表单:

res.render('tags', {
                title: 'RFID Tag Datenseite',
                headline: 'RFID Tag Daten',
                subheadline: 'Tag ' + obj.TagId + ' - ' + obj.MediaTitle,
                varTagId: obj.TagId,
                varTagPreTag: obj.TagPreTag,
                varTagChecksum: obj.TagChecksum,
                varTagRawData: obj.TagRawData,
                varMediaTitle: obj.MediaTitle,
                varMediaType: obj.MediaType,
                varMediaGenre: obj.MediaGenre,
                varMediaDescription: obj.MediaDescription,
                varMediaFiles: obj.MediaFileName,
                varMediaPictures: obj.MediaPicture
            });

这是 json 文件的结构(它是从文件系统读入的实际文件),从中将值传递给表单:

{
  "TagChecksum": "0x97",
  "TagId": "759C71",
  "TagPreTag": "0xf00",
  "TagRawData": "0F00759C7197",
  "MediaTitle": "The 6th book",
  "MediaType": "Audiobook",
  "MediaGenre": "UNDEFINED",
  "MediaDescription": "Beschreibung",
  "MediaFileName": [{"part": "1", "name": "1st-file.mp3", "size": "6M"}, {"part": "2", "name": "2nd-file.mp3", "size": "8M"}],
  "MediaPicture": [{"pic": "1", "name": "1st-pic.jpg"}, {"pic": "2", "name": "2nd-pic.jpg"}, {"pic": "3", "name": "3rd-pic.jpg"}]
}

任何想法,为什么没有迭代的简单值有效,但对 json 数组的迭代却没有???

我的第一个想法是,这是因为它是 json 结构中的数组,而且我可能对 json 文件的格式有问题。但在那种情况下,我无法理解,为什么 each... 块正确地创建了行数。

There is, however, one additional glitch. After inspecting the html output (see attached picture) I came to notice, that the iteration actually creates the correct number of rows (here 2 for the media files), but far too many cols for each row. It looks like it puts all cols of all rows in all rows - very strange.

Can anyone shed some light on this?

Best regards and many many thanks,

Christian

siliconchris

I finally figured it out and thought, since there was no answer at all to this question but quite a few views, it might just be worth to post the solution, so other newbies like me can find it.

The solution was actually quite simple.

The second iteration (that is the each itemM ... block from my question above) was completely nonsense and I simply got rid of it. Instead of iterating through the elements within one of the json array structures, I referenced it directly.

I also exchanged 'each' as a statement with 'for', but I'm not sure if this is necessary.

In the end, my each...block (which did not work):

  each mediaFile in varMediaFiles
    div.row
      each itemM in mediaFile
        div.col-1
          p #{itemM.part}
        div.col-5
          p #{itemM.name}
        div.col-2
        p #{itemM.size}

became this for...block (which works):

    for mediaFile in varMediaFiles
      div.row
        div.col-1
          p #{mediaFile.part}
        div.col-5
          p #{mediaFile.name}
        div.col-2
          p #{mediaFile.size}

The reason for this is pretty clear, once I figured it out. I do not have a nested array - in which case a nested each or for loop would probably have done the trick. I have a json structure with an array of json elements.

[{"part": "1", "name": "1st-file.mp3", "size": "6M"}, {"part": "2", "name": "2nd-file.mp3", "size": "8M"}],

Each element of this array is again a json structure. Since it is a json structure I can simply reference the part of the json element by name, while iterating through the array elements.

有时它实际上比人们想象的要简单。

祝大家编码愉快,

基督教

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章