表格(部分):
include ../mixins/form-helpers
form(action='/users/create', method='post')
div.fields
fieldset
mixin field('text', 'email', 'Email')
mixin field('password', 'password', 'Password')
mixin field('password', 'password_confirmation', 'Confirm password')
mixin field('username', 'username', 'Username')
mixin submit('Register')
Field mixin看起来像这样:
mixin field(type, n, label)
.clearfix
label(for='#{n}') #{label}
.input
input#password.span8(name=n, type=type, value=user[name])
控制器:
(req, res)->
user =
email: 'someemail'
password: ''
password_confirmation: ''
username: 'someusername'
res.render 'users/new', user:user
我正在努力使字段mixin更通用,所以我可以重用它。我正在寻找一种方法来取代"用户[名称]"使用"通用[名称]"
答案 0 :(得分:1)
您可以使用短划线( - )
在jade中的模板内设置变量因此,如果您使用通用[名称],则可以在表单中执行以下操作:
-var generic = user;
include ../mixins/form-helpers
form(action='/users/create', method='post')
div.fields
fieldset
mixin field('text', 'email', 'Email')
mixin field('password', 'password', 'Password')
mixin field('password', 'password_confirmation', 'Confirm password')
mixin field('username', 'username', 'Username')
mixin submit('Register')