|
| 1 | +import 'src/template.dart' as t; |
| 2 | + |
| 3 | +/// A Template can be efficiently rendered multiple times with different |
| 4 | +/// values. |
| 5 | +abstract class Template { |
| 6 | + /// The constructor parses the template source and throws [TemplateException] |
| 7 | + /// if the syntax of the source is invalid. |
| 8 | + /// Tag names may only contain characters a-z, A-Z, 0-9, underscore, and minus, |
| 9 | + /// unless lenient mode is specified. |
| 10 | + factory Template( |
| 11 | + String source, { |
| 12 | + bool lenient, |
| 13 | + bool htmlEscapeValues, |
| 14 | + String name, |
| 15 | + PartialResolver? partialResolver, |
| 16 | + String delimiters, |
| 17 | + }) = t.Template.fromSource; |
| 18 | + |
| 19 | + /// An optional name used to identify the template in error logging. |
| 20 | + String? get name; |
| 21 | + |
| 22 | + /// The template that should be filled when calling [render] or |
| 23 | + /// [renderString]. |
| 24 | + String get source; |
| 25 | + |
| 26 | + /// [values] can be a combination of Map, List, String. Any non-String object |
| 27 | + /// will be converted using toString(). Null values will cause a |
| 28 | + /// [TemplateException], unless lenient module is enabled. |
| 29 | + String renderString(Object? values); |
| 30 | + |
| 31 | + /// [values] can be a combination of Map, List, String. Any non-String object |
| 32 | + /// will be converted using toString(). Null values will cause a |
| 33 | + /// [TemplateException], unless lenient module is enabled. |
| 34 | + void render(Object? values, StringSink sink); |
| 35 | +} |
| 36 | + |
| 37 | +// TODO(stuartmorgan): Remove this. See https://github.com/flutter/flutter/issues/174722. |
| 38 | +// ignore: public_member_api_docs |
| 39 | +typedef PartialResolver = Template? Function(String); |
| 40 | + |
| 41 | +// TODO(stuartmorgan): Remove this. See https://github.com/flutter/flutter/issues/174722. |
| 42 | +// ignore: public_member_api_docs |
| 43 | +typedef LambdaFunction = Object Function(LambdaContext context); |
| 44 | + |
| 45 | +/// Passed as an argument to a mustache lambda function. The methods on |
| 46 | +/// this object may only be called before the lambda function returns. If a |
| 47 | +/// method is called after it has returned an exception will be thrown. |
| 48 | +abstract class LambdaContext { |
| 49 | + /// Render the current section tag in the current context and return the |
| 50 | + /// result as a string. If provided, value will be added to the top of the |
| 51 | + /// context's stack. |
| 52 | + String renderString({Object? value}); |
| 53 | + |
| 54 | + /// Render and directly output the current section tag. If provided, value |
| 55 | + /// will be added to the top of the context's stack. |
| 56 | + void render({Object value}); |
| 57 | + |
| 58 | + /// Output a string. The output will not be html escaped, and will be written |
| 59 | + /// before the output returned from the lambda. |
| 60 | + void write(Object object); |
| 61 | + |
| 62 | + /// Get the unevaluated template source for the current section tag. |
| 63 | + String get source; |
| 64 | + |
| 65 | + /// Evaluate the string as a mustache template using the current context. If |
| 66 | + /// provided, value will be added to the top of the context's stack. |
| 67 | + String renderSource(String source, {Object? value}); |
| 68 | + |
| 69 | + /// Lookup the value of a variable in the current context. |
| 70 | + Object? lookup(String variableName); |
| 71 | +} |
| 72 | + |
| 73 | +/// [TemplateException] is used to obtain the line and column numbers |
| 74 | +/// of the token which caused parse or render to fail. |
| 75 | +abstract class TemplateException implements Exception { |
| 76 | + /// A message describing the problem parsing or rendering the template. |
| 77 | + String get message; |
| 78 | + |
| 79 | + /// The name used to identify the template, as passed to the Template |
| 80 | + /// constructor. |
| 81 | + String? get templateName; |
| 82 | + |
| 83 | + /// The 1-based line number of the token where formatting error was found. |
| 84 | + int get line; |
| 85 | + |
| 86 | + /// The 1-based column number of the token where formatting error was found. |
| 87 | + int get column; |
| 88 | + |
| 89 | + /// The character offset within the template source. |
| 90 | + int? get offset; |
| 91 | + |
| 92 | + /// The template source. |
| 93 | + String? get source; |
| 94 | + |
| 95 | + /// A short source substring of the source at the point the problem occurred |
| 96 | + /// with parsing or rendering. |
| 97 | + String get context; |
| 98 | +} |
0 commit comments