如何在 pug 中使用条件元素而不破坏 DRY
How to use conditional elements with pug and not break DRY
if list_type=='unordered'
ul
for item in items
li= item
else
ol
for item in items
li= item
我不想重复定义 for
循环两次。我怎样才能用 Pug 做到这一点?
解决此问题的最简洁方法是将标记插值与三元条件相结合。但是,这对可读性有负面影响。不太简洁的 mixin 方法可能更利于可维护性。
#{list_type == 'unordered' ? 'ul' : 'ol'}
for item in items
li= item
if list_type=='unordered'
ul
for item in items
li= item
else
ol
for item in items
li= item
我不想重复定义 for
循环两次。我怎样才能用 Pug 做到这一点?
解决此问题的最简洁方法是将标记插值与三元条件相结合。但是,这对可读性有负面影响。不太简洁的 mixin 方法可能更利于可维护性。
#{list_type == 'unordered' ? 'ul' : 'ol'}
for item in items
li= item