Jade模板:嵌套Mixin

3

下面是mixin

mixin form(title, action)
    legend title
    form.form-horizontal(method='post', action=action)
        label Name:
        input(type='text',name=Name,id=Name)

呈现为
<legend>title</legend>
<form method="post" action="save" class="form-horizontal">
  <label>Name:</label>
  <input type="text"/>
</form>

现在,我将标签和字段提取到另一个mixin中。
mixin form(title, action)
    legend title
    form.form-horizontal(method='post', action=action)

mixin field(name)
    label #{name}:
    input(type='text',name=name,id=name)

并将其用作

mixin form("xxxx", "save")
    mixin field('Name')

这会导致错误

>> Line 1209: Unexpected string
Warning: Jade failed to compile test.jade. Use --force to continue.

能否嵌套Mixin?如何使其作为第一输出进行渲染。

谢谢。

2个回答

1
mixin field(name)
    label #{name}:
    input(type='text',name='#{name}',id='#{name}')

mixin forms(title, action, name)
    legend #{title}
    form.form-horizontal(method='post', action='#{action}')
    block
    +field(name)

测试调用

+forms( '*TheTitle*', '*TheAction*' , '*TheName*' )

渲染
<legend>TheTitle</legend>
<form method="post" action="TheAction" class="form-horizontal"></form>
<label>TheName:</label>
<input type="text" name="TheName" id="TheName"/>

你必须分别定义mixin,然后在“forms” mixin的定义中调用“field” mixin。

1

2
这个链接里的人很简洁,但他是正确的。实际上,我花了一些时间来尝试理解他的意思,所以也许对某些人有用。你必须在包含mixin的声明末尾添加块语句。 - Oskar Skuteli
我不再使用mixin,但我认为你只需要像Oscar说的那样在结尾添加“block”。就像链接所示,但只需添加一次。https://github.com/jadejs/jade/issues/1693 - Avec

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接