翡翠模板引擎(在node.js下):无管符号的多行块
题
我目前在一个新项目中使用Jade。它似乎非常适合撰写WebApp布局,但不适合编写静态内容,例如
包含文本的元素。
例如,要创建这样的段落,我相信我需要这样做:
p
| This is my long,
| multi-line
| paragraph.
对于一个充满文本的真实段落的静态网页,由于每行开头的管道符号,使用Jade成为负担。
像管道符号逐条线一样,是否有某种句法糖标记整个块为文本节点?还是我不知道的现有过滤器?
我正在探索的一种解决方案是创建A:块过滤器之类的东西,它可以用|然后将其传递给Jade,但至少可以说Jade关于创建过滤器的文件很少,因此可能需要一段时间才能弄清楚。如果有人可以为这样的解决方案提供指导,我将不胜感激。
解决方案
来自 翡翠github页面:
p.
foo asdf
asdf
asdfasdfaf
asdf
asd.
产生输出:
<p>foo asdf
asdf
asdfasdfaf
asdf
asd
.
</p>
落后期之后 p
就是您要寻找的。
其他提示
修补后,我弄清楚了完成此操作的过滤器的详细信息。在此处发布答案,因为我认为这对使用Jade的其他人很有用。
创建过滤器的代码非常简单:
var jade = require ("jade");
jade.filters.text = function(block, compiler){
return new TextBlockFilter(block).compile();
};
function TextBlockFilter(node) {
this.node = node;
}
TextBlockFilter.prototype.__proto__ = jade.Compiler.prototype;
TextBlockFilter.prototype.visit = function(node){
// first this is called with a node containing all the block's lines
// as sub-nodes, with their first word interpreted as the node's name
//
// so here, collect all the nodes' text (including its name)
// into a single Text node, and then visit that instead.
// the child nodes won't be visited - we're cutting them out of the
// parse tree
var text = new jade.nodes.Text();
for (var i=0; i < node.length; i++) {
text.push (node[i].name + (node[i].text ? node[i].text[0] : ""));
}
this.visitNode (text);
};
然后标记看起来像这样。请注意,它允许您在之间包含其他玉器:文本块:
p
:text
This is my first line of text,
followed by another
and another. Now let's include a jade link tag:
a(href="http://blahblah.com")
:text
and follow it with even more text
and more,
etc
不隶属于 StackOverflow