在开发Jade模板库时,最好使用mixin的块作为属性值,从而简化最终用户的语法。
最终用户可以选择3种创建按钮的方法;通过标签,按钮标签和输入标签。对于输入标记,我想使用块作为值属性,因此语法始终为:
+abtn
| A Button
+btn
| Button
+ibtn
| I Button
+abtn(disabled)
| A Button Disabled
+btn(disabled)
| Button Disabled
+ibtn(disabled)
| I Button Disabled
目前,mixins的精简版看起来像:
mixin abtn
- attributes.href = attributes.href || '#'
- attributes.role = attributes.role || 'button'
- if (attributes.disabled) {
- attributes.class = (attributes.class === undefined) ? 'disabled' : attributes.class + ' disabled';
- attributes.disabled = null
- }
a.btn(attributes)
block
mixin btn
- attributes.type = attributes.type || 'button'
button.btn(attributes)
block
mixin ibtn
- attributes.class = (attributes.class === undefined) ? 'btn' : attributes.class + ' btn';
- attributes.type = attributes.type || 'button'
input(attributes=attributes)
麻烦是指定ibtn的value属性要求最终用户语法为:
+abtn
| A Button
+btn
| Button
+ibtn(value='I Button')
+abtn(disabled)
| A Button Disabled
+btn(disabled)
| Button Disabled
+ibtn(value='I Button Disabled', disabled)
哪个不一致。
是否可以通过内置的javascript访问块,以便可以在属性中使用其内容的非空白版本?如果是这样的话?
修改
为了澄清,我想要以下玉代码:
+ibtn
| My button value
输出:
<input value="My button value">
答案 0 :(得分:0)
Well, it's a syntax problem. When you run a mixin
, becomes in this way, since the parentheses you can give arguments. That is so:
mixin myMixin (arg1, arg2)
p=arg1
p=arg2
+myMixin('Jade is Cool', 'Yeahh!')
is rendered into...
<p>Jade is Cool</p>
<p>Yeahh!</p>
In this case you want to spend attributes
, it becomes as follows:
mixin myMixin (arg1, arg2)
p( id=attributes.id )=arg1
p( class=attributes.class, value=attributes.value )=arg2
+myMixin('Jade is Cool', 'Yeahh!').myClass#MyId( value="Kool" )
which is rendered as ...
<p id="MyId">Jade is Cool</p>
<p class="myClass" value="Kool">Yeahh!</p>
Note standing twice parentheses, the first makes the mixin
leading arguments and run second are attributes mixin
. Where applicable:
+abtn()
| A Button
+btn()
| Button
+ibtn()(value='I Button')
+abtn()(disabled)
| A Button Disabled
+btn()(disabled)
| Button Disabled
+ibtn()(value='I Button Disabled', disabled)
Remember that mixin
s are functions in javascript.