如何在JADE中自定义此mixin

时间:2015-04-24 15:26:13

标签: node.js pug mixins

我正在使用jade中的mixin创建带有img标签的div ...这里是mixin代码:

mixin image(pics)
  each pic in pics
    .row
      .col-md-4
        img.img-responsive(src="#{pic.path}" alt="#{pic.name}")

这是我使用mixin时的一个例子:

+image([
    {'name':'anti'    , 'path':'/images/anti.jpg'},
    {'name':'dark'    , 'path':'/images/dark.jpg'},
    {'name':'haik'    , 'path':'/images/haik.jpg'},
    {'name':'james'   , 'path':'/images/james.jpg'},
    {'name':'johanna' , 'path':'/images/johanna.jpg'},
    {'name':'timothy' , 'path':'/images/Timothy.jpg'}
    ])

是否可以将所有这些值分配给其他文件中的变量然后包含该文件并使用mixin?如果有,怎么样? :d

这是目标,我只是证明我的意思,显然下面的代码不会起作用:

var allPics = [
        {'name':'anti'    , 'path':'/images/anti.jpg'},
        {'name':'dark'    , 'path':'/images/dark.jpg'},
        {'name':'haik'    , 'path':'/images/haik.jpg'},
        {'name':'james'   , 'path':'/images/james.jpg'},
        {'name':'johanna' , 'path':'/images/johanna.jpg'},
        {'name':'timothy' , 'path':'/images/Timothy.jpg'}
        ]

+image(allPics)

1 个答案:

答案 0 :(得分:0)

是的,您可以在包含的文件中使用变量以及Mixins

test.jade包括test_include.jade

test_include.jade:

- var allPics  = [{'name':'anti'    , 'path':'/images/anti.jpg'},{'name':'dark'    , 'path':'/images/dark.jpg'},{'name':'haik'    , 'path':'/images/haik.jpg'},{'name':'james'   , 'path':'/images/james.jpg'},{'name':'johanna' , 'path':'/images/johanna.jpg'},{'name':'timothy' , 'path':'/images/Timothy.jpg'}]
mixin image(pics)
  each pic in pics
    .row
      .col-md-4
        img.img-responsive(src="#{pic.path}" alt="#{pic.name}")
block content

test.jade:

extends test_include
  block content
   +image(allPics)