Skip to content

[mustache_template] Adds onMissingVariable to support custom rendering for unresolved variable tags#11403

Open
vlidholt wants to merge 1 commit intoflutter:mainfrom
vlidholt:mustache_template-fallback
Open

[mustache_template] Adds onMissingVariable to support custom rendering for unresolved variable tags#11403
vlidholt wants to merge 1 commit intoflutter:mainfrom
vlidholt:mustache_template-fallback

Conversation

@vlidholt
Copy link
Copy Markdown
Contributor

@vlidholt vlidholt commented Mar 31, 2026

Issue for this feature lives here:
flutter/flutter#184452

This is an addition to the mustache_template package. It adds a onMissingVariable callback to the render methods. This allows the user of the package to dynamically handle template variables that aren't predefined. This can be useful in a variety of cases.

New renderString signature:

String renderString(
  Object? values, {
  MissingVariableCallback? onMissingVariable,
});

Usage:

final String output = Template('_{{missing}}_', name: 'test_template').renderString(
  {},
  onMissingVariable: (name, context) {
    return 'bob';
  },
);

Example use cases:

  • Image transformations, e.g. {{image.avatar.128.128.webp}}
  • Date formatting tokens, e.g. {{date.createdAt.yMMMd.en_US.utc}}
  • Money and number formatting, e.g. {{money.total.USD.en_US.2}}
  • CSS class composition, e.g. {{class.button.primary.large}}
  • Route or URL generation, e.g. {{route.userProfile.42.settings.mobile}}
  • Icon selection by variant, e.g. {{icon.warning.red.24.filled}}
  • CDN asset selection and cache busting, e.g. {{cdn.heroBanner.dark.2x.webp}}

List which issues are fixed by this PR. You must list at least one issue.
This repository does not have issues enabled.

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an onMissingVariable callback to the mustache_template package, allowing for custom rendering or fallback values when variable tags are unresolved. The changes include updates to the Template interface, the internal Renderer logic to support the callback, and the addition of a MissingVariableContext class to provide metadata such as template name and source offset. The package version is bumped to 2.1.0, and comprehensive tests have been added to verify the functionality in both strict and lenient modes. I have no feedback to provide.

@stuartmorgan-g
Copy link
Copy Markdown
Collaborator

This repository does not have issues enabled.

Please see the repository README. (We wouldn't require issues as part of the template if we didn't have an issue tracker.)

Please do file an issue and expand there on the reason for the proposal. We generally don't want to expand the API surface of this package, and it's not clear from the PR description why a client wouldn't be able to provide values for template variables in advance.

@stuartmorgan-g
Copy link
Copy Markdown
Collaborator

(Marking as a draft pending an issue where the feature request is discussed and evaluated in more detail.)

@vlidholt
Copy link
Copy Markdown
Contributor Author

vlidholt commented Apr 1, 2026

Thank you @stuartmorgan-g, I missed where to place the issue. I have now created an issue in the Flutter repo, which is linked in the PR description.

@vlidholt vlidholt marked this pull request as ready for review April 1, 2026 10:41
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the mustache_template package to version 2.1.0, introducing an onMissingVariable callback to the Template rendering methods for custom handling of unresolved variables. The implementation includes the new MissingVariableCallback and MissingVariableContext types, along with corresponding updates to the Renderer and a comprehensive test suite. Feedback suggests that the htmlEscape property in MissingVariableContext should be modified to account for the template's htmlEscapeValues setting in addition to the tag's escape status.

templateName: templateName,
source: source,
offset: node.start,
htmlEscape: node.escape,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The htmlEscape property in MissingVariableContext should accurately reflect whether the rendered output will be HTML-escaped. Currently, it only considers node.escape (whether the tag is {{var}} vs {{{var}}}), but it should also account for the htmlEscapeValues setting of the template. If htmlEscapeValues is false, no escaping is performed regardless of the tag type.

Suggested change
htmlEscape: node.escape,
htmlEscape: node.escape && htmlEscapeValues,

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants