wesbos
wesbos

Reputation: 26317

Jade variable attribute names

I have a variable called current which is used to set the checked value of an html input

so current can be either '' (blank) or checked=true

Since you cant set HTML attributes to false, I can't do this:

input(checked="#{current}")

So, I need so use a variable as the entire attribute. Is this possible with Jade? Other than just writing the HTML string in there

Edit to be more clear. I want to do this:

someVar = "checked=true"    
input(somevVar) // <input checked="true">

Upvotes: 0

Views: 2327

Answers (2)

Peter Lyons
Peter Lyons

Reputation: 146074

jade supports boolean attribute such as checked directly via this syntax:

input(type="checkbox", checked=current)

From the docs:

Boolean attributes are also supported:

input(type="checkbox", checked)

Boolean attributes with code will only output the attribute when true:

input(type="checkbox", checked=someValue)

Make sure you are specifying the !doctype 5 directive as well in your layout template.

Here's an example:

var jade = require('jade');

var templateFn = jade.compile('doctype 5\ninput(type="checkbox", checked=current)\n');
console.log(templateFn({current: true}));
console.log(templateFn({current: false}));

It outputs:

<!DOCTYPE html><input type="checkbox" checked>
<!DOCTYPE html><input type="checkbox">

Upvotes: 1

origin1tech
origin1tech

Reputation: 749

You can do it as such:

input(type='checkbox', id='IsPublic', name='IsPublic', value= model.IsPublic)

Where model.IsPublic is from the model passed in from your response in node.

In the above example it is backed with mongodb where the model looks like:

IsPublic: { type: Boolean, default: false}

in the above case there is a property that denotes public or not yours would be "current".

NOTE: don't be confused by "model" personally i pass in everything or the core properties as an object called "model" for consistency. In your case you may be passing in the property directly so don't be confused by that part just to clarify.

Upvotes: 0

Related Questions