简体   繁体   English

Jade:如何将随机变量传递到包含的模板中?

[英]Jade: how can I pass a random variable into included template?

I have a jade template ('main page'), and a reusable template ('product template'). 我有一个翡翠模板(“主页”)和一个可重复使用的模板(“产品模板”)。 'product template' has to display dynamic data is used in many other pages, so it has to be unified and not dependable on variable names. “产品模板”必须显示动态数据,其他许多页面也使用了它,因此它必须统一且不依赖变量名。

// main page
... (some code) ...

- var outerObj = [
  { title: '...', price: '...', description: '' },
  { title: '...', price: '...', description: '' },
  { title: '...', price: '...', description: '' }
];

// product temptate

.product
  .product__title=outerObj.title
  .product__price=outerObj.price
  .product__description=outerObj.description

How can Include 'product template' and pass parameters in loop? 如何在循环中包含“产品模板”并传递参数? (somewhat like this) (有点像)

// example of regular usage of 'product template':
- for (var i = 0; i < outerObj.length; i++)
  // pass only outerObj[i]
  include ./path/to/product-template.jade'

Rather than doing an include inside a loop, you probably want to use a mixin which is like a function. 与其在循环内进行include不如使用像函数一样的mixin

Something like this: 像这样:

mixin makeProduct(product)
  .product
    .product__title=product.title
    .product__price=product.price
    .product__description=product.description

each product in outerObj
  +makeProduct(product)

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

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