Skip to content

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

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

[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

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.)

@stuartmorgan-g stuartmorgan-g marked this pull request as draft March 31, 2026 19:50
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