-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[mustache_template] add example app and migrate README to excerpts #11333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
mozammal-hossain
wants to merge
11
commits into
flutter:main
from
mozammal-hossain:fix/add-example-mustache-template
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9b1652d
mustache_template: add example app and migrate README to excerpts
mozammal-hossain 4b77fd2
fix(mustache_template): correct doc region tags in example code
mozammal-hossain d39ef31
feat(mustache_template): enhance README and examples with lambda usage
mozammal-hossain f20d71f
chore(mustache_template): Remove unnecessary function definition
mozammal-hossain c3f0285
feat(mustache_template): enhance example app with structured code exc…
mozammal-hossain 061eed1
fix(mustache templates): Undo readme file
mozammal-hossain 2ded2f9
fix(mustache templates): Undo readme file
mozammal-hossain 272505c
Merge branch 'fix/add-example-mustache-template' of https://github.co…
mozammal-hossain bcd9b60
Merge branch 'main' into fix/add-example-mustache-template
mozammal-hossain cb50370
Restore mustache_template README from 98580c6
mozammal-hossain fc54059
Update mustache_template examples and README for strict and lenient m…
mozammal-hossain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
third_party/packages/mustache_template/example/lib/readme_excerpts.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // This file hosts README code excerpts (see README.md) and is imported by the | ||
| // example runner and excerpt tests. | ||
|
|
||
| // ignore_for_file: public_member_api_docs | ||
|
|
||
| import 'package:mustache_template/mustache_template.dart'; | ||
|
|
||
| String basicRenderExample() { | ||
| // #docregion BasicRender | ||
| var source = ''' | ||
| {{# names }} | ||
| <div>{{ lastname }}, {{ firstname }}</div> | ||
| {{/ names }} | ||
| {{^ names }} | ||
| <div>No names.</div> | ||
| {{/ names }} | ||
| {{! I am a comment. }} | ||
| '''; | ||
|
|
||
| var template = Template(source, name: 'names-template'); | ||
| var output = template.renderString(<String, Object>{ | ||
| 'names': <Map<String, String>>[ | ||
| <String, String>{'firstname': 'Greg', 'lastname': 'Lowe'}, | ||
| <String, String>{'firstname': 'Bob', 'lastname': 'Johnson'}, | ||
| ], | ||
| }); | ||
| // #enddocregion BasicRender | ||
| return output; | ||
| } | ||
|
|
||
| String nestedPathsExample() { | ||
| // #docregion NestedPaths | ||
| var t = Template('{{ author.name }}'); | ||
| var output = t.renderString(<String, Object>{ | ||
| 'author': {'name': 'Greg Lowe'}, | ||
| }); | ||
| // #enddocregion NestedPaths | ||
| return output; | ||
| } | ||
|
|
||
| String partialsExample() { | ||
| // #docregion Partials | ||
| var partial = Template('{{ foo }}', name: 'partial'); | ||
| Template resolver(String name) { | ||
| if (name == 'partial-name') { | ||
| return partial; | ||
| } | ||
| throw StateError('Unknown partial: $name'); | ||
| } | ||
|
|
||
| var t = Template('{{> partial-name }}', partialResolver: resolver); | ||
| var output = t.renderString({'foo': 'bar'}); // bar | ||
| // #enddocregion Partials | ||
| return output; | ||
| } | ||
|
|
||
| String lambdaSimpleExample() { | ||
| // #docregion LambdaSimpleValue | ||
| var t = Template('{{ foo }}'); | ||
|
|
||
| var lambda = (_) => 'bar'; | ||
|
|
||
| var output = t.renderString({'foo': lambda}); // bar | ||
| // #enddocregion LambdaSimpleValue | ||
| return output; | ||
| } | ||
|
|
||
| String lambdaShownExample() { | ||
| // #docregion LambdaSectionReplacement | ||
| var t = Template('{{# foo }}hidden{{/ foo }}'); | ||
| var lambda = (_) => 'shown'; | ||
|
|
||
| var output = t.renderString({'foo': lambda}); // shown | ||
| // #enddocregion LambdaSectionReplacement | ||
| return output; | ||
| } | ||
|
|
||
| String lambdaRenderExample() { | ||
| // #docregion LambdaRenderString | ||
| var t = Template('{{# foo }}{{bar}}{{/ foo }}'); | ||
| var lambda = (LambdaContext context) => | ||
| '<b>${context.renderString().toUpperCase()}</b>'; | ||
|
|
||
| var output = t.renderString({'foo': lambda, | ||
| 'bar': 'pub', | ||
| }); // <b>PUB</b> | ||
| // #enddocregion LambdaRenderString | ||
| return output; | ||
| } | ||
|
|
||
| String lambdaRenderSourceExample() { | ||
| // #docregion LambdaRenderSource | ||
| var t = Template('{{# foo }}{{bar}}{{/ foo }}'); | ||
| var lambda = (LambdaContext ctx) => | ||
| '<b>${ctx.renderString().toUpperCase()}</b>'; | ||
|
|
||
| var output = t.renderString({'foo': lambda, | ||
| 'bar': 'pub', | ||
| }); // <b>PUB</b> | ||
| // #enddocregion LambdaRenderSource | ||
| return output; | ||
| } | ||
|
|
||
| String strictModeBehaviorExample() { | ||
| // #docregion StrictMode | ||
| try { | ||
| Template('{{missing}}').renderString({}); | ||
| return 'No exception thrown (unexpected)'; | ||
| } on TemplateException catch (e) { | ||
| return 'Strict mode exception: ${e.runtimeType}'; | ||
| } | ||
| // #enddocregion StrictMode | ||
| } | ||
|
|
||
| String lenientModeBehaviorExample() { | ||
| // #docregion LenientMode | ||
| final t = Template('{{missing}}', lenient: true); | ||
| final String output = t.renderString({}); // '' | ||
| // #enddocregion LenientMode | ||
| return output; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // ignore_for_file: avoid_print | ||
|
|
||
| import 'package:mustache_template_example/readme_excerpts.dart'; | ||
|
|
||
| void main() { | ||
| print(basicRenderExample()); | ||
| print(nestedPathsExample()); | ||
| print(partialsExample()); | ||
| print(lambdaSimpleExample()); | ||
| print(lambdaShownExample()); | ||
| print(lambdaRenderExample()); | ||
| print(lambdaRenderSourceExample()); | ||
| print(strictModeBehaviorExample()); | ||
| print(lenientModeBehaviorExample()); | ||
| } |
12 changes: 12 additions & 0 deletions
12
third_party/packages/mustache_template/example/pubspec.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| name: mustache_template_example | ||
| publish_to: none | ||
|
|
||
| environment: | ||
| sdk: ^3.9.0 | ||
|
|
||
| dependencies: | ||
| mustache_template: | ||
| path: ../ | ||
|
|
||
| dev_dependencies: | ||
| test: ^1.16.5 |
69 changes: 69 additions & 0 deletions
69
third_party/packages/mustache_template/example/test/readme_excerpts_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // ignore_for_file: avoid_relative_lib_imports | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:mustache_template_example/readme_excerpts.dart' | ||
| as readme_excerpts; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import '../main.dart' as example_app; | ||
|
|
||
| void main() { | ||
| group('Example app', () { | ||
| test('example app runs without error', () { | ||
| expect( | ||
| () => runZoned<void>( | ||
| () => example_app.main(), | ||
| zoneSpecification: ZoneSpecification( | ||
| print: (Zone self, ZoneDelegate parent, Zone zone, String line) {}, | ||
| ), | ||
| ), | ||
| returnsNormally, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| group('README excerpts', () { | ||
| test('basic render example includes rendered names', () { | ||
| final String out = readme_excerpts.basicRenderExample(); | ||
| expect(out, contains('Lowe, Greg')); | ||
| expect(out, contains('Johnson, Bob')); | ||
| }); | ||
|
|
||
| test('nested paths example renders the nested value', () { | ||
| expect(readme_excerpts.nestedPathsExample(), equals('Greg Lowe')); | ||
| }); | ||
|
|
||
| test('partials example renders the partial output', () { | ||
| expect(readme_excerpts.partialsExample(), equals('bar')); | ||
| }); | ||
|
|
||
| test('simple lambda example renders the replacement text', () { | ||
| expect(readme_excerpts.lambdaSimpleExample(), equals('bar')); | ||
| }); | ||
|
|
||
| test('lambda block example renders the alternate text', () { | ||
| expect(readme_excerpts.lambdaShownExample(), equals('shown')); | ||
| }); | ||
|
|
||
| test('lambda render example uppercases the section body', () { | ||
| expect(readme_excerpts.lambdaRenderExample(), equals('<b>PUB</b>')); | ||
| }); | ||
|
|
||
| test('lambda renderSource example uppercases the section body', () { | ||
| expect( | ||
| readme_excerpts.lambdaRenderSourceExample(), equals('<b>PUB</b>')); | ||
| }); | ||
|
|
||
| test('strict mode throws for missing keys', () { | ||
| expect( | ||
| readme_excerpts.strictModeBehaviorExample(), | ||
| contains('Strict mode exception'), | ||
| ); | ||
| }); | ||
|
|
||
| test('lenient mode renders empty for missing keys', () { | ||
| expect(readme_excerpts.lenientModeBehaviorExample(), isEmpty); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should probably be under
example/bin/main.dart.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wouldn't show up on pub if we do that (see https://dart.dev/tools/pub/package-layout#examples). If we want a
binentrypoint it would need to be a thin wrapper aroundlib/main.dart.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL!