【问题标题】:Write formatted text in pug mixin attributes在 pug mixin 属性中写入格式化文本
【发布时间】:2020-03-04 05:56:40
【问题描述】:

我需要在 mixin 属性中编写多行文本。但找不到正确的方法。

所有这些方法都不起作用:

+someMixin({
  key: "text in \n two lines"
})
+someMixin({
  key: "text in two lines"
})
+someMixin({
  key: `text in${ }two lines`
})
+someMixin({
  key: `text in#{ }two lines`
})
+someMixin({
  key: `text in${\n}two lines`
})
+someMixin({
  key: `text in#{\n}two lines`
})
+someMixin({
  key: `text in#{br}two lines`
})
+someMixin({
  key: `text in${br}two lines`
})
+someMixin({
  key: `text in<br/>two lines`
})
+someMixin({
  key: 'text in<br/>two lines'
})

请告诉我这样做的可能性和正确的方法。 谢谢!

【问题讨论】:

  • 你是否考虑过使用 mixin block 代替 mixin 参数?
  • 是的,但我没有通过。

标签: pug


【解决方案1】:

Pug mixin 旨在为控制变量获取属性,为内容获取块。您正在尝试为内容使用属性,这就是您发现实现这一目标如此困难的原因。

此 mixin 会将键视为分隔字符串,将其拆分为数组,然后为数组中的每个成员输出 &lt;p&gt; 元素。

mixin someMixin(options)
  - var delimiter = '\n';
  - var output = options.key.split(delimiter);
  each line in output
    p= line

所以这个

+someMixin({
  key: "text in \n two lines \n no wait make it three"
})

然后会输出这个:

<p>text in </p>
<p> two lines <p>
<p> no wait make it three</p>

【讨论】:

  • 谢谢。我已经在做类似你的代码的事情了。我只是在寻找更优雅的机会来做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多