简体   繁体   English

Jade / Pug mixin扩展

[英]Jade/Pug mixin extension

I've started to use Jade at work and I met a problem for which I couldn't find an answer to on Google. 我开始在工作中使用Jade,但遇到一个无法在Google上找到答案的问题。

I want write such a mixin: 我想写这样一个混合:

mixin menu(...MenuList) 
  ul.main-menu
    each item in MenuList
      li: a(href='##{item.toLowerCase()}.html')= item  

This mixin works well for single name links like +menu('Contacts', 'FAQ') , but doesn't work for multi-word links like +menu('Contact Us', 'Our Price') . 这种混合功能对于+menu('Contacts', 'FAQ')等单个名称链接非常有效,但对于+menu('Contact Us', 'Our Price')等多词链接则无效。

I tried to write something like: 我试图写类似:

li: a(href='##{item.toLowerCase().replace(' ', '_')}.html')= item  

or: 要么:

li: a(href='##{item.toLowerCase(function(x){ return x.replace(' ', '_')})}.html')= item

but nothing works for me. 但对我没有任何作用。 Maybe it's the lack of JS skills, but I would be happy if somebody could help me. 也许是因为缺乏JS技能,但是如果有人可以帮助我,我会很高兴。

The problem was with the way you pass in the href. 问题出在您传递href的方式上。 Here's a working one and Codepen.io link 这是一个有效的代码和Codepen.io链接

mixin menu(...MenuList)
  - var patt = /\s+/i; // This checks for whitespace.

  ul.main-menu
    each item in MenuList
     if patt.test(item)
       - var newitem = item.split(' ').join('_');

       li: a&attributes({ href: '#' + newitem.toLowerCase() + '.html' })= item
     else
       li: a&attributes({ href: '#' + item.toLowerCase() + '.html' })= item

+menu('my-list', 'Contact Us', 'Hello', 'Our Price', 'My Price')

// result
- my-list
- Contact Us
- Hello
- Our Price
- My Price

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM