Skip to content

[Question] Do not replace if {{identifier}} data is not provided. #373

Closed

Description

@leisenstein

If you have a template, but the field that should be replaced is not included when template(data) is called, it replaces it with a blank string.

Is there a way to just skip that field and leave {{identifier}} in the result?

Activity

  1. rexm commented on Oct 15, 2020

    @rexm
    Member

    This unit test shows how to customize the output when a field binding can't be resolved:

    handlebars.Configuration.UnresolvedBindingFormatter = "('{0}' is undefined)";

    {0} is the token that represents the name of the binding, but doesn't include any of the surrounding handlebars {{ }} syntax so you'd need to add that into your string formatter.

  2. oformaniuk commented on Oct 15, 2020

    @oformaniuk
    Member

    Alternatively you can use helperMissing feature (see this unit test for reference). This would handle both fields and helpers (in case you want to do any kind of post processing that may involve helpers).

  3. marocano1 commented on Mar 5, 2025

    @marocano1

    I am having the same problem. Handlebars is stripping out all the items in the template that do not have corresponding properties in the data object. I have tried to implement the "helperMissing" solution. Unfortunately, the "arguments" parameter is always empty and it doesn't work.

    Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions