-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[mustache_template] Add example app and code excerpts #11316
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
saudademjj
wants to merge
3
commits into
flutter:main
from
saudademjj:saudademjj/mustache-template-example-app
+279
−54
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
47 changes: 47 additions & 0 deletions
47
third_party/packages/mustache_template/example/lib/main.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,47 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // ignore_for_file: avoid_print, prefer_function_declarations_over_variables | ||
|
|
||
| import 'package:mustache_template/mustache_template.dart'; | ||
|
|
||
| void main() { | ||
| // Basic template rendering with sections and inverted sections. | ||
| const source = ''' | ||
| {{# names }} | ||
| <div>{{ lastname }}, {{ firstname }}</div> | ||
| {{/ names }} | ||
| {{^ names }} | ||
| <div>No names.</div> | ||
| {{/ names }} | ||
| '''; | ||
|
|
||
| final template = Template(source, name: 'example.html'); | ||
|
|
||
| final String output = template.renderString({ | ||
| 'names': [ | ||
| {'firstname': 'Greg', 'lastname': 'Lowe'}, | ||
| {'firstname': 'Bob', 'lastname': 'Johnson'}, | ||
| ], | ||
| }); | ||
|
|
||
| print('=== Basic Template ==='); | ||
| print(output); | ||
|
|
||
| // Nested paths | ||
| final nested = Template('{{ author.name }}'); | ||
| print('=== Nested Paths ==='); | ||
| print( | ||
| nested.renderString({ | ||
| 'author': {'name': 'Greg Lowe'}, | ||
| }), | ||
| ); | ||
|
|
||
| // Lambdas | ||
| final lambdaTemplate = Template('{{# transform }}hello{{/ transform }}'); | ||
| final String Function(LambdaContext) lambda = (LambdaContext ctx) => | ||
| ctx.renderString().toUpperCase(); | ||
saudademjj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print('=== Lambdas ==='); | ||
| print(lambdaTemplate.renderString({'transform': lambda})); | ||
| } | ||
118 changes: 118 additions & 0 deletions
118
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,118 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // This file exists solely to host compiled excerpts for README.md, and is not | ||
| // intended for use as an actual example application. | ||
|
|
||
| // ignore_for_file: avoid_print, public_member_api_docs, unreachable_from_main | ||
| // ignore_for_file: prefer_function_declarations_over_variables | ||
| // ignore_for_file: specify_nonobvious_local_variable_types | ||
|
|
||
| // #docregion ExampleUsage | ||
| import 'package:mustache_template/mustache_template.dart'; | ||
|
|
||
| void main() { | ||
| const source = ''' | ||
| {{# names }} | ||
| <div>{{ lastname }}, {{ firstname }}</div> | ||
| {{/ names }} | ||
| {{^ names }} | ||
| <div>No names.</div> | ||
| {{/ names }} | ||
| {{! I am a comment. }} | ||
| '''; | ||
|
|
||
| final template = Template(source, name: 'template-filename.html'); | ||
|
|
||
| final output = template.renderString({ | ||
| 'names': [ | ||
| {'firstname': 'Greg', 'lastname': 'Lowe'}, | ||
| {'firstname': 'Bob', 'lastname': 'Johnson'}, | ||
| ], | ||
| }); | ||
|
|
||
| print(output); | ||
| } | ||
| // #enddocregion ExampleUsage | ||
|
||
|
|
||
| void nestedPathsExample() { | ||
| // #docregion NestedPaths | ||
| final t = Template('{{ author.name }}'); | ||
| final output = t.renderString({ | ||
| 'author': {'name': 'Greg Lowe'}, | ||
| }); | ||
| // #enddocregion NestedPaths | ||
| print(output); | ||
| } | ||
|
|
||
| void partialsExample() { | ||
| // #docregion Partials | ||
| final partial = Template('{{ foo }}', name: 'partial'); | ||
|
|
||
| final resolver = (String name) { | ||
| if (name == 'partial-name') { | ||
| // Name of partial tag. | ||
| return partial; | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| final t = Template('{{> partial-name }}', partialResolver: resolver); | ||
|
|
||
| final output = t.renderString({'foo': 'bar'}); // bar | ||
| // #enddocregion Partials | ||
| print(output); | ||
| } | ||
|
|
||
| void lambdaSimpleExample() { | ||
| // #docregion LambdaSimple | ||
| final t = Template('{{# foo }}{{/ foo }}'); | ||
| final lambda = (_) => 'bar'; | ||
| final output = t.renderString({'foo': lambda}); // bar | ||
| // #enddocregion LambdaSimple | ||
| print(output); | ||
| } | ||
|
|
||
| void lambdaShownExample() { | ||
| // #docregion LambdaShown | ||
| final t = Template('{{# foo }}hidden{{/ foo }}'); | ||
| final lambda = (_) => 'shown'; | ||
| final output = t.renderString({'foo': lambda}); // shown | ||
| // #enddocregion LambdaShown | ||
| print(output); | ||
| } | ||
|
|
||
| void lambdaRenderExample() { | ||
| // #docregion LambdaRender | ||
| final t = Template('{{# foo }}oi{{/ foo }}'); | ||
| final lambda = (LambdaContext ctx) => | ||
| '<b>${ctx.renderString().toUpperCase()}</b>'; | ||
| final output = t.renderString({'foo': lambda}); // <b>OI</b> | ||
| // #enddocregion LambdaRender | ||
| print(output); | ||
| } | ||
|
|
||
| void lambdaRenderBarExample() { | ||
| // #docregion LambdaRenderBar | ||
| final t = Template('{{# foo }}{{bar}}{{/ foo }}'); | ||
| final lambda = (LambdaContext ctx) => | ||
| '<b>${ctx.renderString().toUpperCase()}</b>'; | ||
| final output = t.renderString({'foo': lambda, 'bar': 'pub'}); // <b>PUB</b> | ||
| // #enddocregion LambdaRenderBar | ||
| print(output); | ||
| } | ||
|
|
||
| void lambdaRenderSourceExample() { | ||
| // #docregion LambdaRenderSource | ||
| final t = Template('{{# foo }}{{bar}}{{/ foo }}'); | ||
| final lambda = (LambdaContext ctx) => | ||
| ctx.renderSource('${ctx.source} {{cmd}}'); | ||
| final output = t.renderString({ | ||
| 'foo': lambda, | ||
| 'bar': 'pub', | ||
| 'cmd': 'build', | ||
| }); // pub build | ||
| // #enddocregion LambdaRenderSource | ||
| print(output); | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
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,10 @@ | ||
| name: mustache_template_example | ||
| description: Example app for mustache_template package. | ||
| publish_to: none | ||
|
|
||
| environment: | ||
| sdk: ^3.9.0 | ||
|
|
||
| dependencies: | ||
| mustache_template: | ||
| path: ../ |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.