首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将pug编译后的内容追加到pug模板

将pug编译后的内容追加到pug模板
EN

Stack Overflow用户
提问于 2019-08-06 13:46:54
回答 1查看 185关注 0票数 0

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

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

模板的输入为:

代码语言:javascript
复制
[{RowId: 'bla', RowUnit: '-', RowField: pug.compile('input')},
 {RowId: 'blub', RowUnit: 'm', RowField: pug.compile('span')}]

模板如下所示:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
<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>

总而言之,编译后的函数似乎被调用了两次。我可以请教一下吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-07 05:21:07

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

代码语言:javascript
复制
#{rowdef.RowField} This is my span.

这将呈现:

代码语言:javascript
复制
<span>This is my span.</span>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57369614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档