1

Form (partial):

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 looks like this:

mixin field(type, n, label)
  .clearfix
    label(for='#{n}') #{label}
    .input
      input#password.span8(name=n, type=type, value=user[name])

Controller:

(req, res)->

  user =
    email: 'someemail'
    password: ''
    password_confirmation: ''
    username: 'someusername'

  res.render 'users/new',  user:user

I am trying to make the field mixin a little more generic so I can reuse it. I am looking for a way to replace "user[name]" with "generic[name]"

mtsr
  • 3,092
  • 1
  • 14
  • 21
Pickels
  • 33,902
  • 26
  • 118
  • 178

1 Answers1

1

You can set a variable inside a template in jade with a dash ( - )

So if you use generic[name] you can do the following in your form:

-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')
Pickels
  • 33,902
  • 26
  • 118
  • 178