【问题标题】:Append pug compiled Content to pug template将 pug 编译的内容附加到 pug 模板
【发布时间】:2019-08-06 05:46:54
【问题描述】:

我们根据表格生成输入字段。在这些过程中,标题(标题,单位)不变,但可能有多个列具有相同的输入类型(最简单的是 a ,但也可能是动态 svgs),但内容不同。

我们希望动态生成 tds 内容并在渲染时将其附加到模板中。

模板的输入是:

[{RowId: 'bla', RowUnit: '-', RowField: pug.compile('input')},
 {RowId: 'blub', RowUnit: 'm', RowField: pug.compile('span')}]

模板如下所示:

mixin addrow(rowdef)
    tr(id= rowdef.RowId)
        th= rowdef.RowId

        td()
            #{rowdef.RowField()}

        th= "[" + rowdef.RowUnit + "]"

table(class="dialogcontents")
    each rowdef in Contents
        +addrow(rowdef)

button(class="okbtn") Ok
button(class="cancelbtn") Cancel

但是上面的编译是这样的:

<table class="dialogcontents">
    <tr id="bla">
        <th>bla</th>
        <td><<input/>></<input/>></td>
             ^--- It looks like the tagname is "<input/>", so the function is compiled and applied as string then
        <th>[-]</th>
    </tr>
    <tr id="blub">
        <th>blub</th>
        <td><<span></span>></<span></span>></td>
             ^--- as above
        <th>[m]</th>
    </tr>
</table> 
<button class="okbtn">Ok</button>
<button class="cancelbtn">Cancel</button>

总而言之,编译后的函数似乎被调用了两次。请问有什么建议吗?

【问题讨论】:

    标签: javascript node.js pug


    【解决方案1】:

    我不确定你更广泛的目标是什么,但如果你想使用本地定义的标签,你可以设置RowField: 'span',然后使用标签插值:

    #{rowdef.RowField} This is my span.
    

    将渲染:

    <span>This is my span.</span>
    

    【讨论】:

    • 更广泛的目标是将动态内容附加到模板中,因此 td 的子节点可能是 、 或任何其他 html 标记,可能带有子节点
    • 想法是转发pug编译的函数,这样以后可以根据本地设置修改属性,但函数可以重用。与这个答案 (stackoverflow.com/a/55779930/3559690) 类似,但没有提供完整的 html,而是一个看起来像 pug.compile('input(group=var)') 的函数,生成像
    猜你喜欢
    • 2017-09-04
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    相关资源
    最近更新 更多