这是在 pug 模板中的每个循环内创建变量的后续问题
我在 pug 的循环中遇到了一个简单的 if else 问题。这是我的代码:
extends layout
block content
h2 #{item}
each word, index in words
- var spelling = word.orth;
if(item == spelling)
p #{spelling}
else
p does not exist yet
问题是,如果一个单词存在,它会正确打印出来,但也会打印出 尚不存在的单词。如果一个词不存在,它会打印3 次不存在 (这是我在 xml 文件中搜索的词数,见下文)。
背景:我正在一个 xml 文件中搜索单词。变量item是用户输入,我们在 xml 文件中寻找的词。
extends layout
block content
h2 #{item}
- var found = 0
each word, index in words
- var spelling = word.orth;
if(item == spelling)
- found = 1
p #{spelling}
else if (found == 0 && index == words.length -1)
p does not exist yet
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句