How to combine and pass the variable Handlebars to partial

These are the partial Handlebars guys that we named input-field.
We are trying to dynamically create name and email fields based on the number of participants selected on the previous page.

<div class="form-group {{errors.fullName.class}}" id="fullName">
  <label for="full-name">Your full name</label>
  <p class="message-error">{{errors.fullName.message}}</p>
  <input type="text" class="form-control" name="fullName" value="{{values.fullName}}">
</div>

We have some functions that create and display error messages if any of these form fields is blank.
Instead, {{errors.fullName.class}}we need it to be {{errors.fullName{{this}}.class}}.

We found that Handlebars does not allow reference to another handlebars variable inside the handlebars statement in this way.

Everything in every cycle:

{{#each otherOccupiers}}
   {{> input-field}}
{{/each}} 

Does anyone have any idea how to achieve this effect, maybe by writing a helper function handlebars or some other way to do this concatenation.

+4
1

, , handlebars, :

{
  otherOccupiers: ["A", "B"],
  errors: {
    fullName: {
      class: "error-class",
      message: "error-message"
    }
  }
}

, , :

{
  errors: {
    otherOccupiers: [
      "A" : {
        fullName: {
          class: "error-class",
          message: "error-message"
        }
      }
      "B" : {
        fullName: {
          class: "error-class",
          message: "error-message"
        }
      }
    ]
  }
}

:

{{#each errors.otherOccupiers}}
  <div class="form-group {{this.fullName.class}}" id="fullName">
    <label for="full-name">Your full name</label>
    <p class="message-error">{{this.fullName.message}}</p>
    <input type="text" class="form-control" name="fullName">
  </div>
{{/each}}
+1

Source: https://habr.com/ru/post/1658334/


All Articles