问题
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