Jade/Pug JSON interpolation with objects

随声附和 提交于 2019-12-10 15:18:11

问题


I can't seem to find any sufficient documentation on the behavior of Pug with JSON interpolation. It seems that you can, but the methods I've found differ wildly and none do quite what I want. I'm using gulp-pug, by the way.

Is it possible, given a JSON object like so:

{
  0: {name: "zero", desc: "the additive identity"},
  1: {name: "one", desc: "the multiplicative identity"},
  2: {name: "two", desc: "the first prime number"}
}

to interpolate a template something like this

ul
  li(interpolated=0) BlaBlaBla
  li(interpolated=1) EtcEtcEtc

into something like this?

<ul>
  <li title="zero" desc="the additive identity">BlaBlaBla</li>
  <li title="one" desc="the multiplicative identity">EtcEtcEtc</li>
</ul>

Sorry for the vagueness. Is Pug capable of this? I highly doubt it. Please recommend alternatives if not.


回答1:


Yes, PUG (former JADE) is capable to interpolate. Did you already read the official Jade lang reference? Read the section for interpolation

Waht you have to do first is parse your JSON into an object which is accessible in your jade template. You can do it with express, gulp, grunt and many more.

For Example the name of your object is interpolated it would look like this:

interpolated = {
  0: {name: "zero", desc: "the additive identity"},
  1: {name: "one", desc: "the multiplicative identity"},
  2: {name: "two", desc: "the first prime number"}
}

the following is default Javascript behavior. To interpolate this in your template it should look like this:

ul
  li(title= interpolated[0].name, desc= interpolated[0].desc) BlaBlaBla
  li(title= interpolated[1].name, desc= interpolated[1].desc) EtcEtcEtc
  li(title= interpolated[2].name, desc= interpolated[2].desc) EtcEtcEtc

Guess this is a first impression, read the docs and you will learn quickly.



来源:https://stackoverflow.com/questions/38760860/jade-pug-json-interpolation-with-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!