Jade/Pug JSON对象插值 [英] Jade/Pug JSON interpolation with objects

查看:90
本文介绍了Jade/Pug JSON对象插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到关于JSON插值法Pug行为的足够文档.看来您可以,但是我发现的方法千差万别,没有一个可以完全满足我的要求.顺便说一句,我在用口香糖.

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.

有可能给定这样的JSON对象:

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"}
}

插入类似这样的模板

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

变成这样吗?

<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.

推荐答案

是的,PUG(以前的JADE)可以插值.您已经阅读了官方的Jade lang参考资料吗?阅读插值

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

首先要做的是将JSON解析为一个对象,该对象可在您的jade模板中访问.您可以使用express,gulp,grunt等来做到这一点.

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.

例如,您的对象的名称为interpolated,它看起来像这样:

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"}
}

以下是默认的Javascript行为.要将其插值到模板中,它应如下所示:

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.

这篇关于Jade/Pug JSON对象插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆