簡體   English   中英

如何在PUG模板引擎的錨標記類中傳遞循環變量值?

[英]How to pass for loop variable value in a class of a anchor tag in PUG template engine?

我正在嘗試使用PUG模板引擎<a>標簽打印5次。

我想要下面的代碼。 我只想要兩個classe itemitem-1,2,3等。

HTML代碼:預期結果

<a class="item item-1" href="#">1  Item</a><br/>
<a class="item item-2" href="#">2  Item</a><br/>
<a class="item item-3" href="#">3  Item</a><br/>
<a class="item item-4" href="#">4  Item</a><br/>

我現在正在使用下面的這段代碼,並且工作正常

PUG代碼

- for (var x=1; x < 6; x++)
    a.item.item-(href='#') #{x}  Item
    br

HTML的下面哪個輸出和#{x}變量可以正常打印

<a class="item item-" href="#">1  Item</a><br/>
<a class="item item-" href="#">2  Item</a><br/>
<a class="item item-" href="#">3  Item</a><br/>
<a class="item item-" href="#">4  Item</a><br/>
<a class="item item-" href="#">5  Item</a><br/>

但是當我像下面那樣使用#{x}變量時,它顯示錯誤

- for (var x=1; x < 6; x++)
    a.item.item-#{x}(href='#') #{x}  Item
    br

錯誤

Pug:2:17
    1| - for (var x=1; x < 6; x++)
  > 2|     a.item.item-#{x}(href='#') #{x}  Item
-----------------------^
    3|     br

Unexpected token `interpolation` expected `text`, `interpolated-code`, `code`, `:`, `slash`, `newline` or `eos`

據我所知,我們使用為ID,但它為什么不打印后x的值item-類?

通過更改Pug文檔中指定的代碼,我設法獲得了所需的結果:

- for (var x=1; x < 6; x++)
    a(class='item item-'+x)(href='#') #{x}  Item
    br

Codepen: https ://codepen.io/anon/pen/YrqBdY

您可以這樣寫:

- for (var x=1; x < 6; x++)
    a.item(href='#', class="item-#{x}") #{x}  Item
    br

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM