KKendle
KKendle

Reputation: 61

pugJS passing a variable to a mixin in a loop

Trying to loop over some html based on how many questions there are while using a mixin.

I'm expecting that this code would loop over the div 3 times and pass each question into the mixins of input-label and input-field and spit out the proper html as if I'm using a mixin with just text.

PUG Code

- var questions = ['question one', 'question two', 'question three'];
- var answers = ['answer one', 'answer two', 'answer three'];

- for (var i = 0; i < questions.length; i++) {
    div.row.row-loose
        div.col-md-6
            +input-label(#{questions[i]})
        div.col-md-5
            +input-field()(val=#{answers[i]} disabled)
        - if(i == 0) {
            span &nbsp;   
        - }
        - else {
            div.col-md-1
                +button-default('Modify')                    
        - }
- }

input-label.pug

mixin input-label(label)
    label.control-label(class=attributes.class required for=attributes.id+'-'+label)
        if attributes.required
            span.required *
        =label+':'

input-field.pug

mixin input-field()
    input.form-control(type='text' class=attributes.class id=attributes.id value=attributes.value disabled=attributes.disabled readonly=attributes.readonly)

expected results

<div class="row row-loose">
    <div class="col-md-6">
        <label class="control-label">question one</label>
    </div>
    <div class="col-md-5">
        <input class="form-control" type="text" disabled="disabled" val="answer one" />
    </div><span>&nbsp;   </span></div>
<div class="row row-loose">
    <div class="col-md-6">
        <label class="control-label">question two</label>
    </div>
    <div class="col-md-5">
        <input class="form-control" type="text" disabled="disabled" val="answer two" />
    </div>
    <div class="col-md-1">
        <button class="btn btn-default" type="button">Modify</button>
    </div>
</div>
<div class="row row-loose">
    <div class="col-md-6">
        <label class="control-label">question three</label>
    </div>
    <div class="col-md-5">
        <input class="form-control" type="text" disabled="disabled" val="answer three" />
    </div>
    <div class="col-md-1">
        <button class="btn btn-default" type="button">Modify</button>
    </div>
</div>

actual result - pugjs error

27|                 div.row.row-loose
28|                     div.col-md-6
29|                         +input-label(#{questions[i]})
------------------------------------------^
30|                     div.col-md-5
31|                         +input-field()(val=#{questions[i]} disabled)
32|                     - if(i == 0) {

Syntax Error: Unexpected character '#'

Upvotes: 4

Views: 1825

Answers (1)

KKendle
KKendle

Reputation: 61

Found the problem..

Variables didn't need the interpolation since it's being passed to a mixin and not just as text. The input-label mixin call should read +input-label(questions[i])

And for the input-field mixin call, I had the wrong attribute. It's value not val. And also removing the interpolation. So looks like this: +input-field()(value=answers[i] disabled)

Upvotes: 2

Related Questions